Date: 02-16-2022
Return to Index
created by gbSnippets
Which is preferred?
hModule = GetModuleHandle("")
hModule = GetModuleHandle(ByVal %NULL)
hModule = GetModuleHandle(Exe.Path$) '<---- invalid
What is the difference between these two?
threadID = GetCurrentThreadID 'threadID of current thread
threadID = GetCurrentThread 'pseudo handle to current thread
Is the second one right?
hWin = FindWindow(cName,%Nul) '<---- any caption
hWIn = FindWindow(cName,"") '<---- any caption, or no caption?
Is ByVal 0 the same as ByVal %Null?
GetModuleFileName h, fName, SizeOf(fName)
GetModuleFileName(ByVal 0, zPathExeName, %MAX_PATH) 'Get current exe name and path
GetModuleFileName(ByVal %NULL, FileName, %Max_Path)
In enumerating, 've seen
hWin = FindWindow(%Null, %Null)
FindWindow("", Z)
FindWindow("", "Message Box")
FindWindow("Shell_TrayWnd", "")
FindWindow("Shell_TrayWnd", "")
FindWindow(%Null, %Null)
GetForeGroundWindow
GetTopWindow
app --> process ---> thread(s) ---> windows
-hApp -hProcess -hThread -hWin
-hInstance -PID -PID
an application can be run many times
each time is an instance
each instance has its own handle
'hInstance of app
Function WinMain (ByVal hInst As Dword, ...) As Long 'passed to WinMain by OS as hInst argument
hInstance = Shell("calc.exe") 'for app opened via Shell
hInstance = GetWindowLong(hWin, %GWL_HINSTANCE) 'for app containing hWin
'hModule of app or loaded file (EXE/DLL)
hModule = GetModuleHandle("") 'get handle to current EXE
hModule = GetModuleHandle("winmsg.dll") 'get handle of loaded EXE/DLL (after loading)
hModule = LoadLibrary("riched32.dll") 'get handle to loaded EXE/DLL (as loaded)
'path$ to app or loaded file
path$ = Exe.path$ 'PowerBASIC statement - path to EXE
GetWindowModuleFileName(hDlg,wzBuf,SizeOf(wzBuf)) 'path to app associated with window
GetModuleFileName(hModule,wzBuf,SizeOf(wzBuf) 'path to loaded module
'hProcess handle from hInstance
hInstance = Shell("calc.exe") 'instance handle returned
hProcess = OpenProcess(%PROCESS_QUERY_INFORMATION + %PROCESS_TERMINATE, %False, hInstance)
'hProcess from PID
hProcess = OpenProcess(%Process_Query_Informaton or %Process_VM_Read, 0, PID)
'PID from hWin
TID = GetWindowThreadProcessId(hWin,PID) '<--- input hWin, output PID
'threadID of current thread
threadID = GetCurrentThreadID 'API
threadID = ThreadID 'PowerBASIC statement
'hThread of current thread
hThread = GetCurrentThread 'is pseudo handle - always interpreted as handle using it
'hThread of as thread is created
Thread Create MyThread(0) To hThread 'PowerBASIC statement (argument value up to coder)
'Count of all threads in a process
iCount = ThreadCount 'PowerBASIC statement
'hThreads of all threads in a process
??????????? '<--- may not capture all hThreads (handles can be released)?
'TID from hWin
TID = GetWindowThreadProcessId(hWin,PID) '<--- input hWin, output PID
'hWin for Dialog
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg 'hDlg is hWind
'hWin for Control
Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
Control Handle hDlg, %IDC_Button To hWin
'FINDWINDOW API
FindWindow(className, windowName)
'--------------------------------
hWin = FindWindow( cName, wName ) 'wName is Window caption
hWin = FindWindow( cName, %NUL ) 'any Window caption accepted
hWin = FindWindow("#32770", "Caption")
FindWindow("", Z)
FindWindow("", "Message Box")
FindWindow("Shell_TrayWnd", "")
FindWindow("Shell_TrayWnd", "")
FindWindow(%Null, %Null)
'hWin from hInstance =============================================================
'several hWin may be associated with hInstance (an app may have several windows)
'you can iterate all windows, finding those that match the hInstance
Function GetWinHandle(hInstance As Long) As Long 'http://support.microsoft.com/kb/242308
Local hWin As Dword
hWin = FindWindow(%Null, %Null)
Do Until hWin = 0
If GetParent(hWin) = 0 Then
If hInstance = GetWindowLong(hWin, %GWL_HINSTANCE) Then
Function = hWin : Exit Do
End If
End If
hWin = GetWindow(hWin, %GW_hWndNext
Loop
End Function
Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long
GetWindowThreadProcessId hwnd, idProc ' Get PID for this HWnd
Functioni = idProc
End Function
'hWin from PID ===================================================================
'several hWin may be associated with PID (a process have several windows)
'you can iterate all windows, finding those that match the PID
Function GetWinHandle(PID As Long) As Long
'http://support.microsoft.com/kb/242308
Local hWin As Dword
hWin = FindWindow(%Null, %Null)
GetWindowThreadProcessID(hWin,PID)
Do Until hWin = 0
If GetParent(hWin) = 0 Then
If PID = ProcIDFromWnd(hWin) Then
Function = hWin : Exit Do
End If
End If
hWin = GetWindow(hWin, %GW_hWndNext
Loop
End Function
Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim PID As Long
GetWindowThreadProcessId hwnd, PID ' Get PID for this hWnd
Function = PID
End Function
'gbs_01369
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm