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.

271 lines
6.1 KiB

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