Page 1 of 1
HInstance ==> hWnd
Posted: Mon Jun 09, 2003 2:53 pm
by Thorsten
How I can get from a HInstance the hWnd?
I start the web-browser with:
HInstance = ShellExecute_(...)
And now I want to close the window with:
If IsWindow_(hWnd) <> 0
PostMessage_(hWnd, #WM_CLOSE, #Null, #Null)
EndIf
But ... I need the hWnd from the HInstance :roll:
Re: HInstance ==> hWnd
Posted: Tue Jun 10, 2003 2:34 am
by PB
> How I can get from a HInstance the hWnd?
Here's some Visual Basic code that I can't get working... but it apparently
does what you want. I've wanted this for ages, too.

If you can get it
working then please post the PureBasic version here, thanks.
Code: Select all
Private Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
If GetParent(test_hwnd) = 0 Then
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
pid = Shell("calc.exe") 'launch application
hWin = InstanceToWnd(pid) 'get handle of launched window
Posted: Tue Jun 10, 2003 3:33 am
by freak
The problem here is, that RunProgram() doesn't return a ProcessID.
What it returns is a Process HANDLE, which is a different thing.
The GetWindowThreadProcessId_() API however returns Process IDs,
so this search will never find anything.
In VB, this seems different.
You must find a way to get the ProcessID from the HANDLE RunProgram() returns.
There is a GetProcessID() API function, but it only exists in WinXP with SP1.
Unfortunately, I know no way to get this ID.
Timo
Posted: Tue Jun 10, 2003 7:03 am
by Thorsten
At the german PureBasic forum I found the answer:
Start a browser or any other program:
BrowserHandle = RunProgram(Program, File, "", #Null)
And later close it:
TerminateProcess_(BrowserHandle, #Null)
The problem was: If I start the browser like that:
BrowserHandle = RunProgram(URL, "", "", #Null)
Then you get NO Handle
