get hwnd from hprocess-VBForums
Results 1 to 2 of 2

Thread: get hwnd from hprocess

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002

    Question get hwnd from hprocess

    I am writing a shell type program, and am wondering how i can get the window handler of a program, from the process id.
    i am using
    hprocess = OpenProcess(PROCESS_QUERY_INFORMATION, 1, Shell(app), vbNormalFocus))
    to run the program, so ic an tell when it is finished. I also need to be able to minimze/maximize/retore the program but you need the window handler to do that. any help????

  2. #2
    Junior Member
    Join Date
    Feb 2002
    This should help,

    '#Written by Grant French, MCPUK, Feb 2002
    Option Explicit
    'API Declarations
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    'Variable Declarations
    Private Found_hWnd As Long
    Public Function ExecuteAndReturnHWnd(ExecutePath As String) As Long
    'List all windows on the system, (send them to our callback function EnumWindowsCallBack)
    Call EnumWindows(AddressOf EnumWindowsCallBack, Shell(ExecutePath, vbNormalFocus))
    If Not Found_hWnd = 0 Then
        'Windows Handle has been found, return it
        ExecuteAndReturnHWnd = Found_hWnd
        'Windows Handle has not been found, return -1
        ExecuteAndReturnHWnd = -1
    End If
    End Function
    Private Function EnumWindowsCallBack(ByVal hwnd As Long, ByVal lParam As Long) As Long
    'Declare local variable
    Dim lngProcessID
    'Get the ProcessID of the returned handle
    GetWindowThreadProcessId hwnd, lngProcessID
    'If the process id of the returned handle is equal to the processID of
    '   the window we are looking for then store the window handle in
    '   Found_hWnd, and stop the enumeration process
    If lParam = lngProcessID Then
        Found_hWnd = hwnd
        EnumWindowsCallBack = False
        'Else continue enumerating all windows on the system
        EnumWindowsCallBack = True
    End If
    End Function
    '#Sample Usage#
    Sub main()
        'Run notepad and display a message box with its window handle in it
        MsgBox ExecuteAndReturnHWnd("notepad.exe")
    End Sub
    Any problems, mail me.


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

Survey posted by VBForums.