PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/AhkDllThread.ahk

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