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.

227 lines
6.2 KiB

  1. /*****************************************************************************
  2. *
  3. * DIReg.c
  4. *
  5. * Copyright (c) 1996 Microsoft Corporation. All Rights Reserved.
  6. *
  7. * Abstract:
  8. *
  9. * OLE self-registration.
  10. *
  11. * Contents:
  12. *
  13. * DllRegisterServer()
  14. * DllUnregisterServer()
  15. *
  16. *****************************************************************************/
  17. #include "dinputpr.h"
  18. /*****************************************************************************
  19. *
  20. * The sqiffle for this file.
  21. *
  22. *****************************************************************************/
  23. #define sqfl sqflDll
  24. /*****************************************************************************
  25. *
  26. * RegSetStringEx
  27. *
  28. * Add a REG_SZ to hkey\sub::value.
  29. *
  30. *****************************************************************************/
  31. void INTERNAL
  32. RegSetStringEx(HKEY hk, LPCTSTR ptszValue, LPCTSTR ptszData)
  33. {
  34. LONG lRc = RegSetValueEx(hk, ptszValue, 0, REG_SZ,
  35. (PV)ptszData, cbCtch(lstrlen(ptszData)+1));
  36. }
  37. /*****************************************************************************
  38. *
  39. * RegDelStringEx
  40. *
  41. * Remove a REG_SZ from hkey\sub::value. The data is ignored.
  42. * It's passed so that RegDelStringEx matches the prototype for a
  43. * REGSTRINGACTION.
  44. *
  45. *****************************************************************************/
  46. void INTERNAL
  47. RegDelStringEx(HKEY hk, LPCTSTR ptszValue, LPCTSTR ptszData)
  48. {
  49. LONG lRc = RegDeleteValue(hk, ptszValue);
  50. }
  51. /*****************************************************************************
  52. *
  53. * RegCloseFinish
  54. *
  55. * Just close the subkey already.
  56. *
  57. *****************************************************************************/
  58. void INTERNAL
  59. RegCloseFinish(HKEY hk, LPCTSTR ptszSub, HKEY hkSub)
  60. {
  61. LONG lRc = RegCloseKey(hkSub);
  62. }
  63. /*****************************************************************************
  64. *
  65. * RegDelFinish
  66. *
  67. * Delete a key if there is nothing in it.
  68. *
  69. * OLE unregistration rules demand that you not delete a key if OLE
  70. * has added something to it.
  71. *
  72. *****************************************************************************/
  73. void INTERNAL
  74. RegDelFinish(HKEY hk, LPCTSTR ptszSub, HKEY hkSub)
  75. {
  76. LONG lRc;
  77. DWORD cKeys = 0, cValues = 0;
  78. RegQueryInfoKey(hkSub, 0, 0, 0, &cKeys, 0, 0, &cValues, 0, 0, 0, 0);
  79. RegCloseKey(hkSub);
  80. if ((cKeys | cValues) == 0) {
  81. if( fWinnt )
  82. lRc = DIWinnt_RegDeleteKey(hk, ptszSub);
  83. else
  84. lRc = RegDeleteKey(hk, ptszSub);
  85. } else {
  86. lRc = 0;
  87. }
  88. }
  89. /*****************************************************************************
  90. *
  91. * REGVTBL
  92. *
  93. * Functions for dorking with a registry key, either coming or going.
  94. *
  95. *****************************************************************************/
  96. typedef struct REGVTBL {
  97. /* How to create/open a key */
  98. LONG (INTERNAL *KeyAction)(HKEY hk, LPCTSTR ptszSub, PHKEY phkOut);
  99. /* How to create/delete a string */
  100. void (INTERNAL *StringAction)(HKEY hk, LPCTSTR ptszValue, LPCTSTR ptszData);
  101. /* How to finish using a key */
  102. void (INTERNAL *KeyFinish)(HKEY hk, LPCTSTR ptszSub, HKEY hkSub);
  103. } REGVTBL, *PREGVTBL;
  104. typedef const REGVTBL *PCREGVTBL;
  105. const REGVTBL c_vtblAdd = { RegCreateKey, RegSetStringEx, RegCloseFinish };
  106. const REGVTBL c_vtblDel = { RegOpenKey, RegDelStringEx, RegDelFinish };
  107. /*****************************************************************************
  108. *
  109. * @doc INTERNAL
  110. *
  111. * @func void | DllServerAction |
  112. *
  113. * Register or unregister our objects with OLE/COM/ActiveX/
  114. * whatever its name is.
  115. *
  116. *****************************************************************************/
  117. #pragma BEGIN_CONST_DATA
  118. extern const TCHAR c_tszNil[];
  119. #define ctchClsid ctchGuid
  120. const TCHAR c_tszClsidGuid[] =
  121. TEXT("CLSID\\{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}");
  122. const TCHAR c_tszInProcServer32[] = TEXT("InProcServer32");
  123. const TCHAR c_tszThreadingModel[] = TEXT("ThreadingModel");
  124. const TCHAR c_tszBoth[] = TEXT("Both");
  125. #pragma END_CONST_DATA
  126. void INTERNAL
  127. DllServerAction(PCREGVTBL pvtbl)
  128. {
  129. TCHAR tszThisDll[MAX_PATH];
  130. UINT iclsidmap;
  131. GetModuleFileName(g_hinst, tszThisDll, cA(tszThisDll));
  132. for (iclsidmap = 0; iclsidmap < cclsidmap; iclsidmap++) {
  133. TCHAR tszClsid[7+ctchClsid];
  134. HKEY hkClsid;
  135. HKEY hkSub;
  136. REFCLSID rclsid = c_rgclsidmap[iclsidmap].rclsid;
  137. wsprintf(tszClsid, c_tszClsidGuid,
  138. rclsid->Data1, rclsid->Data2, rclsid->Data3,
  139. rclsid->Data4[0], rclsid->Data4[1],
  140. rclsid->Data4[2], rclsid->Data4[3],
  141. rclsid->Data4[4], rclsid->Data4[5],
  142. rclsid->Data4[6], rclsid->Data4[7]);
  143. if (pvtbl->KeyAction(HKEY_CLASSES_ROOT, tszClsid, &hkClsid) == 0) {
  144. TCHAR tszName[127];
  145. /* Do the type name */
  146. LoadString(g_hinst, c_rgclsidmap[iclsidmap].ids,
  147. tszName, cA(tszName));
  148. pvtbl->StringAction(hkClsid, 0, tszName);
  149. /* Do the in-proc server name and threading model */
  150. if (pvtbl->KeyAction(hkClsid, c_tszInProcServer32, &hkSub) == 0) {
  151. pvtbl->StringAction(hkSub, 0, tszThisDll);
  152. pvtbl->StringAction(hkSub, c_tszThreadingModel, c_tszBoth);
  153. pvtbl->KeyFinish(hkClsid, c_tszInProcServer32, hkSub);
  154. }
  155. pvtbl->KeyFinish(HKEY_CLASSES_ROOT, tszClsid, hkClsid);
  156. }
  157. }
  158. }
  159. /*****************************************************************************
  160. *
  161. * @doc INTERNAL
  162. *
  163. * @func void | DllRegisterServer |
  164. *
  165. * Register our classes with OLE/COM/ActiveX/whatever its name is.
  166. *
  167. *****************************************************************************/
  168. void EXTERNAL
  169. DllRegisterServer(void)
  170. {
  171. DllServerAction(&c_vtblAdd);
  172. }
  173. /*****************************************************************************
  174. *
  175. * @doc INTERNAL
  176. *
  177. * @func void | DllUnregisterServer |
  178. *
  179. * Unregister our classes from OLE/COM/ActiveX/whatever its name is.
  180. *
  181. *****************************************************************************/
  182. void EXTERNAL
  183. DllUnregisterServer(void)
  184. {
  185. DllServerAction(&c_vtblDel);
  186. }