- The answer is AutoHotkey.
- Why?
- C++ makes it easier than AutoHotkey by handling the types for you when calling functions, and handling the struct members like an associative array when handling structs.
- [EDIT:] So C++ doesn't really help with learning DllCall, because in C++ the bits that make DllCall hard in AutoHotkey are done for you. (Although C++ introduces new difficulties e.g. passing string variables into a function call.)
- [EDIT:] Learning about DllCall in AutoHotkey comes down to this: if you look up a function e.g. MessageBox or GetWindowRect can you extract the info from MSDN pages, and work out what to put into DllCall. And if there are any structs, do you know how to work out the byte offsets for each struct member and how to use VarSetCapacity/NumGet/NumPut.
Code: Select all
;Winapi function example
;AutoHotkey
DllCall("user32\MessageBox", Ptr,0, Str,"prompt", Str,"title", UInt,0)
//C++
MessageBoxA(0, "prompt", "title", 0); //ANSI
MessageBoxW(0, L"prompt", L"title", 0); //Unicode
MessageBox(0, _T("prompt"), _T("title"), 0); //Unicode or ANSI
Code: Select all
;Winapi struct example
;AutoHotkey
;WinGet, hWnd, ID, ahk_class Notepad
hWnd := DllCall("user32\FindWindow", Str,"Notepad", Ptr,0, Ptr)
VarSetCapacity(RECT, 16, 0)
DllCall("user32\GetWindowRect", Ptr,hWnd, Ptr,&RECT)
vWinX := NumGet(&RECT, 0, "Int")
vWinY := NumGet(&RECT, 4, "Int")
vWinW := NumGet(&RECT, 8, "Int") - vWinX
vWinH := NumGet(&RECT, 12, "Int") - vWinY
vWinPos := Format("x{} y{} w{} h{}", vWinX, vWinY, vWinW, vWinH)
DllCall("user32\MessageBox", Ptr,0, Str,vWinPos, Str,"title", UInt,0)
//C++
HWND hWnd = FindWindow(_T("Notepad"), NULL);
RECT RECT;
GetWindowRect(hWnd, &RECT);
std::wstring vWinX = std::to_wstring(RECT.left);
std::wstring vWinY = std::to_wstring(RECT.top);
std::wstring vWinW = std::to_wstring(RECT.right - RECT.left);
std::wstring vWinH = std::to_wstring(RECT.bottom - RECT.top);
std::wstring vWinPos = L"x" + vWinX + L" y" + vWinY + L" w" + vWinW + L" h" + vWinH;
MessageBoxW(0, vWinPos.c_str(), L"title", 0);
//C++ alternative
HWND hWnd = FindWindow(_T("Notepad"), NULL);
RECT RECT;
GetWindowRect(hWnd, &RECT);
int vWinX = RECT.left;
int vWinY = RECT.top;
int vWinW = RECT.right - RECT.left;
int vWinH = RECT.bottom - RECT.top;
wchar_t vWinPos[1000];
swprintf(vWinPos, 1000, L"x%d y%d w%d h%d", vWinX, vWinY, vWinW, vWinH);
MessageBoxW(0, vWinPos, L"title", 0);
- Java makes it harder because for Java handling the Winapi isn't made easy, you require something like the JNA/the JNI.
- I would still recommend learning more about other programming languages however.
- When I want to use the Winapi I look at the MSDN page for a function.
- I need to figure out the types for each function parameter. The AHK equivalent such as Int or Ptr.
- Some will be familiar, others I can look up on the Internet, on .h files on my hard drive (created when I install Visual Studio), and by code in Visual Studio to test what size a parameter is, and whether it's signed/unsigned. I can also check examples on the forum, but these can often be wrong (e.g. Int or UInt, when it should be Ptr). There's also this:
WinApi
https://hotkeyit.github.io/v2/docs/commands/WinApi.htm
- E.g. Visual Studio Express for Windows Desktop.
- Some Winapi functions require using a struct, a struct is n bytes of binary data, e.g. RECT is 16 bytes. I similarly look up size and signed/unsigned information about the struct parameters. And I use the sizes and knowledge of the rules of struct alignment/padding to work out the necessary offsets (the byte number) at which each struct member appears. The rules are: an n-byte parameter starts at an n-byte offset. If a struct has an n-byte parameter, its overall size must be a multiple of n bytes. When doing the calculations, you must consider that some parameters are actually composed of smaller parameters, e.g. a RECT which is 16 bytes, is made up of 4 4-byte parameters.
- 99% of the time I can figure things out fine, every so often, something weird will come up, and I might ask for help or do some trial-and-error testing.
- I'll be doing a tutorial on DllCall/structs etc but it might be some months.