CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2002
    Location
    Hyderabad
    Posts
    39

    Rerstricting EXE to single instance

    Hi,
    From my application I am launchin another exe by calling thrugh WinExec("EXE path",SW_SHOW).But the problem here is EXE is getting opened every time when I click the toolbar butto.I want to restrict this for single instance.
    can anybody help me regarding this?

  2. #2
    Join Date
    Sep 2002
    Posts
    16
    Hi,
    If your application is MFC based, you can try the following things,

    The safest way is to use a named Mutex. You can do it like this:
    BOOL CMyApp::IsAppRunning ( )
    {
    HANDLE hMutex = NULL;

    hMutex = CreateMutex ( NULL, TRUE, _T("MySpecialMutexNameHERE") );
    if ( GetLastError() == ERROR_ALREADY_EXISTS )
    {
    CloseHandle ( hMutex );
    return TRUE;
    }
    return FALSE;
    }

    Make sure you change the Mutex name to something unique for your app. Now, all you have to do from your InitInstance() is :

    if ( IsAppRunning () ) return FALSE;

  3. #3
    Join Date
    Jun 2003
    Location
    Cochin
    Posts
    364
    Originally posted by vrishchik007
    If your application is MFC based, you can try the following things,

    If your application is not MFC based too you can use IsAppRunning() by creating an object of CMyApp class and calling it from the main function and return if there is already an instance of the application.

    regards
    collin
    Last edited by joscollin; May 7th, 2004 at 04:46 AM.

  4. #4
    Join Date
    Jun 2002
    Posts
    395
    Originally posted by joscollin
    If your application is not MFC based too you can use IsAppRunning() by creating an object of CMyApp class and calling it from the main function and return if there is already an instance of the application.

    regards
    collin
    The IsAppRunning() function that vrishchik007 wrote contains no MFC code, so you could just incorporate it directly; just define it as a regular function rather than a member of a class, or move it into a different class.

    There's no need to create a CMyApp() class and pull in all of MFC just for this function.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    1. Take a look at this Joseph M. Newcomer' article: Avoiding Multiple Instances of an Application
    2. Don't use WinExec in Win32 applications: this function is obsolete. You should use CreateProcess or ShellExecuteEx instead.

  6. #6
    Join Date
    Jun 2003
    Location
    Cochin
    Posts
    364
    Originally posted by wayside
    There's no need to create a CMyApp() class and pull in all of MFC just for this function.
    The need for creating a class depends on ThomasBoseKenn's coding. He might be coding in MFC or in Win32 C++.

    I just want to say this code will work in a win32 project too.

    regards
    collin.
    Last edited by joscollin; May 8th, 2004 at 12:25 AM.

  7. #7
    Join Date
    Oct 2002
    Location
    Hyderabad
    Posts
    39
    Hi,
    Thanks to u both for the response...but the thing is I don't have the code of EXE which I am launching from my apllication !!
    .....I think that I need to get the process ID and then restrict it...but I don't know how to get the process ID of a perticular application!..can u ppl help me regarding this...!!??

    thanx in advance...

  8. #8
    Join Date
    Jun 2003
    Location
    Cochin
    Posts
    364
    PID's will be different for two instances of the same application.

    If you are spawning the process from your application then do it only once. Also the PID can be retrieved from PROCESS_INFORMATION structure after calling CreateProcess().

    regards
    collin.

  9. #9
    Join Date
    Feb 2002
    Posts
    3,788
    Originally posted by ThomasBoseKenn
    Hi,
    Thanks to u both for the response...but the thing is I don't have the code of EXE which I am launching from my apllication !!
    .....I think that I need to get the process ID and then restrict it...but I don't know how to get the process ID of a perticular application!..can u ppl help me regarding this...!!??

    thanx in advance...
    why don't you do it simpler?
    launch the process the first time and after FindWindow(...) to see if there is already an instance of that process running. if there is do nothing.

  10. #10
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    This FAQ should answer your questions.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

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