Solved

Get Process ID from MainWindowHandle

Posted on 2009-05-08
2
1,132 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
[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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Title # Comments Views Activity
LINQ Select all records by MAX data syntax help 1 27
Input parameteres to DragOver 2 36
PowerShell:  Where-Object not working as a filter 2 71
TFS 2015 Access denied. 1 30
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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