single instance only at a time

Discussion in 'C++' started by perera, Feb 9, 2007.

  1. perera

    perera Guest

    I want to create a MFC program *.exe. My reguirement is at a time only
    one instance can be run.
    In Other on mutile instnces can be run at a time
    perera, Feb 9, 2007
    #1
    1. Advertising

  2. perera

    perera Guest

    On Feb 9, 4:04 pm, "perera" <> wrote:
    > I want to create a MFC program *.exe. My reguirement is at a time only
    > one instance can be run.
    > In Other words no mutile instnces can be run at a time
    perera, Feb 9, 2007
    #2
    1. Advertising

  3. perera

    abadura Guest

    > I want to create a MFC program *.exe. My reguirement is at a time only
    > one instance can be run.


    Try using mutexes (this method is described along WinAPI WinMain
    function).

    Add to yout CWinApp a HANDLE member variable. Lets name it like
    this:

    private:
    HANDLE m_hOneInstanceMutex;

    and initialize it to NULL in constructor.
    In your CWinApp InitInstance place at the very beging a code like
    this:

    hOneInstanceMutex = CreateMutex(NULL, TRUE, UNIQE_NAME);
    if(hOneInstanceMutex == NULL) {
    if(GetLastError() == ERROR_ALREADY_EXISTS) {
    // one instance is already running so display a message or
    something like this and exit
    return 0;
    }
    // there was an error, do what you want but it is not clear if this
    is first instance or not
    }
    // if execution comes here then you are the first and only instance of
    your application

    Add the code at the begining because there is no point in
    initialization of anything if you are the exiting because another
    instance is already running.
    You don't have to delete the mutex while exiting because system
    will do it for you (as described in CreateMutex specification).

    Choose UNIQE_NAME wisely because if you use a name already used by
    antoher mutex (another application) you will end being not able to run
    your application (not that a "hacker" can prevent your application
    from runnig in this way - as described in WinMain specification).

    Also I think question has more common with WinAPI and MFC than C++
    so perhaps the choice of group you made was not the best one.

    Adam Badura
    abadura, Feb 9, 2007
    #3
  4. perera

    abadura Guest

    > Try using mutexes (this method is described along WinAPI WinMain
    > function).


    MFC has a class CMutex but I am not quite sure if it can be used in
    this problem - and I suspect that it cannot.

    > Add to yout CWinApp a HANDLE member variable. Lets name it like
    > this:
    >
    > private:
    > HANDLE m_hOneInstanceMutex;


    Since system automaticly releases the handle when the process exits
    you can even simplify this by not storing the HANDLE! It is not needed
    and so can be a local variable in InitInstance (actually you only need
    to know if CreateMutex returns NULL or not - and tha actual handle is
    useless in this problem).

    Adam badura
    abadura, Feb 9, 2007
    #4
  5. perera

    abadura Guest

    > Try using mutexes (this method is described along WinAPI WinMain
    > function).


    MFC has CMutex class but I think it cannot be used here for this
    purpose.

    > Add to yout CWinApp a HANDLE member variable. Lets name it like
    > this:
    >
    > private:
    > HANDLE m_hOneInstanceMutex;


    Since the handle is closed by the system after process exits and
    you don't need it for nothing but to know if CreateMutex returned NULL
    or not that the sollution can be simplified by omiting this member...

    Adam Badura
    abadura, Feb 9, 2007
    #5
  6. perera wrote:
    > I want to create a MFC program *.exe. My reguirement is at a time only
    > one instance can be run.


    It has been long time (~8 years), so if my memory is right, there was an
    example code in Visual C++ package. Try to find and if not, you can also
    try to check all open windows names from within your first "init"
    function and then if you find you own window, then send message to
    activate the one you found and exit from current.

    -haro



    > In Other on mutile instnces can be run at a time
    >
    Haro Panosyan, Feb 9, 2007
    #6
  7. * perera:
    > I want to create a MFC program *.exe. My reguirement is at a time only
    > one instance can be run.
    > In Other on mutile instnces can be run at a time


    off-topic in clc++.

    try posting the question in a Windows newsgroup.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Feb 9, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    1
    Views:
    602
    S. Justin Gengo
    Dec 12, 2005
  2. chris brat
    Replies:
    1
    Views:
    628
    chris brat
    May 10, 2006
  3. Hari Sekhon
    Replies:
    11
    Views:
    578
    Eric S. Johansson
    Oct 3, 2006
  4. Tim Williams
    Replies:
    9
    Views:
    288
    Lawrence D'Oliveiro
    Oct 7, 2006
  5. flamesrock
    Replies:
    8
    Views:
    449
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page