CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2000
    Location
    Sweden, Stockholm
    Posts
    145

    To limit a single instance of a program

    Hi all,

    After searching information about how one can create only one instance of a program I write the following code:
    *****************************
    #include "stdafx.h"
    #include <errno.h>
    #include <Windows.h>

    int main(int argc, char* argv[])
    {
    printf("Hello World!\n");

    HANDLE hMutex = CreateMutex (0,FALSE,"HelloWorld");
    WaitForSingleObject(hMutex, INFINITE);

    long error = GetLastError();

    if(error == ERROR_ALREADY_EXISTS)
    {
    ::MessageBox(NULL, "Already Exists", __FILE__, MB_OK);
    ExitProcess(0);
    }

    ReleaseMutex(hMutex);
    CloseHandle(hMutex);


    return 0;
    }

    ****************************

    But I don't get one instance of my HelloWorld application. Through Task manager I can see that there exits more than one instance of my program.
    When I debug and find the variable
    hMutex holds value "0x00000098" i.e error ERROR_TOO_MANY_MUXWAITERS

    and method GetLastError() always return 0.

    I never gets in "if statement".
    What is going wrong and what I need to change?
    Better if you can send me a small code solving my problem.

    thanks in advance

    regards
    /rsasalm



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

    Re: To limit a single instance of a program


    You need to create a named mutex semaphore when you start your application. When the second one starts it tries to get access to the mutex but will fail...


    // 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 instances 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;
    }
    }




    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  3. #3
    Join Date
    Mar 2000
    Location
    Sweden, Stockholm
    Posts
    145

    Re: To limit a single instance of a program

    Thanks for your reply.
    I still wonder what is named mutex semaphore?
    Is it different from normal semaphore?

    Sorry if it sounds you very basic question.

    regards
    /rsasalm


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

    Re: To limit a single instance of a program

    A named mutex is nothing else than a normal mutex object BUT has a unique name throughout the system. Therefore it can be exist only one with this name. Besides that it behaves just an unnamed one...

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  5. #5
    Join Date
    Mar 2000
    Location
    Sweden, Stockholm
    Posts
    145

    Re: To limit a single instance of a program

    Hi again Andreas,

    Now I see that that in the code you sent in your last mail the class using Mutex is derived from CWinApp which is MFC class. I don't want to use MFC in my application how I can solve my problem then?

    I tried your code as:
    *********************************
    int main(int argc, char* argv[])
    {
    printf("Hello World!\n");

    HANDLE hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");

    switch(::GetLastError())
    {
    case ERROR_SUCCESS: // Mutex created successfully. There is no instances running
    break;
    case ERROR_ALREADY_EXISTS: // Mutex already exists so there is a running instance of our app.
    ::MessageBox(NULL, "Already Exists", __FILE__, MB_OK);
    ExitProcess(0);
    default: // Failed to create mutex by unknown reason
    ;
    //return FALSE;
    }
    return 0;
    ************************

    Which results the same as described in my first mail.

    Do you have any other suggestions other than using MFC classes.

    thanks again

    regards
    /rsasalm


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

    Re: To limit a single instance of a program

    Well there is a slight mistake in your assumption...the example was based on a MFC application but the mutex itself isn't derived from CWinApp at all. 'CreateMutex()' is a SDK function therefore independent from the MFC. I don't know why it's still not working I used it a lot the way I described in my earlier post in MFC applications as well as in console applications without having problems at all...

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  7. #7
    Join Date
    Mar 2000
    Location
    Sweden, Stockholm
    Posts
    145

    Re: To limit a single instance of a program

    Thanks again Andreas,

    Yes, you are right CreateMutex is an WinAPI function and should be the same either using with console or MFC application.

    The thing which surprises me that why my code doesn't work.
    I have even searched CreateMutex word in the mail archive and found the similar solution as I write.

    I am using VisulC++6.0 and WinNT 4.0.

    Can somebody else check my code and find what I am missing.
    I send my code again.
    *****************************************
    int main(int argc, char* argv[])
    {
    HANDLE hMutex;


    printf("Hello World!\n");


    hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");

    switch(::GetLastError())
    {
    case ERROR_SUCCESS: // Mutex created successfully. There is no instances running
    break;
    case ERROR_ALREADY_EXISTS: // Mutex already exists so there is a running instance of our app.
    ::MessageBox(NULL, "Already Exists", __FILE__, MB_OK);
    ExitProcess(0);
    default: // Failed to create mutex by unknown reason
    ;
    }

    return 0;
    }
    ****************************************
    When I debug and find the variable
    hMutex holds value "0x00000098" i.e error ERROR_TOO_MANY_MUXWAITERS


    I have tried my code again and again but couldn't get what the code is supposed to do.

    Any kind of help will be highly appreciated.

    thanks
    regards
    /rsasalm


  8. #8
    Join Date
    Apr 2001
    Location
    CA , USA
    Posts
    83
    Hi


    Hi i want to open many doc files from explorer in a singleton application. I have a singleton application that is working fine(singleton is achieved using mutex). But i want to send message to that existing application when ever user tries to open many other files from the windows explorer with the file name that comes in the command line. How can i communicate bet'n processes using shared memory?? I tried using GlobalAlloc and sending the message. But it did not work.

    Any help is greately appreciated

    Shashi

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