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.

267 lines
6.1 KiB

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