CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Check "Application Running .."

    Hi ,

    For my project I need to check whether my application instance is already running or not.

    I got the following code from Net ..


    [code]


    HANDLE mutex;

    mutex = CreateMutex( NULL, TRUE, _T("MYAPPNAME"));

    if ( GetLastError() == ERROR_ALREADY_EXISTS )
    {
    // There's another instance running. What do you do?
    AfxMessageBox(_T("Application Already Exists"));
    PostQuitMessage(0);
    ExitThread(0);
    return;

    }
    [\code]




    The code works fine .. but the code even works fine , even if I pass any string as a parameter to CreateMutex.

    for e.g.

    mutex = CreateMutex( NULL, TRUE, _T("QQQQ"));


    How it is working fine ? I am confused .. PL help

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Check "Application Running .."

    It is working "fine" as long as no other application has created the same named object.
    See:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Avoiding Multiple Instances of an Application
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: Check "Application Running .."

    Thank u VictorN sir ..

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Check "Application Running .."

    For uniqueness, it's handy to just generate a guid (from the tools\create guid menu item in Visual Studio).

    _T("81804F9D-84EB-472F-8E33-1073424CE635")

    Also, read up on the global namespace for mutex naming, in case multiple users are logged in or remoted in to the same machine.

    P.S. Don't use the guid I've posted above because I'm using it.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Check "Application Running .."

    Quote Originally Posted by Arjay View Post
    P.S. Don't use the guid I've posted above because I'm using it.
    Not smart should've created a new one "just in case".

  6. #6
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: Check "Application Running .."

    Thank you all .. I will read more on this

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