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.

291 lines
8.0 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include "RunOnce.h"
  5. #include "Global.h"
  6. #include "RegistryKey.h"
  7. BOOL g_fRunOnceInitialized = FALSE;
  8. TCHAR g_strRunOnceFilename[MAX_PATH];
  9. //////////////////////////////////////////////////////////////////////////////////////////////
  10. //
  11. //////////////////////////////////////////////////////////////////////////////////////////////
  12. BOOL InitializeRunOnce(const BOOL fCleanStart)
  13. {
  14. CRegistryKey oRegistryKey;
  15. TCHAR strSystemPath[MAX_PATH];
  16. DWORD dwKeyDisposition;
  17. //
  18. // Only run this if g_fRunOnceInitialized is FALSE
  19. //
  20. if (!g_fRunOnceInitialized)
  21. {
  22. //
  23. // Where will we find the setup program
  24. //
  25. if (GetSystemDirectory(strSystemPath, MAX_PATH))
  26. {
  27. wsprintf(g_strRunOnceFilename, TEXT("%s\\WAMSetup.exe"), strSystemPath);
  28. if (fCleanStart)
  29. {
  30. //
  31. // Now we need to delete any old WAMSetup registry key if it exists
  32. //
  33. if (S_OK == oRegistryKey.CheckForExistingKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup")))
  34. {
  35. oRegistryKey.DeleteKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup"));
  36. }
  37. //
  38. // Ok, let's create the root registry key used to store setup information
  39. //
  40. if (S_OK == oRegistryKey.CreateKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup"), 0, KEY_ALL_ACCESS, &dwKeyDisposition))
  41. {
  42. if (S_OK == oRegistryKey.CreateKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup\\WithRegistration"), 0, KEY_ALL_ACCESS, &dwKeyDisposition))
  43. {
  44. if (S_OK == oRegistryKey.CreateKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup\\WithoutRegistration"), 0, KEY_ALL_ACCESS, &dwKeyDisposition))
  45. {
  46. g_fRunOnceInitialized = TRUE;
  47. }
  48. }
  49. }
  50. //
  51. // Close the Registry Key
  52. //
  53. oRegistryKey.CloseKey();
  54. }
  55. else
  56. {
  57. g_fRunOnceInitialized = TRUE;
  58. }
  59. }
  60. }
  61. return g_fRunOnceInitialized;
  62. }
  63. //////////////////////////////////////////////////////////////////////////////////////////////
  64. //
  65. //////////////////////////////////////////////////////////////////////////////////////////////
  66. BOOL FinalizeRunOnce(const BOOL fComplete)
  67. {
  68. BOOL fSuccess = FALSE;
  69. CRegistryKey oRegistryKey;
  70. DWORD dwBytesToWrite, dwKeyDisposition;
  71. TCHAR strRunOnceCmdLine[MAX_PATH];
  72. if (g_fRunOnceInitialized)
  73. {
  74. if (fComplete)
  75. {
  76. //
  77. // Create/Open the registry key
  78. //
  79. if (S_OK == oRegistryKey.CreateKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"), 0, KEY_ALL_ACCESS, &dwKeyDisposition))
  80. {
  81. //
  82. // What will be the run once command
  83. //
  84. wsprintf(strRunOnceCmdLine, TEXT("%s /Cleanup"), g_strRunOnceFilename);
  85. #ifdef _UNICODE
  86. dwBytesToWrite = (StrLen(strRunOnceCmdLine) + 1) * 2;
  87. #else
  88. dwBytesToWrite = StrLen(strRunOnceCmdLine) + 1;
  89. #endif // _UNICODE
  90. if (S_OK == oRegistryKey.SetValue(TEXT("WAMSetup"), REG_SZ, (LPBYTE) strRunOnceCmdLine, dwBytesToWrite))
  91. {
  92. fSuccess = TRUE;
  93. }
  94. //
  95. // Close the registry key
  96. //
  97. oRegistryKey.CloseKey();
  98. }
  99. }
  100. else
  101. {
  102. if (FileExists(g_strRunOnceFilename))
  103. {
  104. //
  105. // Make sure we can delete g_strRunOnceFilename on Reboot
  106. //
  107. SetFileAttributes(g_strRunOnceFilename, FILE_ATTRIBUTE_NORMAL);
  108. //
  109. // Create/Open the registry key
  110. //
  111. if (S_OK == oRegistryKey.CreateKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"), 0, KEY_ALL_ACCESS, &dwKeyDisposition))
  112. {
  113. //
  114. // What will be the run once command do
  115. //
  116. if (OS_VERSION_WIN9X & g_dwOSVersion)
  117. {
  118. wsprintf(strRunOnceCmdLine, TEXT("command /C del ""%s"""), g_strRunOnceFilename);
  119. }
  120. else
  121. {
  122. wsprintf(strRunOnceCmdLine, TEXT("cmd /C del /F ""%s"""), g_strRunOnceFilename);
  123. }
  124. #ifdef _UNICODE
  125. dwBytesToWrite = (StrLen(strRunOnceCmdLine) + 1) * 2;
  126. #else
  127. dwBytesToWrite = StrLen(strRunOnceCmdLine) + 1;
  128. #endif // _UNICODE
  129. if (S_OK == oRegistryKey.SetValue(TEXT("WAMSetup"), REG_SZ, (LPBYTE) strRunOnceCmdLine, dwBytesToWrite))
  130. {
  131. fSuccess = TRUE;
  132. }
  133. //
  134. // Close the registry key
  135. //
  136. oRegistryKey.CloseKey();
  137. }
  138. }
  139. //
  140. // Delete the WAMSetup registry key
  141. //
  142. if (S_OK == oRegistryKey.CheckForExistingKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup")))
  143. {
  144. oRegistryKey.DeleteKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup"));
  145. }
  146. fSuccess = TRUE;
  147. }
  148. //
  149. // Flag g_fRunOnceInitialized
  150. //
  151. g_fRunOnceInitialized = FALSE;
  152. }
  153. return fSuccess;
  154. }
  155. //////////////////////////////////////////////////////////////////////////////////////////////
  156. //
  157. //////////////////////////////////////////////////////////////////////////////////////////////
  158. BOOL SetRunOnceCleanupFile(LPCTSTR strSourceFilename, LPCTSTR strDestinationFilename, const BOOL fRegister)
  159. {
  160. BOOL fSuccess = FALSE;
  161. CRegistryKey oRegistryKey;
  162. TCHAR strRegistryKey[MAX_PATH];
  163. if (g_fRunOnceInitialized)
  164. {
  165. //
  166. // What is the target registry key
  167. //
  168. if (fRegister)
  169. {
  170. wsprintf(strRegistryKey, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup\\WithRegistration"));
  171. }
  172. else
  173. {
  174. wsprintf(strRegistryKey, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup\\WithoutRegistration"));
  175. }
  176. if (S_OK == oRegistryKey.OpenKey(HKEY_LOCAL_MACHINE, strRegistryKey, KEY_ALL_ACCESS))
  177. {
  178. //
  179. // Add a value to the registry. Because it is a REG_SZ value, we do not have to pass in
  180. // a dwDataLen parameter.
  181. //
  182. if (S_OK == oRegistryKey.SetValue(strSourceFilename, REG_SZ, (LPBYTE) strDestinationFilename, 0))
  183. {
  184. fSuccess = TRUE;
  185. }
  186. //
  187. // Close the registry key
  188. //
  189. oRegistryKey.CloseKey();
  190. }
  191. }
  192. return fSuccess;
  193. }
  194. //////////////////////////////////////////////////////////////////////////////////////////////
  195. //
  196. //////////////////////////////////////////////////////////////////////////////////////////////
  197. BOOL GetRunOnceCleanupFile(LPTSTR strSourceFilename, const DWORD dwSourceFilenameLen, LPTSTR strDestinationFilename, const DWORD dwDestinationFilenameLen, const BOOL fRegister)
  198. {
  199. BOOL fSuccess = FALSE;
  200. CRegistryKey oRegistryKey;
  201. DWORD dwSourceLen, dwDestinationLen, dwType;
  202. TCHAR strRegistryKey[MAX_PATH];
  203. if (g_fRunOnceInitialized)
  204. {
  205. //
  206. // What is the target registry key
  207. //
  208. if (fRegister)
  209. {
  210. wsprintf(strRegistryKey, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup\\WithRegistration"));
  211. }
  212. else
  213. {
  214. wsprintf(strRegistryKey, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WAMSetup\\WithoutRegistration"));
  215. }
  216. if (S_OK == oRegistryKey.OpenKey(HKEY_LOCAL_MACHINE, strRegistryKey, KEY_ALL_ACCESS))
  217. {
  218. //
  219. // Enumerate the first value in the registry key. Once this is done, delete it automatically
  220. //
  221. dwSourceLen = dwSourceFilenameLen;
  222. dwDestinationLen = dwDestinationFilenameLen;
  223. if (S_OK == oRegistryKey.EnumValues(0, strSourceFilename, &dwSourceLen, &dwType, (LPBYTE) strDestinationFilename, &dwDestinationLen))
  224. {
  225. if (S_OK == oRegistryKey.DeleteValue(strSourceFilename))
  226. {
  227. fSuccess = TRUE;
  228. }
  229. }
  230. //
  231. // Close the registry key
  232. //
  233. oRegistryKey.CloseKey();
  234. }
  235. }
  236. return fSuccess;
  237. }