This should help,
Code:
'#Written by Grant French, MCPUK, Feb 2002
'#http://www.mcpuk.net, grant@mcpuk.net
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
Else
'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
'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.
Grant
grant@mcpuk.net