Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Urgent gettign the process id of an .exe

Posted on 2003-11-02
1
Medium Priority
?
344 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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

609 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