Noob to DllCalls Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
adrian88888888
Posts: 84
Joined: 13 Mar 2020, 22:51

Noob to DllCalls

Post by adrian88888888 » 20 Feb 2021, 18:20

When you press alt+tab you get all the windows you have, the dlll that i'm trying to call is supposed to give me the list of those windows in that order

I'm doing a call to a .dll that is not made by microsoft, his only documentation is this line:

Code: Select all

* uint ViewGetByLastActivationOrder(HWND *windows, UINT count, BOOL onlySwitcherWindows, BOOL onlyCurrentDesktop) // Get windows in alt tab order

I know nobody is supposed to know my question but, if someone has experience with C++ or C or the microsoft documentation or with DllCalls can try to guess?:

1-what means "*windows"? and what means the asterisk?
2-I have to give it a hwnd? hwnd of what?
3-What is that that count value?
4-what is supposed to give to me? a list? a uint?

I don't care about the BOOL's because I put a 1 or a 0 and see what happens

this is the rest of the documentation if this helps:
https://github.com/Ciantic/VirtualDesktopAccessor#all-functions-exported-by-dll

Thanks
adrian88888888
Posts: 84
Joined: 13 Mar 2020, 22:51

Re: Noob to DllCalls

Post by adrian88888888 » 21 Feb 2021, 14:19

OMG, why i try to reinvent the wheel all the time...thanks a lot for the time saved!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Noob to DllCalls

Post by swagfag » 21 Feb 2021, 21:36

if u got documentation, u read the documentation. if the documentation sucks or u aing got none(which u dont), u read the source. if cant read the source, aint got it(which luckily isnt the case) or cant reverse it, then ure SOL
  • windows is a pointer to a caller(read: you)-allocated array of HWNDs(a type of pointer; a Windows-defined data type, HWND > HANDLE > PVOID > void *)
    the function will fill this array for u and return the number of elements it managed to insert(so u know later on how deep u can index into the array)
    ViewGetByLastActivationOrder wrote:

    Code: Select all

    	UINT i = 0;
    	for (auto entry : unsorted) {
    		windows[i] = entry.hwnd;
    		i++;
    	}
    	
    	return i;
  • count depends on the size of the array u just allocated - the number of HWNDs u can store in it. better make sure it doesnt exceed the size (number of elements X their memory footprint, + accounting for alignment) of the allocated array, otherwise the function might end up writing into memory it has no ownership of
    if u didnt allocate enough space(ie there are more windows on screen, and thus more hwnds to store, than the size ur array allows for), the function call fails
    ViewGetByLastActivationOrder wrote:

    Code: Select all

    	arr->GetCount(&countViews);
    	if (countViews > count) {
    		arr->Release();
    		return 0;
    	}
  • onlySwitcherWindows is some kind of a flag
  • onlyCurrentDesktop is some kind of another flag
in conclusion:

Code: Select all

maxHwnds := 16 ; initially, expect up to this many windows to be on screen

Loop
{
	VarSetCapacity(windows, maxHwnds * A_PtrSize) ; a HWND is ptr-sized (4 or 8 bytes, depending on architecture)
	countHwnds := DllCall("VirtualDesktopAccessor.dll\ViewGetByLastActivationOrder"
		, "Ptr", &windows
		, "UInt", maxHwnds
		, "Int", onlySwitcherWindows ; BOOLs are typedeffed to Ints
		, "Int", onlyCurrentDesktop
		, "UInt")

	if countHwnds ; if the call succeeded, exit the loop
		break

	; otherwise, either we didnt allocated enough space or there
	; was an internal problem getting a reference to the windows
	if (A_Index = 3) ; after 3 failures...
		throw "internal error" ; ...assume it was due to the internal error

	maxHwnds *= 4 ; allocate more space and retry
}

Loop % countHwnds
{
	i := A_Index - 1 ; zero based index, because C/C++ arrays are zero based
	hwnd := NumGet(&windows, i * A_PtrSize, "Ptr")

	; do something with the hwnd
}
Post Reply

Return to “Ask for Help (v1)”