Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find window handle with only the executable/process name

Status
Not open for further replies.

ErroR

Technical User
Apr 9, 2001
79
US
I'm currently using the GetWindow() and GetWindowText() APIs (among others) to find the window text of the program I need to shutdown. I then use PostMessage() to send a WM_CLOSE to it and it works great.

However, the title is not consistent and therefore it makes the code a mess to search for everything the title may be. I need to know of a way to get the handle to the window knowing only the executable name, or the name of the process as it would appear in the Task Manager.

Anyone know of a method of doing this?
 
Do you know the window's class name? If so, you can use FindWindow to get the handle.

There's a free utility, WinSpy, that you can download from my web site which will let you browse running processes/open windows and view their attributes such as class name.

Paul Bent
Northwind IT Systems
 
paulbent thanks for the utility suggestion.

I have the classname, but it's running in a command prompt so the class is "ConsoleWindowClass" and this is what all command prompt windows show for their class.

I think somewhere I had seen a suggestion to search for the specific class of that window, but in this particular case there may be 2 or 3 other command prompts open that I must not disturb. Therefore I didnt think this would work in my situation.
 
If your app launches the command window with Shell or ShellExecute, you could use CreateProcess instead. It returns a process id (dwProcessID) in a PROCESS_INFORMATION structure. You can use the id with OpenProcess and TerminateProcess to kill it.

Paul Bent
Northwind IT Systems
 
Excellent suggestion, however this application is already running when my application is started. I have no control over the application besides trying to close it.

The only thing I have to work with the classname obviously, the list of titles the window usually has (inconsistent), the name of the executable (always consistent) and the path to the executable (also consistent) but beyond that I have nothing.

The only truely consistent and seemingly useful info I have is the executable name which is what alwasy shows as the process name ("Image Name") in Task Manager. Any idea what API could be used to find the process by this name?
 
You might be able to do it like this:

- Call EnumWindows. In the callback proc use the returned hwnd to call GetClassName. If the class is the one you're after, load the hwnd in a global array.

- Loop through the array and for each hwnd, call GetWindowLong with the GWL_HINSTANCE flag to get the instance handle.

- For each instance handle, call GetModuleFilename and compare the result with name of the target executable.

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top