Post by jeeswg » 07 Feb 2019, 11:37
- So far when I have written machine code functions for AHK, they haven't called other functions.
- But here are some fundamental examples, if I did ever want to call other functions from a function.
- The code examples demonstrate: checking the bitness of the AHK script, using an HWND parameter, and calling a machine code function from another machine code function.
Code: Select all
;[MCode function]
;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC
q:: ;test get bitness (for process that runs machine code)
pFunc := MCode("2,x86:uCAAAADD,x64:uEAAAADD")
MsgBox, % DllCall(pFunc, "Cdecl")
return
/*
int ThisProcessGetBitness()
{
#ifdef _WIN64
return 64;
#else
return 32;
#endif
}
*/
;==================================================
w:: ;test call MessageBox
pFuncMessageBox := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"user32", Ptr), AStr,"MessageBoxW", Ptr)
DllCall(pFuncMessageBox, Ptr,0, Str,"prompt", Str,"title", UInt,0)
pFunc := MCode("2,x86:g+wci0QkLIlEJAyLRCQoiUQkCItEJCSJRCQEi0QkIIkEJP9UJDCD7BCDxBzD,x64:SP9kJCg=")
Loop, 3
DllCall(pFunc, Ptr,0, Str,"prompt " A_Index, Str,"title", UInt,0, Ptr,pFuncMessageBox, "Cdecl")
return
/*
void CallMessageBoxW(void* hWnd, short* lpText, short* lpCaption, unsigned int uType, int _stdcall(*MyFunction)(void*,short*,short*,unsigned int))
{
//MessageBoxW(0, L"prompt", L"title", 0);
(*MyFunction)(hWnd, lpText, lpCaption, uType);
return;
}
*/
;==================================================
e:: ;test call towupper
vNum := 97 ;a
pFuncToWUpper := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"msvcrt", Ptr), AStr,"towupper", Ptr)
MsgBox, % DllCall(pFuncToWUpper, Int,vNum, "Cdecl") ;65 ;A
pFunc := MCode("2,x86:/2QkCA==,x64:SP/i")
MsgBox, % DllCall(pFunc, Int,vNum, Ptr,pFuncToWUpper, "Cdecl")
return
/*
void CallToWUpper(int wc, int _cdecl(*MyFunction)(int))
{
//ToWUpper(97);
(*MyFunction)(wc);
return;
}
*/
;==================================================
r:: ;test call custom add function
pFuncAdd := MCode("2,x86:i0QkCANEJATD,x64:jQQRww==")
pFunc := MCode("2,x86:/2QkDA==,x64:Sf/g")
vNum1 := 1, vNum2 := 2
MsgBox, % DllCall(pFuncAdd, Int,vNum1, Int,vNum2, "Cdecl")
MsgBox, % DllCall(pFunc, Int,vNum1, Int,vNum2, Ptr,pFuncAdd, "Cdecl")
return
/*
int Add(int num1, int num2)
{
return num1 + num2;
}
void CallAdd(int num1, int num2, int _cdecl(*MyFunction)(int,int))
{
(*MyFunction)(num1, num2);
return;
}
*/
;==================================================
- Examples of machine code that use pointers to other functions:
buf/byteWrite.c at master · HelgeffegleH/buf · GitHub
https://github.com/HelgeffegleH/buf/blob/master/c%20source/byteWrite.c
'To call our function in MCode we have to give it the pointer to our function.'
MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
https://www.autohotkey.com/boards/viewtopic.php?f=7&t=32
- [EDIT:] A further example:
[compare strings case insensitive, find first difference]
JavaScript vs AutoHotKey (Simple Speed Test) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=61602&p=262545#p262545
- @nnnik: Interesting.
- So far when I have written machine code functions for AHK, they haven't called other functions.
- But here are some fundamental examples, if I did ever want to call other functions from a function.
- The code examples demonstrate: checking the bitness of the AHK script, using an HWND parameter, and calling a machine code function from another machine code function.
[code]
;[MCode function]
;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC
q:: ;test get bitness (for process that runs machine code)
pFunc := MCode("2,x86:uCAAAADD,x64:uEAAAADD")
MsgBox, % DllCall(pFunc, "Cdecl")
return
/*
int ThisProcessGetBitness()
{
#ifdef _WIN64
return 64;
#else
return 32;
#endif
}
*/
;==================================================
w:: ;test call MessageBox
pFuncMessageBox := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"user32", Ptr), AStr,"MessageBoxW", Ptr)
DllCall(pFuncMessageBox, Ptr,0, Str,"prompt", Str,"title", UInt,0)
pFunc := MCode("2,x86:g+wci0QkLIlEJAyLRCQoiUQkCItEJCSJRCQEi0QkIIkEJP9UJDCD7BCDxBzD,x64:SP9kJCg=")
Loop, 3
DllCall(pFunc, Ptr,0, Str,"prompt " A_Index, Str,"title", UInt,0, Ptr,pFuncMessageBox, "Cdecl")
return
/*
void CallMessageBoxW(void* hWnd, short* lpText, short* lpCaption, unsigned int uType, int _stdcall(*MyFunction)(void*,short*,short*,unsigned int))
{
//MessageBoxW(0, L"prompt", L"title", 0);
(*MyFunction)(hWnd, lpText, lpCaption, uType);
return;
}
*/
;==================================================
e:: ;test call towupper
vNum := 97 ;a
pFuncToWUpper := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"msvcrt", Ptr), AStr,"towupper", Ptr)
MsgBox, % DllCall(pFuncToWUpper, Int,vNum, "Cdecl") ;65 ;A
pFunc := MCode("2,x86:/2QkCA==,x64:SP/i")
MsgBox, % DllCall(pFunc, Int,vNum, Ptr,pFuncToWUpper, "Cdecl")
return
/*
void CallToWUpper(int wc, int _cdecl(*MyFunction)(int))
{
//ToWUpper(97);
(*MyFunction)(wc);
return;
}
*/
;==================================================
r:: ;test call custom add function
pFuncAdd := MCode("2,x86:i0QkCANEJATD,x64:jQQRww==")
pFunc := MCode("2,x86:/2QkDA==,x64:Sf/g")
vNum1 := 1, vNum2 := 2
MsgBox, % DllCall(pFuncAdd, Int,vNum1, Int,vNum2, "Cdecl")
MsgBox, % DllCall(pFunc, Int,vNum1, Int,vNum2, Ptr,pFuncAdd, "Cdecl")
return
/*
int Add(int num1, int num2)
{
return num1 + num2;
}
void CallAdd(int num1, int num2, int _cdecl(*MyFunction)(int,int))
{
(*MyFunction)(num1, num2);
return;
}
*/
;==================================================
[/code]
- Examples of machine code that use pointers to other functions:
buf/byteWrite.c at master · HelgeffegleH/buf · GitHub
[url]https://github.com/HelgeffegleH/buf/blob/master/c%20source/byteWrite.c[/url]
'To call our function in MCode we have to give it the pointer to our function.'
MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
[url]https://www.autohotkey.com/boards/viewtopic.php?f=7&t=32[/url]
- [EDIT:] A further example:
[compare strings case insensitive, find first difference]
JavaScript vs AutoHotKey (Simple Speed Test) - Page 3 - AutoHotkey Community
[url]https://autohotkey.com/boards/viewtopic.php?f=23&t=61602&p=262545#p262545[/url]
- @nnnik: Interesting.