Leaked source code of windows server 2003
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.

304 lines
7.4 KiB

  1. //+----------------------------------------------------------------------------
  2. // File: dllreg.cxx
  3. //
  4. // Synopsis:
  5. //
  6. //-----------------------------------------------------------------------------
  7. // Includes -------------------------------------------------------------------
  8. #include <core.hxx>
  9. // Prototypes -----------------------------------------------------------------
  10. static const TCHAR * DeleteSubkeys(HKEY hkeyParent, const TCHAR * pszKey);
  11. static const TCHAR * RegisterKey(HKEY hkeyParent, const TCHAR * pszKey);
  12. //+----------------------------------------------------------------------------
  13. // Function: DllRegisterServer
  14. //
  15. // Synopsis:
  16. //
  17. //-----------------------------------------------------------------------------
  18. STDAPI
  19. DllRegisterServer()
  20. {
  21. const TCHAR ** ppszKey;
  22. const TCHAR * pszKey;
  23. HKEY hkey;
  24. LONG lError;
  25. HRESULT hr;
  26. hr = EnsureThreadState();
  27. if (hr)
  28. goto Cleanup;
  29. for (ppszKey=g_aszKeys; *ppszKey; ppszKey++)
  30. {
  31. pszKey = *ppszKey;
  32. lError = ::RegOpenKeyEx(HKEY_CLASSES_ROOT,
  33. pszKey, 0, KEY_ALL_ACCESS,
  34. &hkey);
  35. if (lError != ERROR_SUCCESS)
  36. {
  37. hr = E_FAIL;
  38. break;
  39. }
  40. pszKey += _tcslen(pszKey) + 1;
  41. Assert(*pszKey);
  42. if (!RegisterKey(hkey, pszKey))
  43. {
  44. hr = E_FAIL;
  45. break;
  46. }
  47. #ifdef _DEBUG
  48. Verify(::RegCloseKey(hkey) == ERROR_SUCCESS);
  49. #else
  50. ::RegCloseKey(hkey);
  51. #endif
  52. }
  53. Cleanup:
  54. return hr;
  55. }
  56. //+----------------------------------------------------------------------------
  57. // Function: DllUnregisterServer
  58. //
  59. // Synopsis:
  60. //
  61. //-----------------------------------------------------------------------------
  62. STDAPI
  63. DllUnregisterServer()
  64. {
  65. const TCHAR ** ppszKey;
  66. const TCHAR * pszKey;
  67. HKEY hkey;
  68. LONG lError;
  69. HRESULT hr;
  70. hr = EnsureThreadState();
  71. if (hr)
  72. goto Cleanup;
  73. for (ppszKey=g_aszKeys; *ppszKey; ppszKey++)
  74. {
  75. pszKey = *ppszKey;
  76. lError = ::RegOpenKeyEx(HKEY_CLASSES_ROOT,
  77. pszKey, 0, KEY_ALL_ACCESS,
  78. &hkey);
  79. if (lError != ERROR_SUCCESS)
  80. {
  81. hr = E_FAIL;
  82. break;
  83. }
  84. pszKey += _tcslen(pszKey) + 1;
  85. Assert(*pszKey);
  86. if (!DeleteSubkeys(hkey, pszKey) ||
  87. ::RegDeleteKey(hkey, pszKey) != ERROR_SUCCESS)
  88. {
  89. hr = E_FAIL;
  90. }
  91. #ifdef _DEBUG
  92. Verify(::RegCloseKey(hkey) == ERROR_SUCCESS);
  93. #else
  94. ::RegCloseKey(hkey);
  95. #endif
  96. if (hr)
  97. goto Cleanup;
  98. }
  99. Cleanup:
  100. return hr;
  101. }
  102. //+----------------------------------------------------------------------------
  103. // Function: DeleteSubkeys
  104. //
  105. // Synopsis:
  106. //
  107. //-----------------------------------------------------------------------------
  108. const TCHAR *
  109. DeleteSubkeys(
  110. HKEY hkeyParent,
  111. const TCHAR * pszKey)
  112. {
  113. const TCHAR * psz;
  114. HKEY hkey = NULL;
  115. LONG lError;
  116. lError = ::RegOpenKeyEx(hkeyParent, pszKey, 0, KEY_ALL_ACCESS, &hkey);
  117. if (lError != ERROR_SUCCESS)
  118. goto Error;
  119. pszKey += _tcslen(pszKey) + 1;
  120. while (*pszKey)
  121. {
  122. switch (*pszKey)
  123. {
  124. case chDEFAULT_SECTION:
  125. pszKey++;
  126. pszKey += _tcslen(pszKey) + 1;
  127. break;
  128. case chVALUES_SECTION:
  129. pszKey++;
  130. while (*pszKey)
  131. {
  132. pszKey += _tcslen(pszKey) + 1;
  133. pszKey += _tcslen(pszKey) + 1;
  134. }
  135. pszKey++;
  136. break;
  137. case chSUBKEY_SECTION:
  138. pszKey++;
  139. psz = pszKey;
  140. pszKey = DeleteSubkeys(hkey, pszKey);
  141. if (!pszKey)
  142. goto Error;
  143. lError = ::RegDeleteKey(hkey, psz);
  144. if (lError != ERROR_SUCCESS)
  145. goto Error;
  146. break;
  147. #ifdef _DEBUG
  148. default:
  149. AssertF("Invalid section in registry key data");
  150. #endif
  151. }
  152. }
  153. pszKey++;
  154. Cleanup:
  155. if (hkey)
  156. {
  157. #ifdef _DEBUG
  158. Verify(::RegCloseKey(hkey) == ERROR_SUCCESS);
  159. #else
  160. ::RegCloseKey(hkey);
  161. #endif
  162. }
  163. return pszKey;
  164. Error:
  165. pszKey = NULL;
  166. goto Cleanup;
  167. }
  168. //+----------------------------------------------------------------------------
  169. // Function: RegisterKey
  170. //
  171. // Synopsis:
  172. //
  173. //-----------------------------------------------------------------------------
  174. const TCHAR *
  175. RegisterKey(
  176. HKEY hkeyParent,
  177. const TCHAR * pszKey)
  178. {
  179. const TCHAR * psz;
  180. HKEY hkey = NULL;
  181. DWORD dwDisposition;
  182. LONG lError;
  183. lError = ::RegCreateKeyEx(hkeyParent, pszKey, 0, _T(""),
  184. REG_OPTION_NON_VOLATILE,
  185. KEY_ALL_ACCESS, NULL,
  186. &hkey, &dwDisposition);
  187. if (lError != ERROR_SUCCESS)
  188. goto Error;
  189. pszKey += _tcslen(pszKey) + 1;
  190. while (*pszKey)
  191. {
  192. switch (*pszKey)
  193. {
  194. case chDEFAULT_SECTION:
  195. pszKey++;
  196. if (!::lstrcmpi(pszKey, szMODULE_PATH))
  197. {
  198. TCHAR szModule[MAX_PATH+1];
  199. Verify(::GetModuleFileName((HMODULE)g_hinst, szModule, ARRAY_SIZE(szModule)));
  200. lError = ::RegSetValueEx(hkey, NULL, 0,
  201. REG_SZ,
  202. (const BYTE *)szModule,
  203. sizeof(TCHAR) * (_tcslen(szModule) + 1));
  204. }
  205. else
  206. {
  207. lError = ::RegSetValueEx(hkey, NULL, 0,
  208. REG_SZ,
  209. (const BYTE *)pszKey,
  210. sizeof(TCHAR) * (_tcslen(pszKey) + 1));
  211. }
  212. if (lError != ERROR_SUCCESS)
  213. goto Error;
  214. pszKey += _tcslen(pszKey) + 1;
  215. break;
  216. case chVALUES_SECTION:
  217. pszKey++;
  218. while (*pszKey)
  219. {
  220. psz = pszKey;
  221. pszKey += _tcslen(pszKey) + 1;
  222. lError = ::RegSetValueEx(hkey, psz, 0,
  223. REG_SZ,
  224. (const BYTE *)pszKey,
  225. sizeof(TCHAR) * (_tcslen(pszKey) + 1));
  226. if (lError != ERROR_SUCCESS)
  227. goto Error;
  228. pszKey += _tcslen(pszKey) + 1;
  229. }
  230. pszKey++;
  231. break;
  232. case chSUBKEY_SECTION:
  233. pszKey++;
  234. pszKey = RegisterKey(hkey, pszKey);
  235. if (!pszKey)
  236. goto Error;
  237. break;
  238. #ifdef _DEBUG
  239. default:
  240. AssertF("Invalid section in registry key data");
  241. #endif
  242. }
  243. }
  244. pszKey++;
  245. Cleanup:
  246. if (hkey)
  247. {
  248. #ifdef _DEBUG
  249. Verify(::RegCloseKey(hkey) == ERROR_SUCCESS);
  250. #else
  251. ::RegCloseKey(hkey);
  252. #endif
  253. }
  254. return pszKey;
  255. Error:
  256. pszKey = NULL;
  257. goto Cleanup;
  258. }