How can I launch an application and get it's hWnd? I can't simply use FindWindow(), because there is more than one open window with the the caption.
Is there a function to launch an application that returns the hWnd? Or any other method?
Printable View
How can I launch an application and get it's hWnd? I can't simply use FindWindow(), because there is more than one open window with the the caption.
Is there a function to launch an application that returns the hWnd? Or any other method?
the app youre launching doesnt have a hWnd by itself. apps are identified by their process id's, but their windows (and other objects inside them) are identified by hWnd's.
it's not very clear which hWnd you are trying to get: do you need a hWnd in your own application? (that should be simple, because the forms and most of the controls expose their hWnd as a property). or your application starts another application (with Shell or something) and you need to capture a hWnd from that started app?
you could enum the windows and get each hwnd for each window of the same caption... but as like radum said.. each window will have its own handle.. so there is no way to get an hwnd for a multiple window application... process ID may be the way to go.. or are you trying to manipulate certain windows in this application?Quote:
Originally posted by The Hobo
How can I launch an application and get it's hWnd? I can't simply use FindWindow(), because there is more than one open window with the the caption.
Is there a function to launch an application that returns the hWnd? Or any other method?
Okay...
I want to launch a window/application/box with a title. Such as Shell(filename).
What I then want to do is get the hWnd of the window, so I can later CloseWindow() said window.
However, since there is more than one application/window running with that title (I see three when I press alt+ctrl+del), FindWindow() does not always return the one that I launched.
Sorry if my terms are not correct, but I'm hoping this helps explain.
It is not a multiple window application, there just happens to be two programs running with the same caption.
you could always just end task on it via code.. as long as it is not processing anything at the time.. it will close like normal (otherwise you will get the not responding message)
OR
you can enum all the desktop windows and send a close message to each that match the caption you are looking for
How do I "end task on it via code?"Quote:
Originally posted by kleinma
you could always just end task on it via code.. as long as it is not processing anything at the time.. it will close like normal (otherwise you will get the not responding message)
But I don't want to close the other ones. Just the one that I launched.Quote:
Originally posted by kleinma
you can enum all the desktop windows and send a close message to each that match the caption you are looking for
this is more or less a brutal method to close an application already running. just copied-pasted it from one of my apps, but i have it from elsewhere... dont remember where. let me know if you need the API declarations part as well. you would call this sub with the process ID returned by the vb Shell() function. Also, take care of the GetWindow() API function call: you would have to substitute UserControl.ContainerHwnd with some hWnd of the application that attempts to close the shelled app (should be a Me.hWnd or something).
VB Code:
Sub StopApplication(ByVal procID As Long) 'shuts down a shelled application 'procID is the value obtained from the Shell() function Dim hwCurr As Long Dim getPid As Long Dim lPid As Long Dim lHp As Long 'Start enumeration of top level windows, get first handle hwCurr = GetWindow(UserControl.ContainerHwnd, GW_HWNDFIRST) Do While hwCurr 'Get the Pid from the window handle GetWindowThreadProcessId hwCurr, getPid If getPid = procID And IsWindowVisible(hwCurr) Then 'If the Pid's match and the window is visible, close it down SendMessageTimeout hwCurr, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0 'If the window doesn't like gentle persuasion, 'bring out the nipple clamps to force it to close If IsWindow(hwCurr) Then Call GetWindowThreadProcessId(hwCurr, lPid) lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid) TerminateProcess lHp&, 0& CloseHandle lHp End If Else 'Continue enumeration hwCurr = GetWindow(hwCurr, GW_HWNDNEXT) End If Loop End Sub
Perfect. Thanks.