Wednesday, May 16, 2012

Fun with DllCall in AutoHotkey

Ok so there are more fun things to do out there. If you're the type of AutoHotkey programmer that doesn't like having to use the command prompt to write to an extra text file which your program has to read, then this is for you. DllCall will usually run contained in the program and can be hidden from the end-user.

The one to rule them all:
This is a function I pulled from the Autohotkey forums submitted by SKAN which lists all the functions for a specified Dynamic Link Library (DLL) along with an inputbox for convenience:
InputBox, FunctionToDo, DLL File, Enter the name of the DLL file you want to (suffix with .dll):,,500,120,,,,,shell32.dll
If not ErrorLevel {
	ThisData:=PE_FunctionExports( FunctionToDo )
	IfExist, %A_Temp%\delete_me.txt
		FileDelete, %A_Temp%\delete_me.txt
	FileAppend, %ThisData%, %A_Temp%\delete_me.txt
	RunWait, %A_Temp%\delete_me.txt
}
PE_FunctionExports( PEFile ) {
	; Lists Exported Functions/ Comp@t: WIN32_NT +AHK +LA +LW
	;By SKAN www.autohotkey.com/forum/viewtopic.php?p=379086#379086 CD:20100824/LM:20100825

	VarSetCapacity( $LI,48,0 ), DllCall( "ImageHlp\MapAndLoad", A_IsUnicode ? "AStr" : "Str",PEFile, Int,0, UInt,&$LI, Int,1, Int,1 )
	nPtr := DllCall( "ImageHlp\ImageRvaToVa", UInt,Numget( $LI,12 ), UInt,Numget( $LI,08 ), UInt, NumGet( ( P := DllCall( "ImageHlp\ImageDirectoryEntryToData", UInt, NumGet( $LI,8 ), Int,0, UShort,0, UIntP,nSz )) + 12 ), UInt,0 )
	VarSetCapacity( Var,1024,0 ), VarSetCapacity( List,10240,0 )
	IfEqual,nPtr,0, Return SubStr( DllCall( "ImageHlp\UnMapAndLoad", UInt,&$LI ), 0,0 )
	Loop % NumGet( P+24 ) + 1
		A_IsUnicode ? Var := DllCall( "MulDiv", Int,nPtr, Int,1, Int,1, AStr ) : DllCall( "lstrcpy", Str,Var, UInt,nPtr ), nPtr := nPtr+StrLen( Var )+1,  List .= "`n" Var, A_Index=1 ? Omit := StrLen( Var )+2 : StringTrimLeft, List, List, %Omit%
	DllCall( "ImageHlp\UnMapAndLoad", UInt,&$LI )
	Return List
}
ExitApp


Examples of DllCall'ing:
// get number of network card interfaces
DllCall("iphlpapi\GetNumberOfInterfaces", "UintP", nIf)
MsgBox % nIf
...
// get current process ID
ProcessID := DllCall("GetCurrentProcessId")

Add comment

Please note: all comments are reviewed before being published.


Security code
Refresh

Member Login

Joes Latest Members

A huge WELCOME goes to today's newest members:

  • infonat
Member Signups (Activated)
BeforeCurrentTrend
Day21=
Week105ê
Month11224ê
Year2,265612ê

Member Stats
69 guests are currently online.
1,178 members are still deciding.
There are 5,935 members in total.

Latest Comments

Paypal Donations

Want to support my work? Any donation is a blessing :c)