title: How do I get Process ID from HWND using managed VB.net code?
body: <p>Is there a managed VB.net way to get the Process ID from the HWND rather than using this Windows API call.</p>
  
  <pre><code>Private Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, _
                ByRef lpdwProcessId As Integer) As Integer
  
  Sub GetProcessID()
  
      'start the application
      Dim xlApp As Object = CreateObject("Excel.Application")
  
      'get the window handle
      Dim xlHWND As Integer = xlApp.hwnd
  
      'this will have the process ID after call to GetWindowThreadProcessId
      Dim ProcIdXL As Integer = 0
  
      'get the process ID
      GetWindowThreadProcessId(xlHWND, ProcIdXL)
  
      'get the process
      Dim xproc As Process = Process.GetProcessById(ProcIdXL)
  
  End Sub
  </code></pre>