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.

236 lines
5.8 KiB

  1. #include "precomp.hpp"
  2. #include "combase.hpp"
  3. //
  4. // Create a new registry key and set its default value
  5. //
  6. BOOL
  7. SetRegKeyValue(
  8. HKEY parentKey,
  9. const WCHAR* keyname,
  10. const WCHAR* value,
  11. HKEY* retkey
  12. )
  13. {
  14. HKEY hkey;
  15. // Create or open the specified registry key
  16. if (RegCreateKeyEx(parentKey,
  17. keyname,
  18. 0,
  19. NULL,
  20. REG_OPTION_NON_VOLATILE,
  21. KEY_ALL_ACCESS,
  22. NULL,
  23. &hkey,
  24. NULL) != ERROR_SUCCESS)
  25. {
  26. return FALSE;
  27. }
  28. // Set the default value for the new key
  29. INT size = sizeof(WCHAR) * (wcslen(value)+1);
  30. if (RegSetValueEx(hkey,
  31. NULL,
  32. 0,
  33. REG_SZ,
  34. (const BYTE*) value,
  35. size) != ERROR_SUCCESS)
  36. {
  37. RegCloseKey(hkey);
  38. return FALSE;
  39. }
  40. // Check if the caller is interested in the handle to the new key
  41. if (retkey != NULL)
  42. *retkey = hkey;
  43. else
  44. RegCloseKey(hkey);
  45. return TRUE;
  46. }
  47. //
  48. // Delete a registry key and everything below it.
  49. //
  50. BOOL
  51. DeleteRegKey(
  52. HKEY parentKey,
  53. const WCHAR* keyname
  54. )
  55. {
  56. HKEY hkey;
  57. // Open the specified registry key
  58. if (RegOpenKeyEx(parentKey,
  59. keyname,
  60. 0,
  61. KEY_ALL_ACCESS,
  62. &hkey) != ERROR_SUCCESS)
  63. {
  64. return FALSE;
  65. }
  66. // Enumerate all subkeys
  67. WCHAR childname[256];
  68. DWORD childlen = 256;
  69. FILETIME filetime;
  70. while (RegEnumKeyEx(hkey,
  71. 0,
  72. childname,
  73. &childlen,
  74. NULL,
  75. NULL,
  76. NULL,
  77. &filetime) == ERROR_SUCCESS)
  78. {
  79. // Recursively delete subkeys
  80. if (!DeleteRegKey(hkey, childname))
  81. {
  82. RegCloseKey(hkey);
  83. return FALSE;
  84. }
  85. childlen = 256;
  86. }
  87. // Close the specified key and then delete it
  88. RegCloseKey(hkey);
  89. return RegDeleteKey(parentKey, keyname) == ERROR_SUCCESS;
  90. }
  91. //
  92. // Register or unregister a component depending on the value
  93. // of registerIt parameter:
  94. // TRUE = register it
  95. // FALSE = unregister it
  96. //
  97. HRESULT
  98. RegisterComponent(
  99. const ComponentRegData& regdata,
  100. BOOL registerIt
  101. )
  102. {
  103. static const WCHAR CLSID_KEYSTR[] = L"CLSID";
  104. static const WCHAR INPROCSERVER32_KEYSTR[] = L"InProcServer32";
  105. static const WCHAR PROGID_KEYSTR[] = L"ProgID";
  106. static const WCHAR PROGIDNOVER_KEYSTR[] = L"VersionIndependentProgID";
  107. static const WCHAR CURVER_KEYSTR[] = L"CurVer";
  108. // compose class ID string
  109. WCHAR clsidStr[64];
  110. StringFromGUID2(*regdata.clsid, clsidStr, 64);
  111. // open registry key HKEY_CLASSES_ROOT\CLSID
  112. BOOL success;
  113. HKEY clsidKey;
  114. if (RegOpenKeyEx(HKEY_CLASSES_ROOT,
  115. CLSID_KEYSTR,
  116. 0,
  117. KEY_ALL_ACCESS,
  118. &clsidKey) != ERROR_SUCCESS)
  119. {
  120. return E_FAIL;
  121. }
  122. if (registerIt)
  123. {
  124. // Register the component
  125. HKEY hkey;
  126. WCHAR fullpath[MAX_PATH];
  127. // HKEY_CLASSES_ROOT
  128. // <Version-independent ProgID> - component friendly name
  129. // CLSID - current version class ID
  130. // CurVer - current version ProgID
  131. if (!GetModuleFileName(globalInstanceHandle, fullpath, MAX_PATH) ||
  132. !SetRegKeyValue(HKEY_CLASSES_ROOT,
  133. regdata.progIDNoVer,
  134. regdata.compName,
  135. &hkey))
  136. {
  137. success = FALSE;
  138. goto regcompExit;
  139. }
  140. success = SetRegKeyValue(hkey, CLSID_KEYSTR, clsidStr, NULL)
  141. && SetRegKeyValue(hkey, CURVER_KEYSTR, regdata.progID, NULL);
  142. RegCloseKey(hkey);
  143. if (!success)
  144. goto regcompExit;
  145. // HKEY_CLASSES_ROOT
  146. // <ProgID> - friendly component name
  147. // CLSID - class ID
  148. if (!SetRegKeyValue(HKEY_CLASSES_ROOT,
  149. regdata.progID,
  150. regdata.compName,
  151. &hkey))
  152. {
  153. success = FALSE;
  154. goto regcompExit;
  155. }
  156. success = SetRegKeyValue(hkey, CLSID_KEYSTR, clsidStr, NULL);
  157. RegCloseKey(hkey);
  158. if (!success)
  159. goto regcompExit;
  160. // HKEY_CLASSES_ROOT
  161. // CLSID
  162. // <class ID> - friendly component name
  163. // InProcServer32 - full pathname to component DLL
  164. // ProgID - current version ProgID
  165. // VersionIndependentProgID - ...
  166. if (!SetRegKeyValue(clsidKey, clsidStr, regdata.compName, &hkey))
  167. {
  168. success = FALSE;
  169. goto regcompExit;
  170. }
  171. success = SetRegKeyValue(hkey, INPROCSERVER32_KEYSTR, fullpath, NULL)
  172. && SetRegKeyValue(hkey, PROGID_KEYSTR, regdata.progID, NULL)
  173. && SetRegKeyValue(hkey, PROGIDNOVER_KEYSTR, regdata.progIDNoVer, NULL);
  174. RegCloseKey(hkey);
  175. }
  176. else
  177. {
  178. // Unregister the component
  179. success = DeleteRegKey(clsidKey, clsidStr)
  180. && DeleteRegKey(HKEY_CLASSES_ROOT, regdata.progIDNoVer)
  181. && DeleteRegKey(HKEY_CLASSES_ROOT, regdata.progID);
  182. }
  183. regcompExit:
  184. RegCloseKey(clsidKey);
  185. return success ? S_OK : E_FAIL;
  186. }