none
How can I keep the program from being opened when it's already running

    Question

  • I get issues occasionally with my programs running while another copy of the exe is running in the background.

    I tried putting in a check when the program opens but it doesn't always catch the other copy that's running


    LONG findprogram()
    {
         LONG count;
         DWORD aProcesses[1024],cbNeeded,cProcesses,processID,CurrentProcessId,lpExitCode;
         unsigned int i;
         TCHAR szProcessName[MAX_PATH]=TEXT("<unknown>");
         HANDLE hProcess;
         HMODULE hMod;
         std::string namepath="programname.exe";
         std::string cmpnamepath;
         std::wstring wtxt;

         CurrentProcessId=GetCurrentProcessId();

         if(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))
         {
           return 0;
         }

         cProcesses=cbNeeded/sizeof(DWORD);
         count=1;  //number of processes running

         for(i=0;i<cProcesses;i++)
         {
           if(aProcesses[i] != 0)
           {
             processID=aProcesses[i];

             if(processID == CurrentProcessId)  //leave out current program
               continue;  

             // Get a handle to the process.
             hProcess=OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,processID);

             // Get the process name.

             if(hProcess != NULL)
             {
               if(EnumProcessModules(hProcess,&hMod,sizeof(hMod),&cbNeeded))
               {
                 GetModuleBaseName(hProcess,hMod,szProcessName,sizeof(szProcessName)/sizeof(TCHAR));
               }
             }

             wtxt=szProcessName;
             cmpnamepath=wstrtostr(wtxt);

             if(ucase(cmpnamepath) == ucase(namepath))
             {  
               count++;
               GetExitCodeProcess(hProcess,&lpExitCode);
               TerminateProcess(hProcess,lpExitCode);
             }  

             CloseHandle(hProcess);
           }
         }

         return count;
    }

    Friday, December 7, 2018 2:04 AM

All replies

  • Hi

    Wrong forum - this is a VB.NET forum and your code shows strange semi colons at end of lines :_

    Maybe, have a try with looking at a setting Solution Explorer -> My Project -> Application -> Make Single Instance Application


    Regards Les, Livingston, Scotland

    Friday, December 7, 2018 2:25 AM
  • Hello,

    See the following.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 7, 2018 2:42 AM
  • Hi

    Wrong forum - this is a VB.NET forum and your code shows strange semi colons at end of lines :_

    Maybe, have a try with looking at a setting Solution Explorer -> My Project -> Application -> Make Single Instance Application


    Regards Les, Livingston, Scotland

    It's C++.  You can tell by the use of HMODULE and TCHAR types in all caps.

    A common method is to set the application to use a specific name (as displayed in the Task Manager's Processes view).  At initialization you'd enumerate running processes seeking that name, and if found you fail to launch or else get a handle on that Window to Activate, Focus, Bring To Front, maybe Maximize or flash the taskbar icon to get the user's attention.

    You could also write a boolean value to a registry key on each startup, then check for it on each init and fail with flashing lights and automagically focusing windowpanes.

    You could also open up a "shared memory" block and increment some addressable value in it for each instance of the application that launches.  If the value is 0, go ahead.  If the value is 1, flail about with the pre-existing window/icons to let the user know they're being redundant.

    Because multiple program launches can happen by users setting Accessibility options (like single-clicks that act like double-clicks) and the people who need those conventions can be counted on to unsteadily double click sometimes, the last option is probably the best.  You just need to be a lot more careful with it.  The first two are fire-and-forget.


    It never hurts to try. In the worst-case scenario, you'll learn something.

    Friday, December 7, 2018 2:48 AM

  • It's C++.  You can tell by the use of HMODULE and TCHAR types in all caps.


    Just to be picayune: Use of macros/types such as HMODULE and TCHAR could 
    also occur in C programs. But the use of std::string and std::wstring are
    sure indicators that it is indeed C++ code.

    - Wayne

    Friday, December 7, 2018 8:49 PM
  • The example of using a mutex provided by Karen is in C#.
    For more examples relevant to C++ see:

    Using Mutex Objects
    https://docs.microsoft.com/en-us/windows/desktop/Sync/using-mutex-objects

    <mutex>
    https://docs.microsoft.com/en-us/cpp/standard-library/mutex?view=vs-2017

    mutex Class (C++ Standard Library)
    https://docs.microsoft.com/en-us/cpp/standard-library/mutex-class-stl?view=vs-2017

    - Wayne

    P.S. - This thread should really be moved again. This time to the Visual C++ forum.

    Friday, December 7, 2018 8:53 PM
  • Just to be picayune: Use of macros/types such as HMODULE and TCHAR could 
    also occur in C programs. But the use of std::string and std::wstring are
    sure indicators that it is indeed C++ code.

    - Wayne


    Bleh.  I had actually realized that myself after I hit post.  I was hoping nobody would notice, or if they did they wouldn't call me out on it!

    It never hurts to try. In the worst-case scenario, you'll learn something.

    Friday, December 7, 2018 9:00 PM
    • Proposed as answer by Guido Franzke Monday, December 10, 2018 8:38 AM
    Friday, December 7, 2018 9:36 PM