Solved

Getting a window handle from process name

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 55
Image(7) 1 53
ConsoleSql 1 34
Installing IIS after Visual Studio 2015 Community Edition 5 46
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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

8 Experts available now in Live!

Get 1:1 Help Now