Urgent gettign the process id of an .exe

Posted on 2003-11-02
Last Modified: 2007-12-19
I want to check to see if there is an instance of a program already runnining and just return from the main method if there is one.  However, I would also like to notify the already running instance that another instance tried to start.  I was doing this easily before using the following scheme:

Process[] procs = Process.GetProcesses();
      int len = procs.GetLength(0);
      for (int i=0; i<len; i++)
                      Process p = procs[i];
                      if (p.Id!=0)
            IntPtr hWnd = p.MainWindowHandle;
            if (hWnd.ToInt32() != 0)
            { // if has a main window:
                                 ProcessModule pm = p.MainModule;
                       String modname = pm.ModuleName;
                       if(modname.ToLower() == "appName.exe")
                                                    SendMessage(hWnd, WM_MESSAGE...)

However, I want to do this same thing with a tray application, so it will have no main Window.  I can't use the above method as it will detect itself as already running.  So, how can I get the Process ID or hWnd of the .exe itself to check against the other processes to see if it is finding itself?  
Question by:jjacksn
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
1 Comment

Accepted Solution

_ys_ earned 200 total points
ID: 9670231
Comparing process Ids should be sufficient.

The static method System.Diagnostics.Process.GetCurrentProcess ( ) returns details on the current process.

using System.Diagnastics;

int iCurrentProcessId = Process.GetCurrentProcess( ).Id;
ProcessModule pmCurrentProcessModule = Process.GetCurrentProcess( ).MainModule;

Process[] procs = Process.GetProcesses();
for (int i = 0; i < procs.GetLength(0); ++i)
    Process p = procs[i];
    if (p.Id == iCurrentProcessId)
    //  Finding itself !!
        ProcessModule pm = p.MainModule;
        String modname = pm.ModuleName;

        if(modname.ToLower( ) == pmCurrentProcessModule.ModuleName.ToLower( ))
        //  Compare versions as well - may have multiple installations
            FileVersionInfo vsnInfo = pm.FileVersionInfo;
            if ( ... )
                SendMessage(hWnd, WM_MESSAGE...)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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