?
Solved

Urgent gettign the process id of an .exe

Posted on 2003-11-02
1
Medium Priority
?
343 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
[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
1 Comment
 
LVL 9

Accepted Solution

by:
_ys_ earned 800 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

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 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