Solved

Urgent gettign the process id of an .exe

Posted on 2003-11-02
1
337 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

23 Experts available now in Live!

Get 1:1 Help Now