Solved

Getting a window handle from process name

Posted on 2004-08-27
5
418 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:c03dn
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 125 total points
ID: 11916018
This will get you main window handle:

            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("MSDEV");
            foreach (System.Diagnostics.Process proc in procs)
            {
                IntPtr hWin = proc.MainWindowHandle
            }
0
 
LVL 19

Expert Comment

by:drichards
ID: 11916026
Of course, you'll have to put in the process name you want!
0
 

Author Comment

by:c03dn
ID: 11916432
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..
0
 
LVL 19

Expert Comment

by:drichards
ID: 11916830
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?
0
 

Author Comment

by:c03dn
ID: 11917003
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now