AutoHotkey via DllCall: AutoHotkey functions as custom functions

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

AutoHotkey 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. By 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.

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

[(A_ComSpec/A_Desktop/A_UserName etc)]
[A_AppData/A_Desktop/A_MyDocuments/A_ProgramFiles/A_Programs/A_StartMenu/A_Startup]
[A_AppDataCommon/A_DesktopCommon/A_ProgramsCommon/A_StartMenuCommon/A_StartupCommon]
[A_ComSpec/A_Temp/A_Language/A_ComputerName/A_UserName/A_WinDir]
[AHK v1: ComSpec/ProgramFiles]
[EnvGet]
jeeswg's Explorer tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 21#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/librar ... s.85).aspx

[A_Args]
[AHK v1: %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 ... 73#p134073

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

[A_Cursor]

Code: Select all

q:: ;cursor get type
vSize := (A_PtrSize=8)?24:20
VarSetCapacity(CURSORINFO, vSize, 0)
NumPut(vSize, &CURSORINFO, 0, "UInt") ;cbSize
DllCall("user32\GetCursorInfo", Ptr,&CURSORINFO)
hCursor := NumGet(&CURSORINFO, 8, "Ptr") ;hCursor
vCursor := "Unknown"
if !vInit
{
	oArray2 := Object(StrSplit("AppStarting,32650;Arrow,32512;Cross,32515;Help,32651;IBeam,32513;Icon,32641;No,32648;Size,32640;SizeAll,32646;SizeNESW,32643;SizeNS,32645;SizeNWSE,32642;SizeWE,32644;UpArrow,32516;Wait,32514", [",",";"])*)
	oArray := {}
	for vKey, vValue in oArray2
	{
		hCursor2 := DllCall("user32\LoadCursor", Ptr,0, Ptr,vValue, Ptr)
		oArray[hCursor2] := vKey
	}
	vInit := 1
}
if oArray.HasKey(hCursor)
	vCursor := oArray[hCursor]
MsgBox, % hCursor "`r`n" vCursor "`r`n" A_Cursor
return
[SysGetIPAddresses]
[AHK v1: A_IPAddress1/A_IPAddress2/A_IPAddress3/A_IPAddress4]
recreate A_IPAddressXXX variables as a function - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=57112

[A_Is64bitOS/A_PtrSize]

Code: Select all

;A_Is64bitOS
;note: this is not necessarily how AHK retrieves A_Is64bitOS
;AHK checks if AHK is 64-bit (if it is then the system is 64-bit)
;otherwise, AHK is 32-bit, if IsWow64Process returns true then the system is 64-bit, otherwise it is 32-bit
;note: GetNativeSystemInfo relates to the system's bitness, GetSystemInfo relates to the process's bitness
q:: ;some methods to determine the bitness of OS
VarSetCapacity(SYSTEM_INFO, A_PtrSize=8?48:36, 0)
DllCall("kernel32\GetNativeSystemInfo", Ptr,&SYSTEM_INFO)
vProcessorArchitecture := NumGet(&SYSTEM_INFO, 0, "UShort") ;wProcessorArchitecture
;PROCESSOR_ARCHITECTURE_AMD64 := 9 ;x64 (AMD or Intel)
;PROCESSOR_ARCHITECTURE_INTEL := 0 ;x86
MsgBox, % vProcessorArchitecture

;MAX_PATH := 260
VarSetCapacity(vSysWow64Dir, 260*(A_IsUnicode?2:1), 0)
DllCall("kernel32\GetSystemWow64Directory", Str,vSysWow64Dir, UInt,260, UInt)
;e.g. the dir exists if 64-bit
MsgBox, % vSysWow64Dir
return

;==================================================

;A_PtrSize
;note: this is not necessarily how AHK retrieves A_PtrSize
;note: GetNativeSystemInfo relates to the system's bitness, GetSystemInfo relates to the process's bitness
w:: ;some methods to determine the bitness of a process
VarSetCapacity(SYSTEM_INFO, A_PtrSize=8?48:36, 0)
DllCall("kernel32\GetSystemInfo", Ptr,&SYSTEM_INFO)
vProcessorArchitecture := NumGet(&SYSTEM_INFO, 0, "UShort") ;wProcessorArchitecture
;PROCESSOR_ARCHITECTURE_AMD64 := 9 ;x64 (AMD or Intel)
;PROCESSOR_ARCHITECTURE_INTEL := 0 ;x86
MsgBox, % vProcessorArchitecture

hWnd := A_ScriptHwnd
WinGet, vPID, PID, % "ahk_id " hWnd
;WinGet, vPID, PID, ahk_class Notepad
MsgBox, % JEE_ProcessIs64Bit(vPID)
return

;==================================================

JEE_ProcessIs64Bit(vPID)
{
	if !vPID
		return
	else if !A_Is64bitOS
		return 0
	;PROCESS_QUERY_INFORMATION := 0x400
	hProc := DllCall("kernel32\OpenProcess", UInt,0x400, Int,0, UInt,vPID, Ptr)
	DllCall("kernel32\IsWow64Process", Ptr,hProc, PtrP,vIsWow64Process)
	DllCall("kernel32\CloseHandle", Ptr,hProc)
	return !vIsWow64Process
}

;==================================================
[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

[A_IsPaused/A_IsSuspended]
[Reload/Edit/Pause/Suspend/ExitApp/ListLines/ListVars/ListHotkeys/KeyHistory]
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

[A_IsUnicode]

Code: Select all

;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_LastError]

Code: Select all

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_Now]
[A_DD/A_MDay/A_MM/A_Mon/A_WDay/A_Year/A_YYYY]
[A_Hour/A_Min/A_MSec/A_Sec]
[A_DDD/A_DDDD/A_MMM/A_MMMM]
[A_YDay/A_YWeek]

Code: Select all

q:: ;get date/time now (UTC) (recreate A_Now and other A_ variables)

;list of A_ variables to recreate:
vList := "A_Now"
vList .= ",A_DD,A_MDay,A_MM,A_Mon,A_WDay,A_Year,A_YYYY"
vList .= ",A_Hour,A_Min,A_MSec,A_Sec"
vList .= ",A_DDD,A_DDDD,A_MMM,A_MMMM"
vList .= ",A_YDay,A_YWeek"

VarSetCapacity(SYSTEMTIME, 16, 0)
DllCall("kernel32\GetLocalTime", Ptr,&SYSTEMTIME)
vYear := NumGet(&SYSTEMTIME, 0, "UShort") ;wYear
vMonth := NumGet(&SYSTEMTIME, 2, "UShort") ;wMonth
vWDay := NumGet(&SYSTEMTIME, 4, "UShort") ;wDayOfWeek
vDay := NumGet(&SYSTEMTIME, 6, "UShort") ;wDay
vHour := NumGet(&SYSTEMTIME, 8, "UShort") ;wHour
vMin := NumGet(&SYSTEMTIME, 10, "UShort") ;wMinute
vSec := NumGet(&SYSTEMTIME, 12, "UShort") ;wSecond
vMSec := NumGet(&SYSTEMTIME, 14, "UShort") ;wMilliseconds

vNow := Format("{:04}{:02}{:02}{:02}{:02}{:02}", vYear, vMonth, vDay, vHour, vMin, vSec)

vList2 := "DDD,DDDD,MMM,MMMM"
Loop, Parse, vList2, % ","
{
	vFormat := A_LoopField
	if (vFormat = "DDD") || (vFormat = "DDDD")
		vFormat := Format("{:L}", vFormat)
	;LANG_USER_DEFAULT := 0x400
	vSize := DllCall("GetDateFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,&vFormat, Ptr,0, Int,0)
	VarSetCapacity(vTemp, vSize*(A_IsUnicode?2:1), 0)
	DllCall("GetDateFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,&vFormat, Str,vTemp, Int,vSize)
	v%A_LoopField% := vTemp
}

;leap year: 4Y AND (100N OR 400Y)
vIsLeapYear := !Mod(vYear, 4) && (!!Mod(vYear, 100) || !Mod(vYear, 400))
vYDay := vDay + [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334][vMonth]
if vIsLeapYear && (vMonth > 2)
	vYDay++

vYWeek := JEE_DateGetYWeek(vNow)

;==============================

;see above: vNow

;see above: vYear
vYYYY := vYear
vMM := Format("{:02}", vMonth)
vMon := Format("{:02}", vMonth)
vWDay += 1
vDD := Format("{:02}", vDay)
vMDay := Format("{:02}", vDay)
vHour := Format("{:02}", vHour)
vMin := Format("{:02}", vMin)
vSec := Format("{:02}", vSec)
vMSec := Format("{:03}", vMSec)

;see above: vDDD/vDDDD/vMMM/vMMMM

;see above: vYDay/vYWeek

;==============================

Loop, Parse, vList, % ","
{
	vTemp := SubStr(A_LoopField, 3)
	vOutput .= A_LoopField ": " %A_LoopField% " " v%vTemp% "`r`n"
}
MsgBox, % vOutput
return

;==================================================

;vYDay := FormatTime(vDate, "YDay")
JEE_DateGetYDay(vDate)
{
	static oYDay := [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
	if (StrLen(vDate) < 8)
		vDate := DateAdd(vDate, 0, "S")
	;leap year: 4Y AND (100N OR 400Y)
	vIsLeapYear := !Mod(vYear, 4) && (!!Mod(vYear, 100) || !Mod(vYear, 400))
	return vDay + oYDay[vMonth] + !!(vIsLeapYear && (vMonth > 2))
}

;==================================================

;vWDay := FormatTime(vDate, "WDay")
;get WDay (1-based)
;MsgBox, % JEE_DateGetWDay(A_Now)
JEE_DateGetWDay(vDate)
{
	if (StrLen(vDate) < 14)
		vDate := DateAdd(vDate, 0, "S")
	VarSetCapacity(SYSTEMTIME, 16, 0)
	NumPut(SubStr(vDate, 1, 4), &SYSTEMTIME, 0, "UShort") ;wYear
	NumPut(SubStr(vDate, 5, 2), &SYSTEMTIME, 2, "UShort") ;wMonth
	;NumPut(0, &SYSTEMTIME, 4, "UShort") ;wDayOfWeek
	NumPut(SubStr(vDate, 7, 2), &SYSTEMTIME, 6, "UShort") ;wDay
	NumPut(SubStr(vDate, 9, 2), &SYSTEMTIME, 8, "UShort") ;wHour
	NumPut(SubStr(vDate, 11, 2), &SYSTEMTIME, 10, "UShort") ;wMinute
	NumPut(SubStr(vDate, 13, 2), &SYSTEMTIME, 12, "UShort") ;wSecond
	NumPut(SubStr(vDate, 15, 3), &SYSTEMTIME, 14, "UShort") ;wMilliseconds
	VarSetCapacity(FILETIME, 8, 0)
	DllCall("kernel32\SystemTimeToFileTime", Ptr,&SYSTEMTIME, Ptr,&FILETIME)
	DllCall("kernel32\FileTimeToSystemTime", Ptr,&FILETIME, Ptr,&SYSTEMTIME)
	return 1+NumGet(&SYSTEMTIME, 4, "UShort") ;wDayOfWeek
}

;==================================================

;DateGetYWeek based on AHK source code (util.cpp: GetISOWeekNumber)
;DateGetYWeek based on Linux glibc source code (GPL)
;vYWeek := FormatTime(vDate, "YWeek")
;vYWeek := JEE_DateGetYWeek(vDate)
;JEE_DateGetISOWeek
JEE_DateGetYWeek(vDate)
{
	static ISO_WEEK_START_WDAY := 1 ;Monday
	static ISO_WEEK1_WDAY := 4 ;Thursday
	if (StrLen(vDate) < 8)
		vDate := DateAdd(2000, 0, "S")
	vYear := SubStr(vDate, 1, 4)
	vYDay := FormatTime(vDate, "YDay") - 1 ;0-based
	vWDay := FormatTime(vDate, "WDay") - 1 ;0-based
	vDays := (vYDay - Mod((vYDay - vWDay + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)), 7) + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)
	;vDays := (vYDay - (vYDay - vWDay + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)) mod 7 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)

	;original C++ formula: (yday - (yday - wday + ISO_WEEK1_WDAY + ((366 / 7 + 2) * 7)) % 7
	if (vDays < 0) ;ISO week belongs to previous year
	{
		vYear--
		;leap year: 4Y AND (100N OR 400Y)
		vIsLeapYear := !Mod(vYear, 4) && (!!Mod(vYear, 100) || !Mod(vYear, 400))
		vYDay += (365 + vIsLeapYear)
		vDays := (vYDay - Mod((vYDay - vWDay + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)), 7) + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)
		;vDays := (vYDay - (vYDay - vWDay + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)) mod 7 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)
	}
	else
	{
		;leap year: 4Y AND (100N OR 400Y)
		vIsLeapYear := !Mod(vYear, 4) && (!!Mod(vYear, 100) || !Mod(vYear, 400))
		vYDay -= (365 + vIsLeapYear)
		vTemp := (vYDay - Mod((vYDay - vWDay + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)), 7) + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)
		;vTemp := (vYDay - (vYDay - vWDay + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)) mod 7 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)
		if (0 <= vTemp) ;ISO week belongs to next year
			vYear += 1, vDays := vTemp
	}
	return Format("{:04}{:02}", vYear, (vDays // 7) + 1)
}

;==================================================

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689
DateAdd(DateTime, Time, TimeUnits)
{
	EnvAdd DateTime, %Time%, %TimeUnits%
	return DateTime
}
DateDiff(DateTime1, DateTime2, TimeUnits)
{
	EnvSub DateTime1, %DateTime2%, %TimeUnits%
	return DateTime1
}
FormatTime(YYYYMMDDHH24MISS:="", Format:="")
{
	local OutputVar
	FormatTime OutputVar, %YYYYMMDDHH24MISS%, %Format%
	return OutputVar
}

;==================================================
[A_NowUTC]

Code: Select all

q:: ;get date/time now (UTC) (recreate A_NowUTC)
VarSetCapacity(SYSTEMTIME, 16, 0)
DllCall("kernel32\GetSystemTime", Ptr,&SYSTEMTIME)
vYear := NumGet(&SYSTEMTIME, 0, "UShort") ;wYear
vMonth := NumGet(&SYSTEMTIME, 2, "UShort") ;wMonth
;vWDay := NumGet(&SYSTEMTIME, 4, "UShort") ;wDayOfWeek
vDay := NumGet(&SYSTEMTIME, 6, "UShort") ;wDay
vHour := NumGet(&SYSTEMTIME, 8, "UShort") ;wHour
vMin := NumGet(&SYSTEMTIME, 10, "UShort") ;wMinute
vSec := NumGet(&SYSTEMTIME, 12, "UShort") ;wSecond
;vMSec := NumGet(&SYSTEMTIME, 14, "UShort") ;wMilliseconds

vNowUTC := Format("{:04}{:02}{:02}{:02}{:02}{:02}", vYear, vMonth, vDay, vHour, vMin, vSec)
MsgBox, % vNowUTC "`r`n" A_NowUTC
return
[A_OSVersion]
GetVersion function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
GetVersionEx function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

[A_ScreenDPI]

Code: Select all

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_ScreenWidth/A_ScreenHeight]

Code: Select all

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_ScriptHwnd]

Code: Select all

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_TickCount (more accurate alternative)]
[Speedtest] Bitwise Hacks - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=37217

[A_TickCount]

Code: Select all

q:: ;get tick count
vTickCount := DllCall("kernel32\GetTickCount", UInt)
MsgBox, % vTickCount "`r`n" A_TickCount
return
[A_WorkingDir/A_InitialWorkingDir]
[SetWorkingDir]

Code: Select all

;note: for A_InitialWorkingDir, AHK uses GetCurrentDirectory when it starts up
q:: ;get/set working directory
;MAX_PATH := 260
VarSetCapacity(vWorkingDir, 260*(A_IsUnicode?2:1), 0)
DllCall("kernel32\GetCurrentDirectory", UInt,260, Str,vWorkingDir, UInt)
MsgBox, % vWorkingDir "`r`n" A_WorkingDir

SplitPath, A_AhkPath,, vDir
DllCall("kernel32\SetCurrentDirectory", Str,vDir)
MsgBox, % A_WorkingDir
return
[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/8530 ... ntry544320

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

[ClipboardAll]
clipboard: remove individual clipboard formats + save to clp file - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=39522
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 6 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 68#p191068

[(Clipboard variable) (built-in variable)]
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 42#p144342
Unicode functions for AutoHotkey Basic / AutoHotkey x32 ANSI - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 20#p173620

[AHK v1: ComObjMissing]

Code: Select all

;VT_ERROR := 0xA ;DISP_E_PARAMNOTFOUND := 0x80020004
m := ComObject(0xA, 0x80020004)
[ControlGetStyle/ControlGetExStyle]
[ControlSetStyle/ControlSetExStyle]
[WinGetStyle/WinGetExStyle]
[WinSetStyle/WinSetExStyle]

Code: Select all

q:: ;window - get/set style/extended style
WinGet, hWnd, ID, A
;ControlGet, hWnd, Hwnd,, Edit1, A
;GWL_STYLE := -16 ;GWL_EXSTYLE := -20
vWinStyle := DllCall("user32\GetWindowLong" (A_PtrSize=8?"Ptr":""), Ptr,hWnd, Int,-16, Ptr)
vWinExStyle := DllCall("user32\GetWindowLong" (A_PtrSize=8?"Ptr":""), Ptr,hWnd, Int,-20, Ptr)
MsgBox, % Format("0x{:08X} 0x{:08X}", vWinStyle, vWinExStyle)

vWinStyle |= 1
vWinExStyle |= 1
DllCall("SetWindowLong" (A_PtrSize=8?"Ptr":""), Ptr,hWnd, Int,-16, Ptr,vWinStyle, Ptr)
DllCall("SetWindowLong" (A_PtrSize=8?"Ptr":""), Ptr,hWnd, Int,-20, Ptr,vWinExStyle, Ptr)
return
[ControlGetPos/ControlMove (client coordinates)]
jeeswg's Notepad tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31764
convert coordinates between Client/Screen/Window modes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38472

[ControlSend/Send (ControlSend alternatives)]
PostMessage plz help - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 65#p144465
AutoHotkey source code: Send/ControlSend (modifier keys) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 98#p128898
SendInput specify hWnd / improved ControlSend - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 25#p170825
How to mouse wheeldown less than 1 unit - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 99#p157799

[(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 ... 37&t=29689

[AHK v1: EnvUpdate]

Code: Select all

;note: WM_SETTINGCHANGE is the same as WM_WININICHANGE
;HWND_BROADCAST := 0xFFFF
SendMessage, 0x1A,,,, ahk_id 0xFFFF ;WM_SETTINGCHANGE := 0x1A
[(Unicode functions for AutoHotkey Basic/ANSI)]
[ControlGetText/ControlSetText]
[FileRead/FileAppend (binary data/hex)]
[StrReplace (via StringReplace)]
[WinGetTitle]
Unicode functions for AutoHotkey Basic / AutoHotkey x32 ANSI - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 20#p173620

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

[FileGetTime/FileSetTime (milliseconds)]
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 76#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

[FileSelect]
[AHK v1: FileSelectFile]
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/4921 ... -08/page-1

[Format (via DllCall: _vscwprintf/_vsnwprintf)]
Floating-point issue [being solved?] - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 810#p12810

[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/1688 ... xdecimals/

[FormatTime]
WinAPI - GetXxxFormat Functions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 47#p170047

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

[AHK v1: Gui]
[GuiCreate]
[> create controls]
GUIs via DllCall: control zoo - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=32106
[> create MsgBox]
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/2112 ... -commands/
[> get/set control text]
GUIs via DllCall: text functions (get/set internal/external control text) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40514
[> general]
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893
[> list windows]
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/AutoHotk ... inEnum.ahk
[> further]
slightly-improved dialogs - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=45220
objects: backport AHK v2 Gui/Menu classes to AHK v1 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=43530

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

[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/librar ... s.85).aspx
WritePrivateProfileString function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

[InputBox]
[unresolved]

[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

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

[(Loop-Reg control flow statement)]
[RegRead/RegWrite]
RegRead64() and RegWrite64() - no redirect to Wow6432Node - Scripts - AutoHotkey Community
https://autohotkey.com/board/topic/3629 ... w6432node/
registry: list keys/values via DllCall - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38191

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

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

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

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

[OutputDebug]

Code: Select all

;note: download DebugView, run Dbgview.exe,
;any time an OutputDebug line is executed,
;the text appears in the DebugView window

q:: ;recreate OutputDebug command
DllCall("kernel32\OutputDebugString", Str,"abc`ndef`nghi")
return
[PostMessage/SendMessage]

Code: Select all

q:: ;Notepad - invoke Save As
;PostMessage, 0x111, 4,,, A ;WM_COMMAND := 0x111 ;Save As
;SendMessage, 0x111, 4,,, A ;WM_COMMAND := 0x111 ;Save As
;vRet := ErrorLevel

WinGet, hWnd, ID, A
vRet := DllCall("user32\PostMessage", Ptr,hWnd, UInt,0x111, UPtr,4, Ptr,0) ;WM_COMMAND := 0x111 ;Save As
;vRet := DllCall("user32\SendMessage", Ptr,hWnd, UInt,0x111, UPtr,4, Ptr,0) ;WM_COMMAND := 0x111 ;Save As

SoundBeep
MsgBox, % vRet
return
[ProcessClose]

Code: Select all

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
[(ProcessGetPriority) (not currently an AHK function)]
[ProcessSetPriority]

Code: Select all

q:: ;process - get/set priority
WinGet, vPID, PID, A
vPriority := % JEE_ProcessGetPriority(vPID)
JEE_ProcessSetPriority(vPID, "Low")
MsgBox, % JEE_ProcessGetPriority(vPID)
JEE_ProcessSetPriority(vPID, "High")
MsgBox, % JEE_ProcessGetPriority(vPID)
JEE_ProcessSetPriority(vPID, vPriority)
MsgBox, % JEE_ProcessGetPriority(vPID)
return

;==================================================

JEE_ProcessGetPriority(vPID)
{
	static oArray := {0x40:"Low",0x4000:"BelowNormal",0x20:"Normal",0x8000:"AboveNormal",0x80:"High",0x100:"Realtime"}
	;ABOVE_NORMAL_PRIORITY_CLASS := 0x8000 ;BELOW_NORMAL_PRIORITY_CLASS := 0x4000
	;REALTIME_PRIORITY_CLASS := 0x100 ;HIGH_PRIORITY_CLASS := 0x80
	;IDLE_PRIORITY_CLASS := 0x40 ;NORMAL_PRIORITY_CLASS := 0x20 ;(idle: low)

	local vPriority
	if !RegExMatch(vPID, "^\d*$") ;if not digit
		return "ERROR"
	Process, Exist, % vPID
	vPID := ErrorLevel
	if (vPID = 0)
		return "ERROR"

	;PROCESS_QUERY_INFORMATION := 0x400
	hProc := DllCall("kernel32\OpenProcess", UInt,0x400, Int,0, UInt,vPID, Ptr)
	vPriority := DllCall("kernel32\GetPriorityClass", Ptr,hProc, UInt)
	DllCall("kernel32\CloseHandle", Ptr,hProc)
	return oArray.HasKey(vPriority) ? oArray[vPriority] : vPriority
}

;==================================================

JEE_ProcessSetPriority(vPID, vPriority)
{
	static oArray := {Low:0x40,BelowNormal:0x4000,Normal:0x20,AboveNormal:0x8000,High:0x80,Realtime:0x100}
	;ABOVE_NORMAL_PRIORITY_CLASS := 0x8000 ;BELOW_NORMAL_PRIORITY_CLASS := 0x4000
	;REALTIME_PRIORITY_CLASS := 0x100 ;HIGH_PRIORITY_CLASS := 0x80
	;IDLE_PRIORITY_CLASS := 0x40 ;NORMAL_PRIORITY_CLASS := 0x20 ;(idle: low)

	;PROCESS_SET_INFORMATION := 0x200
	if oArray.HasKey(vPriority)
		vPriority := oArray[vPriority]
	hProc := DllCall("kernel32\OpenProcess", UInt,0x200, Int,0, UInt,vPID, Ptr)
	DllCall("kernel32\SetPriorityClass", Ptr,hProc, UInt,vPriority)
	DllCall("kernel32\CloseHandle", Ptr,hProc)
}
[Run (alternatives)]
Programs started with the AHK Run command are slow - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 12#p147812
Run/RunWait and set process priority - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 82#p145082
CreateProcess function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
ShellExecute function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
ShellExecuteEx function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

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

[Sleep]

Code: Select all

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

Code: Select all

q:: ;sound beep
DllCall("kernel32\Beep", UInt,523, UInt,150) ;C5 523Hz
return
[SoundGet/SoundSet]
Vista Audio Control Functions - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/2198 ... functions/

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

[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

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

[MonitorGetWorkArea]
[AHK v1: SysGet-MonitorWorkArea]

Code: Select all

q:: ;SysGet-MonitorWorkArea
VarSetCapacity(RECT, 16, 0)
;SPI_GETWORKAREA := 0x30
DllCall("user32\SystemParametersInfo", UInt,0x30, UInt,0, Ptr,&RECT, UInt,0)
vWinX := NumGet(&RECT, 0, "Int"), vWinY := NumGet(&RECT, 4, "Int")
, vWinR := NumGet(&RECT, 8, "Int"), vWinB := NumGet(&RECT, 12, "Int")
, vWinW := vWinR - vWinX, vWinH := vWinB - vWinY
MsgBox, % Format("x{} y{} r{} b{}", vWinX, vWinY, vWinR, vWinB) "`r`n" Format("x{} y{} w{} h{}", vWinX, vWinY, vWinW, vWinH)

SysGet, vMon, MonitorWorkArea
vWinX := vMonTop, vWinY := vMonLeft
, vWinR := vMonRight, vWinB := vMonBottom
, vWinW := vWinR - vWinX, vWinH := vWinB - vWinY
MsgBox, % Format("x{} y{} r{} b{}", vWinX, vWinY, vWinR, vWinB) "`r`n" Format("x{} y{} w{} h{}", vWinX, vWinY, vWinW, vWinH)

;get taskbar coordinates
WinGetPos, vWinX, vWinY, vWinW, vWinH, ahk_class Shell_TrayWnd
MsgBox, % Format("x{} y{} w{} h{}", vWinX, vWinY, vWinW, vWinH)
return
[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
slightly-improved dialogs - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 57#p222957

[AHK v1: 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

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

[Download]
[AHK v1: UrlDownloadToFile]
Simple Download (Bin, ToString und ToFile) - Gebrauchsfertige Skripte & Funktionen - AutoHotkey Community
https://autohotkey.com/board/topic/8919 ... nd-tofile/

[Download (faster alternative)]
[AHK v1: UrlDownloadToFile]
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/1010 ... gress-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/8919 ... nd-tofile/
UrlDownloadToVar [AHK 1.1] - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3291

[WinGetList/WinGetControls/WinGetControlsHwnd]
[AHK v1: 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/AutoHotk ... inEnum.ahk

[WinGetPID]

Code: Select all

q:: ;window get PID (process ID)
WinGet, hWnd, ID, A
DllCall("user32\GetWindowThreadProcessId", Ptr,hWnd, UIntP,vPID, UInt)
MsgBox, % vPID
return
[WinGetProcessPath]
Retrieve the path of a running process - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/1052 ... g-process/
Getting a full executable path from a running process - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/4119 ... g-process/
Getting path of active program - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7291 ... e-program/
process list+file names+command lines - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/8228 ... and-lines/

[WinGetClass]

Code: Select all

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
[WinGetPos (via DllCall: GetWindowRect)]
DllCall
https://autohotkey.com/docs/commands/Dl ... m#Examples

[MenuSelect]
[AHK v1: WinMenuSelectItem]
StrSplit MaxParts and WinMenuSelectItem sysmenu - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 96#p193996

[WinRedraw]
[AHK v1: WinSet-Redraw]

Code: Select all

q:: ;redraw window (the window will flash momentarily)
WinGet, hWnd, ID, A
DllCall("user32\InvalidateRect", Ptr,hWnd, Ptr,0, Int,1)
return
[WinSetTitle]
GUIs via DllCall: get/set internal/external control text - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40514

[MsgBox/InputBox/ToolTip]
[AHK v1: Progress/SplashImage/SplashTextOff/SplashTextOn]
slightly-improved dialogs - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=45220

[(ErrorLevel variable) (built-in variable)]

Code: Select all

;ErrorLevel can be assigned like any other variable
ErrorLevel := 123
MsgBox, % ErrorLevel
ErrorLevel := "abc"
MsgBox, % ErrorLevel
ErrorLevel := ["a", "b", "c"]
MsgBox, % ErrorLevel.Length()
[(True/False variables) (built-in variables)]

Code: Select all

;True or False (True and False variables recreated)
;note: in some programming languages/situations True is defined as -1
vTrue := 1
vFalse := 0
MsgBox, % True " " vTrue
MsgBox, % False " " vFalse
[CallbackCreate/CallbackFree]
[AHK v1: RegisterCallback]
[v1] v2's CallbackCreate function for AHK v1. - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=53457

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 9 guests