Ciantic / VirtualDesktopAccessor

DLL for accessing Windows 11/10 Virtual Desktop features from e.g. AutoHotkey
MIT License
772 stars 93 forks source link

Two modifications to make your example simpler #58

Closed ZhuangQu closed 1 year ago

ZhuangQu commented 1 year ago

In example, you get ahkWindowHwnd by

ahkWindowHwnd := WinExist("ahk_pid " . DllCall("GetCurrentProcessId", "Uint"))
ahkWindowHwnd += 0x1000 << 32

which is not necessary. AutoHotkey provides the built-in variable A_ScriptHwnd.

ZhuangQu commented 1 year ago

In example, you call GetCurrentDesktopNumber by

VDA_PATH := "C:\Source\Rust\winvd\target\release\VirtualDesktopAccessor.dll"
hVirtualDesktopAccessor := DllCall("LoadLibrary", "Str", VDA_PATH, "Ptr")
GetCurrentDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetCurrentDesktopNumber", "Ptr")

current := DllCall(GetCurrentDesktopNumberProc, "UInt")

If you define a function

VDA(func, argv*) {
    static path := "C:\Source\Rust\winvd\target\release\VirtualDesktopAccessor.dll"
    static dll := DllCall("LoadLibrary", "Str", path, "Ptr")
    proc := DllCall("GetProcAddress", "Ptr", dll, "AStr", func, "Ptr")
    return DllCall(proc, argv*)
}

then you can call GetCurrentDesktopNumber only by

current := VDA("GetCurrentDesktopNumber", "UInt")

Other calls like these:

VDA("GoToDesktopNumber", "UInt", index)
VDA("RegisterPostMessageHook", "Int", A_ScriptHwnd, "Int", 0xFFFF)
VDA("GetDesktopName", "UInt", index, "Ptr", buffer)

which are more concise without any global variables.

Ciantic commented 1 year ago

I switched to using A_ScriptHwnd.

I don't plan to change the AHK example a lot, I like your function idea but I want to keep this simple.