Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "AfxPath.inc"
#Include "WMI.inc"
Global hDlg, PID, hBrowser As Dword, BrowserEXE$$
%IDC_Open = 500
%IDC_GetBrowserHandle = 501
%IDC_MoveBrowser = 502
%IDC_ListView = 503
Function PBMain() As Long
Dialog Default Font "Tahoma", 12, 1
Dialog New Pixels, 0, "",200,200,650,300, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Open,"Open Browser", 10,10,130,25
Control Add Button, hDlg, %IDC_GetBrowserHandle,"Get Handles", 10,50,130,25
Control Add Button, hDlg, %IDC_MoveBrowser, "Move Browser", 10,90,130,25
Control Add ListView, hDlg, %IDC_ListView,"" , 150,10,490,400
ListView Insert Column hDlg, %IDC_ListView, 1, "PID", 150, 0
ListView Insert Column hDlg, %IDC_ListView, 2, "Name", 320, 0
ListView Insert Column hDlg, %IDC_ListView, 3, "Path", 320, 0
Dialog Set Text hDlg, Str$(hDlg)
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local iReturn,w,h As Long, hKey As Dword
Select Case Cb.Msg
Case %WM_InitDialog
BrowserEXE$$ = AfxGetDefaultBrowserpath
Dialog Set Text hDlg, "Default Browser: " + BrowserEXE$$
Case %WM_Size
Dialog Get Client hDlg To w,h
Control Set Size hDlg, %IDC_ListView, 490, h-20
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Open
iReturn = ShellExecute(hDlg, "Open", "http://www.garybeene.com/", $Nul, $Nul, %SW_ShowNormal)
Case %IDC_MoveBrowser
SetWindowPos hBrowser, %Hwnd_NoTopMost, 0,0,0,0, %SWP_NoSize
Case %IDC_GetBrowserHandle
'EnumWindows CodePtr(EnumCallBack), 0&
JoseListing
ListView Sort hDlg, %IDC_ListView, 2, Descend
End Select
End Select
End Function
Function EnumCallback (ByVal hWnd As Long, lParam As Long) As Long
Local PID,hProcess As Dword, EXEName As WStringZ*%Max_Path
ListView Insert Item hDlg, %IDC_ListView, 1, 0, Hex$(hWnd)
GetWindowThreadProcessID(hWnd,PID)
hProcess = OpenProcess(%Process_Query_Information Or %Process_VM_Read, %False, PID)
GetModuleBaseName(hProcess, %Null, EXEName, %Max_Path)
CloseHandle(hProcess)
ListView Set Text hDlg, %IDC_ListView, 1,2, ExeName
Function = 1
End Function
Function JoseListing() As Long
Local hr As Long ' // HRESULT
Local pServices As ISWbemServices ' // ISWbemServices interface
Local pObjectSet As ISWbemObjectSet ' // ISWbemObjectSet interface
Local pEnum As IEnumVARIANT ' // IEnumVARIANT interface
Local strDisplayName As WString ' // Display name
Local strQuery As WString ' // Query filter
Local vItem As Variant ' // Collection's item
Local oItem As Dispatch ' // Collection's item
Local vRes As Variant ' // Variant result
' Connect to WMI using a moniker
strDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
pServices = WmiGetObject(strDisplayName)
If IsNothing(pServices) Then Exit Function
' Execute a query to get a reference to the collection of running processes
strQuery = "SELECT ProcessId, Name, ExecutablePath FROM Win32_Process"
pObjectSet = pServices.ExecQuery(strQuery, "WQL", %wbemFlagReturnImmediately)
If IsNothing(pObjectSet) Then Exit Function
pEnum = pObjectSet.NewEnum_ ' Retrieve a reference to the collection's enumerator
If IsNothing(pEnum) Then Exit Function
Do ' Iterate through the collection of objects (WMI collections are 1 based).
hr = pEnum.Next(1, vItem, ByVal %NULL) ' Retrieve a reference to the next object in the collection
If hr <> %S_Ok Then Exit Do
oItem = vItem : vItem = Empty ' Assign the VT_DISPATCH variant to the object variable
If IsNothing(oItem) Then Exit Do
Object Get oItem.ProcessID To vRes ' Retrieve some information about the processes
ListView Insert Item hDlg, %IDC_ListView, 1,0,Str$(Variant#(vRes))
Object Get oItem.Name To vRes ' Retrieve some information about the processes
ListView Set Text hDlg, %IDC_ListView, 1,2, Variant$$(vRes)
Object Get oItem.ExecutablePath To vResv ' Retrieve some information about the processes
ListView Set Text hDlg, %IDC_ListView, 1, 3, Variant$$(vRes)
oItem = Nothing ' Release the object
Loop
pEnum = Nothing ' Release the enumerator
pObjectSet = Nothing ' Release the collection
pServices = Nothing ' Cleanup
End Function
http://www.garybeene.com/sw/gbsnippets.htm