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.

292 lines
6.8 KiB

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