Solved

Get Process ID from MainWindowHandle

Posted on 2009-05-08
2
1,119 Views
Last Modified: 2013-11-08
I am attempting to kill a process by using the processID.  I can seem to do this by the process name, but I need to be able to do this by the actual processID since I will have multiples running with the same name I can not kill.  For some reason they are all returning a processID = 0, even when I set the window to visible.  If I debug and look at all the processes collected in the getprocesses() method and take a look at the processes that have open windows (i.e. notepad) they have zero as mainwindowhandle.
private Int32 GetCurrentExcelProcessID()

        {
 

            Process[] aProcesses = Process.GetProcesses();
 

            Int32 processID = 0;
 

            bool originalVisibility = commXl.Visible;
 

            commXl.Visible = true;
 

 
 

            for (int i = 0; i <= aProcesses.GetUpperBound(0); i++)
 

            {
 

                if (aProcesses[i].MainWindowHandle.ToString() == commXl.Hwnd.ToString())
 

                {
 

                    processID = aProcesses[i].Id;
 

                    break;
 

                }
 

            }
 

            commXl.Visible = originalVisibility;
 

            return processID;
 

        }
 

 
 

        private void KillCurrentExcelProcess()
 

        {
 

            Process[] aProcesses = Process.GetProcesses();
 

            Process aProcess = null;
 

 
 

            for (int i = 0; i <= aProcesses.GetUpperBound(0); i++)
 

            {
 

                if (aProcesses[i].Id.ToString() == currProcID.ToString())
 

                {
 

                    aProcess = aProcesses[i];
 

                    break;
 

                }
 

            }
 

 
 

            try
 

            {
 

                if (aProcess != null)
 

                {
 

                    aProcess.Kill();
 

                }
 

            }

            catch { }

        }

    }

Open in new window

0
Comment
Question by:dtadin
2 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 24345663
HI

This is a known problem
Try to use WIN32 API GetMainProcessWindow(<processId>) instead of aProcesses[i].MainWindowHandle
0
 
LVL 4

Accepted Solution

by:
TMarkham1 earned 250 total points
ID: 24704397
elimeskika:

I looked through the Windows API Reference on MSDN, and I don't see a function named GetMainProcessWindow in the entire API (unless I am looking in the wrong place).

I've seen this "GetMainProcessWindow" functionality offered up as a solution a few times in web searches I've done, but none of them went beyond a basic suggestion of "Try the WIN32 API GetMainProcessWindow instead of MainWindowHandle".

Have you personally implemented your suggestion in the past using the .Net Framework? If so, how about a code example?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
How can I use SQL Stored Procedure to return FedEx Zone 17 42
Error in page 3 46
Device Location 4 19
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

19 Experts available now in Live!

Get 1:1 Help Now