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;


           return 0;

         count=1;  //number of processes running

           if(aProcesses[i] != 0)

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

             // Get a handle to the process.

             // Get the process name.

             if(hProcess != NULL)


             if(ucase(cmpnamepath) == ucase(namepath))


         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


    mutex Class (C++ Standard Library)

    - 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,


    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