Solved

Urgent gettign the process id of an .exe

Posted on 2003-11-02
1
336 Views
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?  
0
Comment
Question by:jjacksn
1 Comment
 
LVL 9

Accepted Solution

by:
_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 !!
    }
    else
    {
        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...)
            }
        }
    }
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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