Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

  1. #include <windows.h>
  2. #include "resource.h"
  3. #include "dlgapp.h"
  4. #include "util.h"
  5. // Code to ensure only one instance of a particular window is running
  6. HANDLE CheckForOtherInstance(HINSTANCE hInstance)
  7. {
  8. TCHAR szCaption[128];
  9. HANDLE hMutex;
  10. LoadStringAuto(hInstance, IDS_TITLEBAR, szCaption, 128);
  11. // We create a named mutex with our window caption just as a way to check
  12. // if we are already running autorun.exe. Only if we are the first to
  13. // create the mutex do we continue.
  14. hMutex = CreateMutex (NULL, FALSE, szCaption);
  15. if ( !hMutex )
  16. {
  17. // failed to create the mutex
  18. return 0;
  19. }
  20. else if (GetLastError() == ERROR_ALREADY_EXISTS)
  21. {
  22. // Mutex created but by someone else, activate that window
  23. HWND hwnd = FindWindow( WINDOW_CLASS, szCaption );
  24. SetForegroundWindow(hwnd);
  25. CloseHandle(hMutex);
  26. return 0;
  27. }
  28. // we are the first
  29. return hMutex;
  30. }
  31. /**
  32. * This function is the main entry point into our application.
  33. *
  34. * @return int Exit code.
  35. */
  36. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin, int nShowCmd )
  37. {
  38. HANDLE hMutex = CheckForOtherInstance(hInstance);
  39. if ( hMutex )
  40. {
  41. CDlgApp dlgapp;
  42. dlgapp.Register(hInstance);
  43. if ( dlgapp.InitializeData(lpCmdLin) )
  44. {
  45. dlgapp.Create(nShowCmd);
  46. dlgapp.MessageLoop();
  47. }
  48. CloseHandle(hMutex);
  49. }
  50. return 0;
  51. }