Counter Strike : Global Offensive Source Code
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.

69 lines
2.0 KiB

  1. // ======= Copyright (c) 2009, Valve Corporation, All rights reserved. =========
  2. //
  3. // appinstance.h
  4. //
  5. // Purpose: Provide a simple way to enforce that only one instance of an
  6. // application is running on a machine at any one time.
  7. //
  8. // Usage: declare a global object of CSingleAppInstance type, with the unique name
  9. // you want to use wrapped in the TEXT( " " ) macro.
  10. //
  11. // upon entering main you can check the IsUniqueInstance() method to determine if another instance is running
  12. // or you can call the CheckForOtherRunningInstances() method to perform the check AND optinally
  13. // pop up a message box to the user, and/or have the program terminate
  14. //
  15. // Example:
  16. //
  17. // CSingleAppInstance g_ThisAppInstance( TEXT("this_source_app") );
  18. //
  19. // void main(int argc,char **argv)
  20. // {
  21. // if ( g_ThisAppInstance.CheckForOtherRunningInstances() ) return;
  22. //
  23. // .. rest of code ..
  24. //
  25. // Notes: Currently this object only works when IsPlatformWindows() is true
  26. // i.e. no Xbox 360, linux, or Mac yet..
  27. // (feel free to impliment)
  28. //
  29. // ===========================================================================
  30. #ifndef APPINSTANCE_H
  31. #define APPINSTANCE_H
  32. #ifdef _WIN32
  33. #pragma once
  34. #endif
  35. // check if handle is defined rather than inlcude another header
  36. #ifndef HANDLE
  37. typedef void *HANDLE;
  38. #endif
  39. class CSingleAppInstance
  40. {
  41. public:
  42. explicit CSingleAppInstance( tchar* InstanceName, bool exitOnNotUnique = false, bool displayMsgIfNotUnique = false );
  43. ~CSingleAppInstance();
  44. bool CheckForOtherRunningInstances( bool exitOnNotUnique = false, bool displayMsgIfNotUnique = true );
  45. static bool CheckForRunningInstance( tchar* InstanceName );
  46. bool IsUniqueInstance() { return m_isUniqueInstance; }
  47. HANDLE GetHandle() { return reinterpret_cast< HANDLE >( (intp) m_hMutex ); }
  48. private:
  49. CSingleAppInstance(); // Hidden for a reason. You must specify the instance name
  50. #ifdef OSX
  51. char m_szLockPath[ MAX_PATH ];
  52. int m_hMutex;
  53. #else
  54. HANDLE m_hMutex;
  55. #endif
  56. bool m_isUniqueInstance;
  57. };
  58. #endif