I've discovered something cool!
It is possible to "call" APIs (or really any function) from machine code:
Here is a quick example:
Code:
_MessageBoxA := DllCall("GetProcAddress", "uint", DllCall("GetModuleHandle", "str", "user32"), "str", "MessageBoxA") ;Get the address of MessageBoxA
MCode(MessageBoxA, "8B4424148B4C24108B54240C508B44240C515250FF542414C3") ;Using MCodeGen with VS2010 installed (http://www.autohotkey.com/forum/viewtopic.php?t=59593)
DllCall(&MessageBoxA, "uint", _MessageBoxA, "uint", 0, "str", "This is my text.", "str", "This is my caption.", "uint", 0, "cdecl")
The trick is to call the function by address inside the machine code. This is the C code compiled:
Code:
#include <windows.h>
int CallMessageBox(int (__stdcall *MessageBoxFunc)(HWND, LPCTSTR, LPCTSTR, UINT), HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) {
return MessageBoxFunc(hWnd, lpText, lpCaption, uType);
}
MessageBox is just an example, but you could use this technique for any function. Obviously, you'll have to compile the appropriate C code for functions with different signatures.
After running a few benchmarks, it seems to be faster than calling the target function directly with DllCall. Although I couldn't find a good CPU intensive function to test with (suggestions?). Regardless, we still have to use DllCall in the end, incurring whatever overhead comes with it.