CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2004
    Posts
    206

    [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

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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)

  3. #3
    Join Date
    Oct 2004
    Posts
    206

    Resolved Re: How to get handle of main form using ProcessID

    Excellent! That code works like a charm.

    Thanks very much,

    dlarkin77

  4. #4
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    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

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  6. #6
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    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

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  8. #8
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    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.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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...

  10. #10
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    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.

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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...

  12. #12
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: [RESOLVED] How to get handle of main form using ProcessID

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured