wolInit.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. ** Command & Conquer Generals(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. // FILE: WOLinit.cpp //////////////////////////////////////////////////////
  19. // Westwood Online DLL/COM/ initialization/teardown
  20. // Author: Matthew D. Campbell, December 2001
  21. #ifndef WIN32_LEAN_AND_MEAN
  22. #define WIN32_LEAN_AND_MEAN
  23. #endif
  24. #include <snmp.h>
  25. #include <winreg.h>
  26. #include <atlbase.h>
  27. extern CComModule _Module; // Required for COM - must be between atlbase.h and atlcom.h. Funky, no?
  28. #include <atlcom.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include "wolSetup.h"
  32. #include "wolapi/wolapi.h"
  33. unsigned long g_wolapiRegistryVersion = 0;
  34. unsigned long g_wolapiRealVersion = 0;
  35. bool g_wolapiInstalled = false;
  36. char g_wolapiRegFilename[MAX_PATH];
  37. char g_wolapiRealFilename[MAX_PATH];
  38. char g_generalsFilename[MAX_PATH];
  39. char g_generalsSerial[1024];
  40. #define GENERALS_REG_KEY_TOP "HKEY_LOCAL_MACHINE" ///< Registry base
  41. #define GENERALS_REG_KEY_PATH "SOFTWARE\\Westwood\\Generals" ///< Generals registry key
  42. #define GENERALS_REG_KEY_BOTTOM GENERALS_REG_KEY_PATH "\\" ///< Generals registry key with trailing backslashes
  43. #define GENERALS_REG_KEY_VERSION "Version" ///< Version registry key
  44. #define GENERALS_REG_KEY_SKU "SKU" ///< SKU registry key
  45. #define GENERALS_REG_KEY_NAME "Name" ///< Product name registry key
  46. #define GENERALS_REG_KEY_INSTALLPATH "InstallPath" ///< Install path registry key
  47. #define GENERALS_REG_KEY_SERIAL "Serial" ///< Serial # registry key
  48. #define GENERALS_REG_KEY GENERALS_REG_KEY_TOP "\\" GENERALS_REG_KEY_BOTTOM ///< Full Generals registry path
  49. #define WOLAPI_REG_KEY_TOP "HKEY_LOCAL_MACHINE" ///< Registry base
  50. #define WOLAPI_REG_KEY_PATH "SOFTWARE\\Westwood\\WOLAPI" ///< WOLAPI registry key
  51. #define WOLAPI_REG_KEY_BOTTOM WOLAPI_REG_KEY_PATH "\\" ///< WOLAPI registry key with trailing backslashes
  52. #define WOLAPI_REG_KEY_VERSION "Version" ///< Version registry key
  53. #define WOLAPI_REG_KEY_INSTALLPATH "InstallPath" ///< Install path registry key
  54. #define WOLAPI_REG_KEY WOLAPI_REG_KEY_TOP "\\" WOLAPI_REG_KEY_BOTTOM ///< Full WOLAPI registry path
  55. #define DLL_REG_KEY_TOP "HKEY_CLASSES_ROOT" ///< Registry base
  56. #define DLL_REG_KEY_PATH "CLSID\\{18FD6763-F5EA-4fa5-B2A9-668554152FAE}\\InprocServer32" ///< WOLAPI registry key
  57. #define DLL_REG_KEY_BOTTOM DLL_REG_KEY_PATH "\\" ///< WOLAPI registry key with trailing backslashes
  58. #define DLL_REG_KEY_LOCATION "" ///< Version registry key
  59. void getPathsFromRegistry( void )
  60. {
  61. HKEY handle;
  62. unsigned long type;
  63. unsigned long size;
  64. int returnValue;
  65. size = sizeof(g_generalsFilename);
  66. strcpy(g_generalsFilename, "No install path in registry");
  67. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, GENERALS_REG_KEY_PATH, 0, KEY_ALL_ACCESS, &handle ) == ERROR_SUCCESS) {
  68. returnValue = RegQueryValueEx(handle, GENERALS_REG_KEY_INSTALLPATH, NULL, &type, (unsigned char *) &g_generalsFilename, &size);
  69. if (returnValue != ERROR_SUCCESS)
  70. {
  71. strcpy(g_generalsFilename, "No install path in registry");
  72. }
  73. RegCloseKey( handle );
  74. }
  75. size = sizeof(g_generalsSerial);
  76. strcpy(g_generalsSerial, "0");
  77. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, GENERALS_REG_KEY_PATH, 0, KEY_ALL_ACCESS, &handle ) == ERROR_SUCCESS) {
  78. returnValue = RegQueryValueEx(handle, GENERALS_REG_KEY_SERIAL, NULL, &type, (unsigned char *) &g_generalsSerial, &size);
  79. if (returnValue != ERROR_SUCCESS)
  80. {
  81. strcpy(g_generalsSerial, "0");
  82. }
  83. RegCloseKey( handle );
  84. }
  85. size = sizeof(g_wolapiRegFilename);
  86. strcpy(g_wolapiRegFilename, "No install path in registry");
  87. g_wolapiInstalled = true;
  88. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, WOLAPI_REG_KEY_PATH, 0, KEY_ALL_ACCESS, &handle ) == ERROR_SUCCESS) {
  89. returnValue = RegQueryValueEx(handle, WOLAPI_REG_KEY_INSTALLPATH, NULL, &type, (unsigned char *) &g_wolapiRegFilename, &size);
  90. if (returnValue != ERROR_SUCCESS)
  91. {
  92. strcpy(g_wolapiRegFilename, "No install path in registry");
  93. g_wolapiInstalled = false;
  94. }
  95. RegCloseKey( handle );
  96. }
  97. size = sizeof(g_wolapiRealFilename);
  98. strcpy(g_wolapiRealFilename, "No wolapi.dll installed");
  99. if (RegOpenKeyEx( HKEY_CLASSES_ROOT, DLL_REG_KEY_PATH, 0, KEY_ALL_ACCESS, &handle ) == ERROR_SUCCESS) {
  100. returnValue = RegQueryValueEx(handle, DLL_REG_KEY_LOCATION, NULL, &type, (unsigned char *) &g_wolapiRealFilename, &size);
  101. if (returnValue != ERROR_SUCCESS)
  102. {
  103. strcpy(g_wolapiRealFilename, "No wolapi.dll installed");
  104. g_wolapiInstalled = false;
  105. }
  106. RegCloseKey( handle );
  107. }
  108. }
  109. void setupGenerals( const char *genPath, const char *genSerial )
  110. {
  111. HKEY handle;
  112. unsigned long type;
  113. unsigned long returnValue;
  114. int size;
  115. if (RegCreateKeyEx( HKEY_LOCAL_MACHINE, GENERALS_REG_KEY_PATH, 0, "REG_NONE", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &handle, NULL ) == ERROR_SUCCESS) {
  116. type = REG_SZ;
  117. size = strlen(genPath)+1;
  118. returnValue = RegSetValueEx(handle, GENERALS_REG_KEY_INSTALLPATH, 0, type, (unsigned char *) genPath, size);
  119. size = strlen(genSerial)+1;
  120. returnValue = RegSetValueEx(handle, GENERALS_REG_KEY_SERIAL, 0, type, (unsigned char *) genSerial, size);
  121. size = strlen("Generals")+1;
  122. returnValue = RegSetValueEx(handle, GENERALS_REG_KEY_NAME, 0, type, (unsigned char *) "Generals", size);
  123. type = REG_DWORD;
  124. size = sizeof(DWORD);
  125. unsigned long value = 65536;
  126. returnValue = RegSetValueEx(handle, GENERALS_REG_KEY_VERSION, 0, type, (unsigned char *) &value, size);
  127. value = 12544;
  128. returnValue = RegSetValueEx(handle, GENERALS_REG_KEY_SKU, 0, type, (unsigned char *) &value, size);
  129. RegCloseKey( handle );
  130. }
  131. }
  132. /**
  133. * OLEInitializer class - Init and shutdown OLE & COM as a global
  134. * object. Scary, nasty stuff, COM. /me shivers.
  135. */
  136. class OLEInitializer
  137. {
  138. public:
  139. OLEInitializer() { OleInitialize(NULL); }
  140. ~OLEInitializer() { OleUninitialize(); }
  141. };
  142. OLEInitializer g_OLEInitializer;
  143. CComModule _Module;
  144. IChat *g_pChat = NULL;
  145. /**
  146. * checkInstalledWolapiVersion inits WOLAPI if possible and gets its version
  147. * number. It also saves off its install path from the registry.
  148. */
  149. void checkInstalledWolapiVersion( void )
  150. {
  151. // Initialize this instance
  152. _Module.Init(NULL, g_hInst);
  153. // Create the WOLAPI instance
  154. CoCreateInstance(CLSID_Chat, NULL, CLSCTX_INPROC_SERVER, \
  155. IID_IChat, (void**)&g_pChat);
  156. if (g_pChat)
  157. {
  158. // Grab versions
  159. g_pChat->GetVersion(&g_wolapiRealVersion);
  160. // Release everything
  161. g_pChat->Release();
  162. g_wolapiInstalled = true;
  163. }
  164. _Module.Term();
  165. // Grab path info from registry
  166. getPathsFromRegistry();
  167. return;
  168. }