Results 1 to 3 of 3

Thread: Shell()'s TaskID to hWnd?

  1. #1
    Guest
    How do I get the window handle of a shelled program's main window from the taskid that the shell function returns?

    E.G. If I use the following code:
    Code:
    lTaskID = Shell("pbrush.exe", vbNormalFocus)
    I need to use lTaskID to get the handle of the window entitled "Untitled - Paint".

    I can't simply use FindWindow because the FindWindow function would usually run before the shelled program has finished loading up. That is the method I'm currently using, along with the Sleep function, but it's far from reliable.

    I can use the ShellExecute API directly is that is easier.

  2. #2
    Lively Member
    Join Date
    Aug 2000
    Location
    quebec
    Posts
    81

    Cool

    From an example on MSDN using ShellExecute...

    Code:
    'in module
    Public Const GW_HWNDNEXT = 2
    
    Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
      ByVal wCmd As Long) As Long
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
      (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Public Declare Function GetWindowThreadProcessId Lib "user32" _
      (ByVal hwnd As Long, lpdwprocessid As Long) As Long
      
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
            (ByVal hwnd As Long, _
            ByVal lpOperation As String, _
            ByVal lpFile As String, _
            ByVal lpParameters As String, _
            ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    
    Function ProcIDFromWnd(ByVal hwnd As Long) As Long
       Dim idProc As Long
      
       ' Get PID for this HWnd
       GetWindowThreadProcessId hwnd, idProc
       ProcIDFromWnd = idProc
    End Function
          
    Function GetWinHandle(hInstance As Long) As Long
       Dim tempHwnd As Long
          
       ' Grab the first window handle that Windows finds:
       tempHwnd = FindWindow(vbNullString, vbNullString)
       
       ' Loop until you find a match or there are no more window handles:
       Do Until tempHwnd = 0
          ' Check if no parent for this window
          If GetParent(tempHwnd) = 0 Then
             ' Check for PID match
             If hInstance = ProcIDFromWnd(tempHwnd) Then
                ' Return found handle
                GetWinHandle = tempHwnd
                ' Exit search loop
                Exit Do
             End If
          End If
       
          ' Get the next window handle
          tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
       Loop
    
    End Function
    C/C++,Delphi,VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
    I love deadlines. I like the whooshing sound they make as they fly by.
    —Douglas Adams

  3. #3
    Guest
    Cheers, matey!

    That code looks promising.
    I'll give it a try on Monday when I'm back in work.

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