Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting a window handle from process name

Posted on 2004-08-27
5
423 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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