Date: 02-16-2022
Return to Index
created by gbSnippets
'A handle to an EXE or a DLL file is required by a variety of API. Here's
'the code to get the handle or filename of the current EXE. The code also
'shows how to load a DLL at runtime and get it's filename as well.
'The API use here, as declared in win32api.inc:
Declare Function GetModuleHandle LIB "KERNEL32.DLL" ALIAS "GetModuleHandleA" _
(lpModuleName As AsciiZ) AS DWord 'NULL returns handle to the EXE
Declare Function GetModuleFileName LIB "KERNEL32.DLL" ALIAS "GetModuleFileNameA" _
(ByVal hModule AS DWord, lpFileName As AsciiZ, ByVal nSize AS DWord) AS DWord
Declare Function LoadLibrary LIB "KERNEL32.DLL" ALIAS "LoadLibraryA" _
(lpLibFileName As AsciiZ) As Long
'Do not use LoadLibrary to start an EXE file (use CreateProcess instead)
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Load WinMsg.dll", 50,10,100,20
Control Add Button, hDlg, 200,"Get EXE Handle", 50,50,100,20
Control Add Button, hDlg, 300,"Get EXE Name", 50,90,100,20
Control Add Button, hDlg, 400,"Get DLL Handle", 50,130,100,20
Control Add Button, hDlg, 500,"Get DLL Name", 50,170,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local h as DWord, temp$, fName As AsciiZ * %Max_Path, h2 as DWord
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
h = GetModuleHandle("") 'get handle to current EXE
If h Then MsgBox Str$(h)
End If
If CB.Msg = %WM_Command AND CB.Ctl = 300 AND CB.Ctlmsg = %BN_Clicked Then
h = GetModuleHandle("") 'get handle to current EXE
GetModuleFileName h, fName, SizeOf(fName)
MsgBox fName
End If
If CB.Msg = %WM_Command AND CB.Ctl = 400 AND CB.Ctlmsg = %BN_Clicked Then
h = LoadLibrary("winmsg.dll")
h2 = GetModuleHandle("winmsg.dll") 'get handle to current EXE
If h = h2 Then MsgBox Str$(h)
End If
If CB.Msg = %WM_Command AND CB.Ctl = 500 AND CB.Ctlmsg = %BN_Clicked Then
h = LoadLibrary("winmsg.dll")
GetModuleFileName h, fName, SizeOf(fName) 'get handle to winmsg.dll
MsgBox fName
End If
End Function
'gbs_00487
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm