Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting a window handle from process name

Posted on 2004-08-27
5
Medium Priority
?
429 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

718 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