CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2004
    Posts
    156

    detect multiple instances of our program

    Friends is there any way to detect two instances of our program executing.I mean multiple instances of the application running so that i could prompt a message to the user
    Last edited by anand123; September 24th, 2004 at 12:46 AM.

  2. #2
    Join Date
    Sep 2004
    Posts
    156

    Re: detect multiple instances of our program

    i got it


    // app.h
    class CYourApp : public CWinApp
    {
    ...

    private:
    HANDLE hMutex;
    };

    // app.cpp
    BOOL CYourApp::InitInstance()
    {
    // Create mutex
    hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");

    switch(::GetLastError())
    {
    case ERROR_SUCCESS:
    // Mutex created successfully. There is no instance running
    break;

    case ERROR_ALREADY_EXISTS:
    // Mutex already exists so there is a running instance of our app.
    return FALSE;

    default:
    // Failed to create mutex by unknown reason
    return FALSE;
    }
    }



    i got the code from previous faq

  3. #3
    Join Date
    Jul 2004
    Posts
    155

    Re: detect two instances of our program

    hi ,

    what will happen , if another program resort to the same technique ?
    (with mutex name "GlobalMutex" )
    why one cannot use GUIDs for this ? any thoughts ?

    Praseed Pai

  4. #4
    Join Date
    Sep 2004
    Posts
    27

    Re: detect two instances of our program

    HANDLE hdl;
    hdl = CreateMutex(NULL, TRUE, "MyTestMutex");
    if(hdl)
    {
    DWORD dwRetCode = GetLastError();
    if(dwRetCode == ERROR_ALREADY_EXISTS)
    {
    AfxMessageBox("Another instance is already running@!!!");
    return FALSE;
    }
    }

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: detect two instances of our program

    Quote Originally Posted by SOCM_FP_CPP
    hi ,

    what will happen , if another program resort to the same technique ?
    (with mutex name "GlobalMutex" )
    why one cannot use GUIDs for this ? any thoughts ?

    Praseed Pai
    The FAQ presents a simple technique to achive the goal. Of course "GlobalMutex" is not a very good name. You can come with something like "GlobalMutex@ProgramName#Author@Year". I doubt that you'll have two indentical identifiers...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Aug 2004
    Location
    Earth, Solar System, Milky Way
    Posts
    80

    Re: detect two instances of our program

    ... or better use GUIDGEN.EXE provided with Visual C++ to generate a unique string as for example "{3690872D-DAB2-4243-B5F8-1832567BC25E}".
    Hokutata Yakubotu

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: detect two instances of our program

    Take a look at the following FAQ...

  8. #8
    Join Date
    Sep 2004
    Posts
    156

    Re: detect two instances of our program

    but this is not working in vc.net ,its working in vc++ only

  9. #9
    Join Date
    Jul 2004
    Posts
    155

    Re: detect two instances of our program

    Hi anand ,

    It seems that ur writing a managed application using VC.NET. if ur trying to write unmanged ( win32 console or Win32 gui ) code , the above scheme should work.

    Praseed Pai

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: detect two instances of our program

    Quote Originally Posted by anand123
    but this is not working in vc.net ,its working in vc++ only
    What do you mean with "it not working in vc.net"? This will work disregarding whether it is .NET or not in the first place...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured