singletoninstancekeeper.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : commando *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/singletoninstancekeeper.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 11/29/01 1:05p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "singletoninstancekeeper.h"
  36. #include "wwdebug.h"
  37. #include "autostart.h"
  38. //
  39. // Disable 'unreferenced inline function has been removed'
  40. //
  41. #pragma warning(disable : 4514)
  42. //////////////////////////////////////////////////////////////////////
  43. // Local constants
  44. //////////////////////////////////////////////////////////////////////
  45. static const char * AUTOPLAY_GUID = "01AF9993-3492-11d3-8F6F-0060089C05B1";
  46. //////////////////////////////////////////////////////////////////////
  47. // Static member initialization
  48. //////////////////////////////////////////////////////////////////////
  49. const char * SingletonInstanceKeeperClass::APP_GUID = "C6D925A3-7A9B-4ca3-866D-8B4D506C3665";
  50. bool SingletonInstanceKeeperClass::AllowMultipleInstances = false;
  51. //////////////////////////////////////////////////////////////////////
  52. //
  53. // SingletonInstanceKeeperClass
  54. //
  55. //////////////////////////////////////////////////////////////////////
  56. SingletonInstanceKeeperClass::SingletonInstanceKeeperClass (void) :
  57. AppMutex (NULL),
  58. AutoPlayMutex (NULL)
  59. {
  60. if (AutoRestart.Get_Restart_Flag()) {
  61. AllowMultipleInstances = true;
  62. }
  63. return;
  64. }
  65. //////////////////////////////////////////////////////////////////////
  66. //
  67. // SingletonInstanceKeeperClass
  68. //
  69. //////////////////////////////////////////////////////////////////////
  70. SingletonInstanceKeeperClass::~SingletonInstanceKeeperClass (void)
  71. {
  72. if (AppMutex != NULL) {
  73. ::CloseHandle (AppMutex);
  74. }
  75. if (AutoPlayMutex != NULL) {
  76. ::CloseHandle (AutoPlayMutex);
  77. }
  78. return;
  79. }
  80. //////////////////////////////////////////////////////////////////////
  81. //
  82. // Verify_Safe_To_Execute
  83. //
  84. //////////////////////////////////////////////////////////////////////
  85. bool
  86. SingletonInstanceKeeperClass::Verify_Safe_To_Execute (void)
  87. {
  88. bool retval = false;
  89. //
  90. // Create a mutex with a unique name to Renegade in order to determine if
  91. // our app is already running.
  92. //
  93. // WARNING: DO NOT use this number for any other application except Renegade
  94. //
  95. AppMutex = ::CreateMutex (NULL, FALSE, APP_GUID);
  96. //
  97. // Is there already an instance of this app somewhere?
  98. //
  99. if (::GetLastError () == ERROR_ALREADY_EXISTS) {
  100. if (AllowMultipleInstances) {
  101. WWDEBUG_SAY (("Renegade is already running but AllowMultipleInstances is true\n"));
  102. retval = true;
  103. } else {
  104. //
  105. // Find the previous instance
  106. //
  107. HWND main_wnd = ::FindWindow (APP_GUID, NULL);
  108. if (main_wnd != NULL) {
  109. ::SetForegroundWindow (main_wnd);
  110. ::ShowWindow (main_wnd, SW_RESTORE);
  111. }
  112. WWDEBUG_SAY (("Renegade is already running...Bail!\n"));
  113. }
  114. } else {
  115. WWDEBUG_SAY (("Create AppMutex okay.\n"));
  116. //
  117. // Obtain the mutex unique to the Renegade AutoPlay application.
  118. //
  119. // WARNING: DO NOT use this number for any other application except Renegade AutoPlay
  120. //
  121. do
  122. {
  123. //
  124. // Attempt to open the mutex
  125. //
  126. AutoPlayMutex = ::OpenMutex (MUTEX_ALL_ACCESS, FALSE, AUTOPLAY_GUID);
  127. if (AutoPlayMutex != NULL) {
  128. WWDEBUG_SAY (("Waiting for Autoplay to quit!\n"));
  129. //
  130. // Wait for up to 30 seconds for the autoplay app to close
  131. //
  132. if (::WaitForSingleObject (AutoPlayMutex, 30000) == WAIT_FAILED) {
  133. WWDEBUG_SAY (("Failed waiting for AutoPlayMutex\n"));
  134. ::CloseHandle (AutoPlayMutex);
  135. AutoPlayMutex = NULL;
  136. }
  137. }
  138. //
  139. // Create a mutex with a name unique to the Renegade AutoPlay application.
  140. // This prevents the autoplay from running since it cannot get the mutex.
  141. // Renegade needs both of these mutexs before it is allowed to run.
  142. //
  143. if (AutoPlayMutex == NULL) {
  144. AutoPlayMutex = ::CreateMutex (NULL, FALSE, AUTOPLAY_GUID);
  145. if (::GetLastError () == ERROR_ALREADY_EXISTS) {
  146. ::CloseHandle (AutoPlayMutex);
  147. AutoPlayMutex = NULL;
  148. ::Sleep (2500);
  149. } else {
  150. WWDEBUG_SAY (("Create AutoPlayMutex.\n"));
  151. }
  152. }
  153. } while (AutoPlayMutex == NULL);
  154. WWDEBUG_SAY (("Got AutoPlayMutex okay.\n"));
  155. retval = true;
  156. }
  157. return retval;
  158. }
  159. //////////////////////////////////////////////////////////////////////
  160. //
  161. // Allow_Multiple_Instances
  162. //
  163. //////////////////////////////////////////////////////////////////////
  164. void SingletonInstanceKeeperClass::Allow_Multiple_Instances(bool flag)
  165. {
  166. AllowMultipleInstances = flag;
  167. }