/lib/AhkDllThread.ahk

http://github.com/Skiouros/Macro · AutoHotKey · 145 lines · 99 code · 3 blank · 43 comment · 11 complexity · d288ed23826486fed0a07c853ed4f239 MD5 · raw file

  1. AhkDllThread(dll="AutoHotkey.dll",obj=0){
  2. static
  3. local v,v1,v2
  4. static init, DynaCall:="DynaCall", MemoryLoadLibrary:="MemoryLoadLibrary",MemoryFreeLibrary:="MemoryFreeLibrary"
  5. ,ResourceLoadLibrary:="ResourceLoadLibrary", MemoryGetProcAddress:="MemoryGetProcAddress"
  6. static functions ="
  7. (Join
  8. ahkKey:s|ahkFunction:s=sssssssssss|ahkPostFunction:i=sssssssssss|
  9. ahkdll:ui=sss|ahktextdll:ui=sss|ahkReady:|ahkReload:ui=|
  10. ahkTerminate:i|addFile:ui=sucuc|addScript:ui=si|ahkExec:ui=s|
  11. ahkassign:ui=ss|ahkExecuteLine:ui=uiuiui|ahkFindFunc:ui=s|
  12. ahkFindLabel:ui=s|ahkgetvar:s=sui|ahkLabel:ui=sui|ahkPause:s
  13. )"
  14. static AhkDllThreadfunc ="
  15. (Join`r`n
  16. #Persistent
  17. #NoTrayIcon
  18. Return
  19. AhkDllThread(dll=""AutoHotkey.dll"",obj=0,map=""""){
  20. static
  21. local v,v1,v2
  22. static functions = ""ahkKey:s|ahkFunction:s=sssssssssss|ahkPostFunction:i=sssssssssss|""
  23. . ""ahkdll:ui=sss|ahktextdll:ui=sss|ahkReady:|ahkReload:ui=|""
  24. . ""ahkTerminate:i|addFile:ui=sucuc|addScript:ui=si|ahkExec:ui=s|""
  25. . ""ahkassign:ui=ss|ahkExecuteLine:ui=uiuiui|ahkFindFunc:ui=s|""
  26. . ""ahkFindLabel:ui=s|ahkgetvar:s=sui|ahkLabel:ui=sui|ahkPause:s""
  27. If !dll {
  28. Loop `% i
  29. {
  30. idx:=A_Index
  31. `%MemoryFreeLibrary`%(dllmodule`%A_Index`%)
  32. obj`%A_Index`%:="""",dll`%A_Index`%:="""",dllmodule`%A_Index`%:=""""
  33. }
  34. i=0
  35. return
  36. } else if (!FileExist(dll)){
  37. MsgBox File: `%dll`% does not exist`, provide correct path for AutoHotkey.dll
  38. ExitApp
  39. }
  40. i++
  41. dllmodule`%i`%:= MemoryLoadLibrary(dll)
  42. if IsObject(obj)
  43. object:=obj
  44. else
  45. object := Object()
  46. object[""""]:=dllmodule`%i`%
  47. Loop,Parse,functions,|
  48. {
  49. StringSplit,v,A_LoopField,:
  50. object[map="""" ? v1 : !InStr(map,v1) ? v1 : SubStr(map,InStr(map,v1)+StrLen(v1)+1,InStr(map,A_Space,0,InStr(map,v1)))]:=DynaCall(MemoryGetProcAddress(dllmodule`%i`%,v1),v2)
  51. }
  52. obj`%i`%:=object
  53. dll`%i`%:=dll
  54. return &obj`%i`%
  55. }
  56. )"
  57. If !(dll){
  58. If !AHK_H{
  59. return DllCall(dll "\ahkFunction","Str","AhkDllThread","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str")
  60. }
  61. Loop % i
  62. {
  63. idx:=A_Index
  64. %MemoryFreeLibrary%(dllmodule%A_Index%)
  65. obj%A_Index%:="",dll%A_Index%:="",dllmodule%A_Index%:=""
  66. }
  67. i=0
  68. return
  69. } else if (!FileExist(dll) && !A_IsCompiled){
  70. MsgBox File: %dll%`ndoes not exist`, provide correct path for AutoHotkey.dll
  71. ExitApp
  72. }
  73. If !AHK_H{
  74. If (init || init:=DllCall("LoadLibrary","Str",dll)){
  75. If DllCall(dll "\ahktextdll","Str",AhkDllThreadfunc,"Str","","Str","","Cdecl UInt")
  76. Return Object(0+DllCall(dll "\ahkFunction","Str","AhkDllThread","Str",dll,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str"))
  77. else
  78. Return 0,ErrorLevel:="Could not load AutoHotkey.dll"
  79. } else {
  80. MsgBox Could not load %dll%
  81. Return 0
  82. }
  83. }
  84. i++
  85. dllmodule%i%:=A_IsCompiled ? %ResourceLoadLibrary%(dll) : %MemoryLoadLibrary%(dll)
  86. if IsObject(obj)
  87. object:=obj
  88. else
  89. object := Object()
  90. object[""]:=dllmodule%i%
  91. Loop,Parse,functions,|
  92. {
  93. StringSplit,v,A_LoopField,:
  94. object[map="" ? v1 : !InStr(map,v1) ? v1 : SubStr(map,InStr(map,v1)+StrLen(v1)+1,InStr(map,A_Space,0,InStr(map,v1)))]:=%DynaCall%(%MemoryGetProcAddress%(dllmodule%i%,v1),v2)
  95. }
  96. obj%i%:=object
  97. dll%i%:=dll
  98. return obj%i%
  99. }
  100. /*
  101. AhkDllThread(dll=""){
  102. static
  103. static ResourceLoadLibrary:="ResourceLoadLibrary"
  104. static functions:="ahkdll|ahktextdll|ahkReady|ahkReload|ahkTerminate|addFile|addScript|assign|ahkExecuteLine|ahkFindFunc|ahkFindLabel|ahkgetvar|ahkLabel|ahkPause"
  105. If !(dll){
  106. Loop % i
  107. {
  108. idx:=A_Index
  109. Loop,Parse,functions,|
  110. DynaCall(dll%idx% . "\" . A_LoopField)
  111. MemoryFreeLibrary(dllmodule%A_Index%)
  112. obj%A_Index%=
  113. dll%A_Index%=
  114. dllmodule%A_Index%=
  115. }
  116. i=0
  117. return
  118. }
  119. i++
  120. dllmodule%i%:=A_IsCompiled ? %ResourceLoadLibrary%(dll) : MemoryLoadLibrary(dll)
  121. object := Object()
  122. Loop,Parse,functions,|
  123. object[A_LoopField]:=MemoryGetProcAddress(dllmodule%i%,A_LoopField)
  124. DynaCall(object.ahkdll,"Str","","Str","","Str","","CDecl UInt")
  125. DynaCall(object.ahktextdll,"Str","","Str","","Str","","CDecl UInt")
  126. DynaCall(object.ahkReady,"Cdecl Int")
  127. DynaCall(object.ahkReload,"Cdecl Int")
  128. DynaCall(object.ahkTerminate,"Int",0,"Cdecl Int")
  129. DynaCall(object.addFile,"Str","","uchar",0,"uchar",0,"CDecl UInt")
  130. DynaCall(object.addScript,"Str","","Int",0,"Int",0,"CDecl UInt")
  131. DynaCall(object.ahkassign,"Str","","Str","")
  132. DynaCall(object.ahkExecuteLine,"UInt",0,"UInt",0,"UInt",0,"CDecl UInt")
  133. DynaCall(object.ahkFindFunc,"Str","","CDecl UInt")
  134. DynaCall(object.ahkFunction,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str")
  135. DynaCall(object.ahkPostFunction,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str")
  136. DynaCall(object.ahkgetvar,"Str","","UInt",0,"CDecl Str")
  137. DynaCall(object.ahkLabel,"Str","","CDecl UInt")
  138. DynaCall(object.ahkPause,"Str","")
  139. obj%i%:=object
  140. dll%i%:=dll
  141. return obj%i%
  142. }