|
-
February 21st, 2008, 11:04 AM
#1
[RESOLVED] How to get handle of main form using ProcessID
Hi,
I need to get the handle of the main form for a process. All I have to work with is the process id and I don't really know where to start!!
Can anyone suggest where I might begin?
Thanks very much,
dlarkin77
-
February 21st, 2008, 11:54 AM
#2
Re: How to get handle of main form using ProcessID
Since there is no direct API call to determine the hWnd of a ProcID I worked this one out.
There is an API, however, to get the ProcID of an hWnd, so what I did is:
Call EnumerateWindows to get listed all windows of active processes
For every enumerated window check if its proces id is the same as the wanted ProcID, and there we are:
Code:
Option Explicit
'API calls used
'get process ID from a given window handle
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hWnd As Long, _
ByRef lpdwProcessId As Long _
) As Long
'enumerate all windows to a callback function
Private Declare Function EnumWindows Lib "user32" ( _
ByVal lpEnumFunc As Long, _
ByVal lParam As Long _
) As Boolean
'----------------------------------------------------------------
'this holds the return value
Private ProcWinHandle As Long
'this holds the entry process id
Private ProcessHandle As Long
'----------------------------------------------------------------
'this is the function to be called to get the window handle from process ID
Public Function GetProcessWindowHandle(ProcID As Long) As Long
ProcessHandle = ProcID
ProcWinHandle = 0
EnumWindows AddressOf EnumCallBack, 0&
GetProcessWindowHandle = ProcWinHandle
End Function
'this is the callback function for the enumeration process
Private Function EnumCallBack(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim pid As Long
'get the process id for that window handle
GetWindowThreadProcessId hWnd, pid
If pid = ProcessHandle Then 'if it is our process
ProcWinHandle = hWnd 'we found what we wanted
Else 'else
EnumCallBack = True 'tell windows to continue enumeration
End If
End Function
Put the above code in a module (must be in a module because of the callback)
From your program you can then
Code:
dim hWndOfMyProc as Long
hWndOfMyProg = GetProcessWindowHandle(MyProcID)
-
February 22nd, 2008, 04:16 AM
#3
Re: How to get handle of main form using ProcessID
Excellent! That code works like a charm.
Thanks very much,
dlarkin77
-
February 22nd, 2008, 04:22 AM
#4
Re: [RESOLVED] How to get handle of main form using ProcessID
How about this?
WMI Script
Code:
SELECT Handle FROM Win32_Process WHERE ProcessID = num
-
February 22nd, 2008, 07:01 AM
#5
Re: [RESOLVED] How to get handle of main form using ProcessID
That looks interesting.
And how would I implement such a line within a VB program?
Please elaborate on a working example.
-
February 22nd, 2008, 07:59 AM
#6
Re: [RESOLVED] How to get handle of main form using ProcessID
hmm!
Code:
Private Sub Test()
Dim processes
Dim objProcess
Set processes = FindProcess()
Debug.Print "Caption", "Command Line", "Executable Path", "Handle", "ProcessID"
For Each objProcess In processes
Debug.Print objProcess.Caption, objProcess.CommandLine, objProcess.ExecutablePath, objProcess.Handle, objProcess.ProcessID
Next
End Sub
Private Function FindProcess() As Variant
Dim objWMIService, objProcess, objProp
Dim colProcesses
Set objWMIService = GetObject("winmgmts:")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
Set FindProcess = colProcesses
End Function
Private Sub Form_Load()
Call Test
End Sub
-
February 22nd, 2008, 08:08 AM
#7
Re: [RESOLVED] How to get handle of main form using ProcessID
Thats really interesting. I didn't know about that WMI scripting.
BUT Handle and ProcessID seem to be the same number, so Handle does not appear to be the hWnd (window handle) but the process handle.
-
February 22nd, 2008, 08:30 AM
#8
Re: [RESOLVED] How to get handle of main form using ProcessID
ParentProcessID column is also there.
I have just given an example of WMI script.
-
February 22nd, 2008, 11:05 AM
#9
Re: [RESOLVED] How to get handle of main form using ProcessID
Is a window handle column there?
I mean, this WMI scripting is obviously a very sophisticated method to go not only for process data. Would be an elegant solution...
-
February 23rd, 2008, 04:07 AM
#10
Re: [RESOLVED] How to get handle of main form using ProcessID
I'll list all the available columns here which you get from Win32_Process
Code:
1) Caption
2) CommandLine
3) CreationClassName
4) CreationDate
5) CSCreationClassName
6) CSName
7) Description
8) ExecutablePath
9) ExecutionState
10) Handle
11) HandleCount
12) InstallDate
13) KernelModeTime
14) MaximunWorkingSetsize
15) MinimumWorkingSetsize
16) Name
17) OSCreationClassName
18) OSName
19) OtherOperationCount
20) OtherTransferCount
21) PageFaults
22) PageFileUsage
23) ParentProcessID
24) PeakPageFileUsage
25) PeakVirtualSize
26) PeakWorkingSetsize
27) Priority
28) PrivatePageCount
29) ProcessID
30) QuotaNonPoolPagedUsage
31) QuotaPagedPoolUsage
32) QuotaPeakNonPoolPagedUsage
33) QuotaPeakPagedPoolUsage
34) ReadOperationCount
35) ReadTransferCount
36) SessionID
37) Status
38) TerminationDate
39) ThreadCount
40) UserModeTime
41) VirtualSize
42) WindowsVersion
43) WorkingSetsize
44) WriteOperationCount
45) WriteTransferCount
I am using HostMonitor's WMI Explorer.
-
February 23rd, 2008, 06:47 AM
#11
Re: [RESOLVED] How to get handle of main form using ProcessID
Have you any idea where they have the window handle?
The Handle is obviously always the same as the ProcessID, as can be seen in your working sample...
-
February 23rd, 2008, 06:54 AM
#12
Re: [RESOLVED] How to get handle of main form using ProcessID
 Originally Posted by WoF
Have you any idea where they have the window handle?
The Handle is obviously always the same as the ProcessID, as can be seen in your working sample...
No idea!
But just searching for the same using WMI.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|