Code example to check only one program instance

Add the following code to your WinMain() function to check that only one instance of a program is running. The code snippet first checks to see if the application exists, if it does the application will be brought to the foreground.

In this example we have a win32 based Borland TApplication with the name "Application_name" (i.e. Application->Title = "Application_name").

Show Plain Text
C code
  1. WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  2. {
  3.     HWND HWnd;
  4.     if ((HWnd = FindWindow("TApplication", "Application_name")) != NULL)
  5.     {
  6.         ShowWindow(HWnd, SW_RESTORE);
  7.         SetForegroundWindow(HWnd);
  8.         return 0;
  9.     }
  10.     ...
  11. }
Filed under: Code Snip  Tags: C

0 Responses to “Code example to check only one program instance”

Sorry, comments have been closed for this post.