locked
How can I keep the program from being opened when it's already running RRS feed

  • 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

  • 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
    • Proposed as answer by Guido Franzke Monday, December 10, 2018 8:38 AM
    Friday, December 7, 2018 9:36 PM
  • Hello,

    Have you got any updates? If your case has been solved, please help to mark answers. If not, just feel free to contact us.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Jack



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 25, 2018 6:49 AM