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.

264 lines
5.8 KiB

  1. // Copyright (c) 1999 Microsoft Corporation
  2. // oledll.cpp
  3. //
  4. // Handle standard stuff for OLE server DLL
  5. //
  6. #include <objbase.h>
  7. #include <iostream.h>
  8. #include "oledll.h"
  9. static const char g_szCLSID[] = "CLSID";
  10. static const char g_szCLSIDSlash[] = "CLSID\\";
  11. static const char g_szInProc32[] = "InProcServer32";
  12. static const char g_szProgIDKey[] = "ProgID";
  13. static const char g_szVerIndProgIDKey[] = "VersionIndependentProgID";
  14. static const char g_szCurVer[] = "CurVer";
  15. static const char g_szThreadingModel[] = "ThreadingModel";
  16. static const char g_szBoth[] = "Both";
  17. static const int CLSID_STRING_SIZE = 39;
  18. static LONG RegSetDefValue(LPCSTR pstrKey, LPCSTR pstrSubkey, LPCSTR pstrValueName, LPCSTR pstrValue);
  19. static void RegRemoveSubtree(HKEY hk, LPCSTR pstrChild);
  20. STDAPI
  21. RegisterServer(HMODULE hModule,
  22. const CLSID &clsid,
  23. const char *szFriendlyName,
  24. const char *szVerIndProgID,
  25. const char *szProgID)
  26. {
  27. char szCLSID[CLSID_STRING_SIZE];
  28. HRESULT hr;
  29. LONG lr;
  30. hr = CLSIDToStr(clsid, szCLSID, sizeof(szCLSID));
  31. if (!SUCCEEDED(hr)) {
  32. return hr;
  33. }
  34. char szClsKey[256];
  35. strcpy(szClsKey, g_szCLSIDSlash);
  36. strcat(szClsKey, szCLSID);
  37. char szModule[512];
  38. lr = ::GetModuleFileName(hModule, szModule, sizeof(szModule));
  39. lr = 0;
  40. lr |= RegSetDefValue(szClsKey, NULL, NULL, szFriendlyName);
  41. lr |= RegSetDefValue(szClsKey, g_szInProc32, NULL, szModule);
  42. lr |= RegSetDefValue(szClsKey, g_szInProc32, g_szThreadingModel, g_szBoth);
  43. lr |= RegSetDefValue(szClsKey, g_szProgIDKey, NULL, szProgID);
  44. lr |= RegSetDefValue(szClsKey, g_szVerIndProgIDKey, NULL, szVerIndProgID);
  45. lr |= RegSetDefValue(szVerIndProgID, NULL, NULL, szFriendlyName);
  46. lr |= RegSetDefValue(szVerIndProgID, g_szCLSID, NULL, szCLSID);
  47. lr |= RegSetDefValue(szVerIndProgID, g_szCurVer, NULL, szProgID);
  48. lr |= RegSetDefValue(szProgID, NULL, NULL, szFriendlyName);
  49. lr |= RegSetDefValue(szProgID, g_szCLSID, NULL, szCLSID);
  50. #if 0
  51. if (lr) {
  52. UnregisterServer(clsid,
  53. szFriendlyName,
  54. szVerIndProgID,
  55. szProgID);
  56. // ???
  57. //
  58. return S_OK;
  59. }
  60. #endif
  61. return S_OK;
  62. }
  63. STDAPI
  64. UnregisterServer(const CLSID &clsid,
  65. const char *szFriendlyName,
  66. const char *szVerIndProgID,
  67. const char *szProgID)
  68. {
  69. char szCLSID[CLSID_STRING_SIZE];
  70. HRESULT hr;
  71. hr = CLSIDToStr(clsid, szCLSID, sizeof(szCLSID));
  72. if (!SUCCEEDED(hr)) {
  73. return hr;
  74. }
  75. char szClsKey[256];
  76. strcpy(szClsKey, g_szCLSIDSlash);
  77. strcat(szClsKey, szCLSID);
  78. RegRemoveSubtree(HKEY_CLASSES_ROOT, szClsKey);
  79. RegRemoveSubtree(HKEY_CLASSES_ROOT, szVerIndProgID);
  80. RegRemoveSubtree(HKEY_CLASSES_ROOT, szProgID);
  81. return S_OK;
  82. }
  83. BOOL
  84. GetCLSIDRegValue(const CLSID &clsid,
  85. const char *szKey,
  86. LPVOID pValue,
  87. LPDWORD pcbValue)
  88. {
  89. char szCLSID[CLSID_STRING_SIZE];
  90. HRESULT hr;
  91. HKEY hk;
  92. DWORD dw;
  93. hr = CLSIDToStr(clsid, szCLSID, sizeof(szCLSID));
  94. if (!SUCCEEDED(hr)) {
  95. return FALSE;
  96. }
  97. char szClsKey[256];
  98. strcpy(szClsKey, g_szCLSIDSlash);
  99. strcat(szClsKey, szCLSID);
  100. strcat(szClsKey, "\\");
  101. if (szKey)
  102. {
  103. strcat(szClsKey, szKey);
  104. }
  105. if (RegOpenKeyEx(HKEY_CLASSES_ROOT,
  106. szClsKey,
  107. 0,
  108. KEY_READ,
  109. &hk)) {
  110. return FALSE;
  111. }
  112. if (RegQueryValueEx(hk,
  113. NULL,
  114. NULL,
  115. &dw,
  116. (LPBYTE)pValue,
  117. pcbValue)) {
  118. RegCloseKey(hk);
  119. return FALSE;
  120. }
  121. RegCloseKey(hk);
  122. return TRUE;
  123. }
  124. HRESULT
  125. CLSIDToStr(const CLSID &clsid,
  126. char *szStr,
  127. int cbStr)
  128. {
  129. // XXX What to return here?
  130. //
  131. LPOLESTR wszCLSID = NULL;
  132. HRESULT hr = StringFromCLSID(clsid, &wszCLSID);
  133. if (!SUCCEEDED(hr)) {
  134. return hr;
  135. }
  136. // Covert from wide characters to non-wide.
  137. wcstombs(szStr, wszCLSID, cbStr);
  138. // Free memory.
  139. CoTaskMemFree(wszCLSID);
  140. return S_OK;
  141. }
  142. HRESULT
  143. StrToCLSID(char *szStr,
  144. CLSID &clsid,
  145. int cbStr)
  146. {
  147. WCHAR wsz[512];
  148. if (cbStr > 512)
  149. {
  150. cbStr = 512;
  151. }
  152. mbstowcs(wsz, szStr, cbStr);
  153. return CLSIDFromString(wsz, &clsid);
  154. }
  155. static LONG
  156. RegSetDefValue(LPCSTR pstrKey,
  157. LPCSTR pstrSubkey,
  158. LPCSTR pstrValueName,
  159. LPCSTR pstrValue)
  160. {
  161. HKEY hk;
  162. LONG lr;
  163. char sz[1024];
  164. LPCSTR pstr;
  165. if (!pstrSubkey) {
  166. pstr = pstrKey;
  167. } else {
  168. strcpy(sz, pstrKey);
  169. strcat(sz, "\\");
  170. strcat(sz, pstrSubkey);
  171. pstr = sz;
  172. }
  173. lr = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  174. pstr,
  175. 0,
  176. NULL,
  177. REG_OPTION_NON_VOLATILE,
  178. KEY_ALL_ACCESS,
  179. NULL,
  180. &hk,
  181. NULL);
  182. if (lr) {
  183. return lr;
  184. }
  185. lr = RegSetValueEx(hk,
  186. pstrValueName,
  187. 0,
  188. REG_SZ,
  189. (CONST BYTE*)pstrValue,
  190. 1+strlen(pstrValue));
  191. RegCloseKey(hk);
  192. return lr;
  193. }
  194. static void
  195. RegRemoveSubtree(HKEY hk,
  196. LPCSTR pstrChild)
  197. {
  198. LONG lResult;
  199. HKEY hkChild;
  200. lResult = RegOpenKeyEx(hk,
  201. pstrChild,
  202. 0,
  203. KEY_ALL_ACCESS,
  204. &hkChild);
  205. if (lResult) {
  206. return;
  207. }
  208. char szSubkey[256];
  209. // NOTE: Unlike regular enumeration, we always grab the 0th item
  210. // and delete it.
  211. //
  212. while (!RegEnumKey(hkChild, 0, szSubkey, sizeof(szSubkey))) {
  213. RegRemoveSubtree(hkChild, szSubkey);
  214. }
  215. RegCloseKey(hkChild);
  216. RegDeleteKey(hk, pstrChild);
  217. }