Link to home
Start Free TrialLog in
Avatar of c03dn
c03dn

asked on

Getting a window handle from process name

I need to find out if there is a way to get a window handle from just a process name in C# and if so, how. Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drichards
drichards

Of course, you'll have to put in the process name you want!
Avatar of c03dn

ASKER

Ie tried that, and it's troublesome because it doesn't use the executable file name. If I look at the Process tab in the task manager, Notepad will be listed as notepad.exe. To search for it using your method I will have to omit the .exe part. Isn't there some better method? I am almost sure that I have read about one but I am not able to find it..
There very well may be a better way, but I do not know of one.  There really isn't a standard notion of "process name" anyway.  You notice that in task manager, the column is labeled "Image Name".  In Win32 API, the PROCESSENTRY structure that you use when enumerating processes has a 'szExeFile' member.  This is what Task manager uses as the Image Name.  Really, the process ID is the identifier.  It is unique, whereas several processes may have the same name if you go by executable image name.

What is your requirement here?  Where are you getting a process name from and is it possible to use the process ID instead?
Avatar of c03dn

ASKER

I am making a small program which will force focus of a program during repeated intervals. The program which is to be focused can be changed when the program is running. For example, while the program is running, the user can execute:

focus.exe /f:notepad.exe

and this will change the current focus to the notepad program. I am going to use this program on my home theater pc to prevent any program going out of of focus and then in turn breaking the interface. Kind of crude and unsophisticated, but hey..

So I guess what I mean is executable name instead of process name. I think that's the easiest way to identify which program is to be focused. I need it so that I can call the API functions and focus on the specific handle of the program. But if the process name is just the executable with the .exe part removed, it's not that hard to trim it off the end i guess, but it feels kind of weird.

Anyways, sorry for not being more specific on the executable/process name part. I'll leave it open for now and if someone doesn't come up with a better way I'll award you the points.