AHK via DllCall: AutoHotkey functions as custom functions

Helpful script writing tricks and HowTo's
User avatar
jeeswg
Posts: 2647
Joined: 19 Dec 2016, 01:58
Location: UK

AHK via DllCall: AutoHotkey functions as custom functions

04 Oct 2017, 15:24

This is a collection of code that recreates/improves commands/functions/built-in variables. In general this is done via the Winapi (via DllCall), but sometimes via lower-level AutoHotkey commands and functions.

People are welcome to submit links or code for functions etc that aren't listed here. (Or that are already listed here.)

Every so often I need to alter or recreate a command/function/built-in variable, or understand how it works for debugging purposes. Such knowledge is also useful for creating programs in other programming languages.

As an alternative to WinGetPos I might try GetWindowRect, but WinGetPos uses GetWindowRect. I can find this out by checking the AutoHotkey source code, but this isn't always easy, even if you're an expert. This is why in the past I have suggested a project to give a brief summary (maybe one or two lines) for each function, outlining how it works, and also, possibly, for anyone who wants to contribute, to recreate all of AutoHotkey's, functions via DllCall. For almost all the functions this is possible. Translating the C++ source code to AutoHotkey code, or achieving the same goal via alternative means. Much of this work has already been done and is presented here.

==================================================

[(Unicode functions for AutoHotkey Basic/ANSI)]
[StrReplace (via StringReplace)]
[ControlGetText/ControlSetText]
[WinGetTitle]
[FileRead/FileAppend]
Unicode functions for AutoHotkey Basic / AutoHotkey x32 ANSI - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=32487&p=173620#p173620

[(Unicode functions for AutoHotkey Basic/ANSI)]
[StrGet/StrPut]
StrPut / StrGet - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/55299-strput-strget/

[FormatTime]
WinAPI - GetXxxFormat Functions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=10082&p=170047#p170047

[Gui]
GUIs via DllCall: control zoo - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=32106
GUIs via DllCall: MsgBox - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=30688
[tutorial] Creating windows without GUI commands - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/21124-tutorial-creating-windows-without-gui-commands/
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893

[WinGet-List/WinGet-ControlList/WinGet-ControlListHwnd]
GUIs via DllCall: list windows/child windows (controls) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=36405
AutoHotkey-Util/WinEnum.ahk at master · cocobelgica/AutoHotkey-Util · GitHub
https://github.com/cocobelgica/AutoHotkey-Util/blob/master/WinEnum.ahk

[RegRead/RegWrite]
RegRead64() and RegWrite64() - no redirect to Wow6432Node - Scripts - AutoHotkey Community
https://autohotkey.com/board/topic/36290-regread64-and-regwrite64-no-redirect-to-wow6432node/
registry: list keys/values via DllCall - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38191

[Loop-Files (long filenames)]
259-char path limit workarounds - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26170

[NumGet/NumPut (via * and DllCall: RtlFillMemory)]
Choose naming and syntax for built-in Extract/InsertInteger - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/18072-choose-naming-and-syntax-for-built-in-extractinsertinteger/

[MsgBox]
DllCall
https://autohotkey.com/docs/commands/DllCall.htm#Examples

[InputBox]
[unresolved]

[Mod (via arithmetic)]
Mod function returning wrong value? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=14&t=29762

[SetTimer]
SetTimerF() - SetTimer for Functions - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/59492-settimerf-settimer-for-functions/
MessageFunc() [Obsolete] - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/59245-messagefunc-obsolete/
[solved] SetTimer to call a Method - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=3447

[Format (via DllCall: sprintf/wsprintf)]
SPRINTF - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/16868-sprintf/
UInt64 <--> Int64: Using large unsigned hex/decimals - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/16888-uint64-int64-using-large-unsigned-hexdecimals/

[WinGetPos (via DllCall: GetWindowRect)]
DllCall
https://autohotkey.com/docs/commands/DllCall.htm#Examples

[WinGet-ProcessPath]
Retrieve the path of a running process - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/10525-retrieve-the-path-of-a-running-process/
Getting a full executable path from a running process - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/41197-getting-a-full-executable-path-from-a-running-process/
Getting path of active program - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/72912-getting-path-of-active-program/
process list+file names+command lines - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/8228-process-listfile-namescommand-lines/

[FileGetShortcut (get hotkey)]
COM QueryInterface - Page 3 - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/74471-com-queryinterface/page-3

[FileGetTime/FileSetTime (milliseconds)]
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=36789&p=170076#p170076
TouchIT() : Sets Compilation time and Checksum for AutoHotkey executables - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=36889
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

[IniRead/IniWrite (UTF-8 workaround)]
UTF-8 ini files - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38511

[IniRead/IniWrite]
GetPrivateProfileString function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v=vs.85).aspx
WritePrivateProfileString function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms725501(v=vs.85).aspx

[ImageSearch (stored data instead of the screen)]
AutoHotkey/Functions/Gdip_ImageSearch at master · MasterFocus/AutoHotkey · GitHub
https://github.com/MasterFocus/AutoHotkey/tree/master/Functions/Gdip_ImageSearch

[ListLines/ListVars/ListHotkeys/KeyHistory (get data without showing main window)]
DebugVars - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=24984
ScriptInfo(): Get ListLines/ListVars/ListHotkeys/KeyHistory text - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=9656

[Reload/Edit/Pause/Suspend/ExitApp/ListLines/ListVars/ListHotkeys/KeyHistory]
[A_IsPaused/A_IsSuspended]
list of AutoHotkey WM_COMMAND IDs (e.g. Reload/Edit/Suspend/ListVars on another script) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=27824

[Click/MouseClick]
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

[Run (alternatives)]
Programs started with the AHK Run command are slow - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=31675&p=147812#p147812
Run/RunWait and set process priority - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=31054&p=145082#p145082
CreateProcess function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
ShellExecute function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
ShellExecuteEx function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx

[ControlSend/Send (ControlSend alternatives)]
PostMessage plz help - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30925&p=144465#p144465
AutoHotkey source code: Send/ControlSend (modifier keys) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26615&p=128898#p128898
SendInput specify hWnd / improved ControlSend - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=37120&p=170825#p170825
How to mouse wheeldown less than 1 unit - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34065&p=157799#p157799

[SoundBeep]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;sound beep
DllCall("kernel32\Beep", UInt,523, UInt,150) ;C5 523Hz
return


[SoundPlay]
create wave data and play it live? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34066&p=161897#p161897
Seamless Looping of Audio - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=680

[SoundGet/SoundSet]
Vista Audio Control Functions - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/21984-vista-audio-control-functions/

[SplitPath (via RegExReplace)]
[Trim/LTrim/RTrim (via RegExReplace)]
jeeswg's RegEx tutorial (RegExMatch, RegExReplace) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28031

[StatusBarGetText (via Acc) (handle controls that do not have ClassNN 'msctls_statusbar321')]
jeeswg's Notepad tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31764

[ToolTip (set fonts/colours)]
ToolTips: set border colour (custom font/colour ToolTips) (borders around windows/controls) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=36959
ToolTipEx - custom fonts and colors in ToolTips - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=4350
ToolTipFont / ToolTipColor - options for the ToolTip command - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=4777

[(AHK v2 commands for AHK v1)]
[DateAdd/DateDiff/DirExist (for AHK v1)]
commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

[UrlDownloadToFile (faster alternative)][Download (AHK v2)]
download urls to vars, partially/fullly, via WinHttpRequest - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26528
Super simple download with progress bar. - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/101007-super-simple-download-with-progress-bar/
[solved] UrlDownloadToFile slows down GUI response - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=9338
Simple Download (Bin, ToString und ToFile) - Gebrauchsfertige Skripte & Funktionen - AutoHotkey Community
https://autohotkey.com/board/topic/89198-simple-download-bin-tostring-und-tofile/
UrlDownloadToVar [AHK 1.1] - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3291

[FileSelectFile][FileSelect (AHK v2)]
SelectFolderEx() - new dialog on Win Vista+ - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=18939
[AHK & AHK_L] Forms Framework 0.8 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/49214-ahk-ahk-l-forms-framework-08/page-1

[ProcessSetPriority]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[Process-Close]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;close process
;warning this would close Notepad without prompting
WinGet, vPID, PID, ahk_class Notepad
;PROCESS_ALL_ACCESS := 0x1FFFFF
;PROCESS_ALL_ACCESS := 0x1F0FFF ;pre-Vista
hProc := DllCall("kernel32\OpenProcess", UInt,0x1FFFFF, Int,0, UInt,vPID, Ptr)
DllCall("kernel32\TerminateProcess", Ptr,hProc, UInt,0)
DllCall("kernel32\CloseHandle", Ptr,hProc)
return


[Type]
type(v) for v1.1 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=2306

[WinGet-Style/WinGet-ExStyle][WinSet-Style/WinSet-ExStyle]
[ControlGet-Style/ControlGet-ExStyle][ControlSet-Style/ControlSet-ExStyle]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[WinGet-PID]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;window get PID (process ID)
WinGet, hWnd, ID, A
DllCall("user32\GetWindowThreadProcessId", Ptr,hWnd, UIntP,vPID, UInt)
MsgBox, % vPID
return


[WinGetClass]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;window get class
WinGet, hWnd, ID, A
VarSetCapacity(vWinClass, 260*(A_IsUnicode?2:1))
DllCall("user32\GetClassName", Ptr,hWnd, Str,vWinClass, Int,260)
MsgBox, % vWinClass
return


[GetKeyName]
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486

[ControlGetPos/ControlMove (client coordinates)]
jeeswg's Notepad tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31764

[Transform-HTML]
text to html, html to text (recreate AHK's Transform HTML subcommand) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38424

[Sleep]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;sleep 3 seconds
MsgBox
DllCall("kernel32\Sleep", UInt,3000)
MsgBox
return


[PostMessage/SendMessage]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[Ord/Chr]
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486

[A_OSVersion]
GetVersion function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx
GetVersionEx function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).aspx

[A_Args][%0% %1% %2%][command line parameters]
Args() - Returns command line parameters as array - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=4357
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=27069&p=134073#p134073

[A_ScreenDPI]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get screen DPI
;LOGPIXELSX := 88
hDC := DllCall("user32\GetDC", Ptr,0, Ptr)
vScreenDPI := DllCall("gdi32\GetDeviceCaps", Ptr,hDC, Int,88)
DllCall("user32\ReleaseDC", Ptr,0, Ptr,hDC)
MsgBox, % vScreenDPI
return


[A_Desktop/A_UserName/ComSpec etc]
[A_ComSpec/A_Temp/A_Language/A_ComputerName/A_UserName/A_WinDir]
[A_AppData/A_Desktop/A_MyDocuments/A_ProgramFiles/A_Programs/A_StartMenu/A_Startup]
[ComSpec/ProgramFiles]
[EnvGet]
jeeswg's Explorer tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31755&p=148121#p148121
list Recent Items (My Recent Documents) (Start Menu) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31386
SHGetFolderPath function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx

[A_LastError]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get last error
vLastError := DllCall("kernel32\GetLastError", UInt)
return


[A_Now/A_MSec]
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

[A_CaretX/A_CaretY]
convert coordinates between Client/Screen/Window modes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38472

[A_ScreenWidth/A_ScreenHeight]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get screen width/height
;SM_CXSCREEN := 0 ;SM_CYSCREEN := 1
vScreenWidth := DllCall("user32\GetSystemMetrics", Int,0)
vScreenHeight := DllCall("user32\GetSystemMetrics", Int,1)
MsgBox, % vScreenWidth " " vScreenHeight
return


[A_TickCount (more accurate alternative)]
[Speedtest] Bitwise Hacks - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=37217

[A_TickCount]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get tick count
vTickCount := DllCall("kernel32\GetTickCount", UInt)
return


[ClipboardAll]
clipboard: remove individual clipboard formats + save to clp file - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=39522

[Clipboard (built-in variable)]
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893&p=144342#p144342
Unicode functions for AutoHotkey Basic / AutoHotkey x32 ANSI - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=32487&p=173620#p173620

[A_ScriptHwnd]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get script hWnd
DetectHiddenWindows, On
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
;Process, Exist
;vScriptPID := ErrorLevel ;this also works
WinGet, hWnd, ID, % "ahk_pid " vScriptPID
WinGetTitle, vWinTitle, % "ahk_id " hWnd
MsgBox, % hWnd "`r`n" vWinTitle
return


[A_Cursor]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[A_WorkingDir/A_InitialWorkingDir]
[SetWorkingDir]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[A_WorkingDir (for external processes)]
external process: get working directory (current directory) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=38526
[SOLVED]get other process's working dir - Page 2 - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/85304-solvedget-other-processs-working-dir/page-2#entry544320

[A_IsUnicode]

Code: [Select all] [Download] GeSHi © Codebox Plus

;note: this is not necessarily how AHK retrieves A_IsUnicode
q:: ;check if a window is Unicode/ANSI
hWnd := A_ScriptHwnd
MsgBox, % DllCall("user32\IsWindowUnicode", Ptr,hWnd)
return


[A_Is64bitOS/A_PtrSize]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[A_IsAdmin (for AHK and for external processes)]
Check if any app is running as administrator - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26700

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests