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.

307 lines
7.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: C O M U T I L I T Y . C P P
  7. //
  8. // Contents: COM support classes and functions
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 17 Aug 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "ComUtility.h"
  18. HRESULT HrCoTaskMemAllocArray(long nNumber, long nElemSize, void ** ppv)
  19. {
  20. CHECK_POINTER(ppv);
  21. Assert(nNumber);
  22. Assert(nElemSize);
  23. HRESULT hr = S_OK;
  24. *ppv = CoTaskMemAlloc(nNumber * nElemSize);
  25. if(!*ppv)
  26. {
  27. hr = E_OUTOFMEMORY;
  28. }
  29. TraceHr(ttidError, FAL, hr, FALSE, "HrCoTaskMemAllocArray");
  30. return hr;
  31. }
  32. HRESULT HrCoTaskMemAllocString(long nChars, wchar_t ** psz)
  33. {
  34. CHECK_POINTER(psz);
  35. HRESULT hr = S_OK;
  36. *psz = reinterpret_cast<wchar_t*>(CoTaskMemAlloc(nChars * sizeof(wchar_t)));
  37. if(!*psz)
  38. {
  39. hr = E_OUTOFMEMORY;
  40. }
  41. TraceHr(ttidError, FAL, hr, FALSE, "HrCoTaskMemAllocString");
  42. return hr;
  43. }
  44. HRESULT HrCoTaskMemAllocString(wchar_t * sz, wchar_t ** psz)
  45. {
  46. CHECK_POINTER(sz);
  47. CHECK_POINTER(psz);
  48. HRESULT hr = HrCoTaskMemAllocString(lstrlen(sz) + 1, psz);
  49. if(SUCCEEDED(hr))
  50. {
  51. lstrcpy(*psz, sz);
  52. }
  53. TraceHr(ttidError, FAL, hr, FALSE, "HrCoTaskMemAllocString");
  54. return hr;
  55. }
  56. HRESULT HrCoCreateInstanceBase(
  57. REFCLSID clsid,
  58. DWORD dwClsContext,
  59. REFIID riid,
  60. void ** ppv)
  61. {
  62. return ::CoCreateInstance(
  63. clsid,
  64. NULL,
  65. dwClsContext,
  66. riid,
  67. ppv);
  68. }
  69. HRESULT HrCoCreateInstanceInprocBase(
  70. REFCLSID clsid,
  71. REFIID riid,
  72. void ** ppv)
  73. {
  74. return ::CoCreateInstance(
  75. clsid,
  76. NULL,
  77. CLSCTX_INPROC_SERVER,
  78. riid,
  79. ppv);
  80. }
  81. HRESULT HrCoCreateInstanceLocalBase(
  82. REFCLSID clsid,
  83. REFIID riid,
  84. void ** ppv)
  85. {
  86. return ::CoCreateInstance(
  87. clsid,
  88. NULL,
  89. CLSCTX_LOCAL_SERVER,
  90. riid,
  91. ppv);
  92. }
  93. HRESULT HrCoCreateInstanceServerBase(
  94. REFCLSID clsid,
  95. REFIID riid,
  96. void ** ppv)
  97. {
  98. return ::CoCreateInstance(
  99. clsid,
  100. NULL,
  101. CLSCTX_SERVER,
  102. riid,
  103. ppv);
  104. }
  105. HRESULT HrIsSameObject(const IUnknown * pUnk1, const IUnknown * pUnk2)
  106. {
  107. if(!pUnk1 || !pUnk2)
  108. {
  109. return E_POINTER;
  110. }
  111. IUnknown * pUnk1Temp = NULL;
  112. IUnknown * pUnk2Temp = NULL;
  113. HRESULT hr;
  114. hr = const_cast<IUnknown*>(pUnk1)->QueryInterface(&pUnk1Temp);
  115. if(FAILED(hr))
  116. {
  117. return hr;
  118. }
  119. hr = const_cast<IUnknown*>(pUnk2)->QueryInterface(&pUnk2Temp);
  120. if(SUCCEEDED(hr))
  121. {
  122. if(pUnk1Temp == pUnk2Temp)
  123. {
  124. hr = S_OK;
  125. }
  126. else
  127. {
  128. hr = S_FALSE;
  129. }
  130. ReleaseObj(pUnk2Temp);
  131. }
  132. ReleaseObj(pUnk1Temp);
  133. return hr;
  134. }
  135. HRESULT HrSetProxyBlanketHelper(
  136. IUnknown * pUnkProxy,
  137. DWORD dwAuthnLevel,
  138. DWORD dwImpLevel,
  139. DWORD dwCapabilities)
  140. {
  141. CHECK_POINTER(pUnkProxy);
  142. HRESULT hr = S_OK;
  143. DWORD dwAuthnSvc = 0;
  144. DWORD dwAuthzSvc = 0;
  145. wchar_t * szServerPrincName = NULL;
  146. RPC_AUTH_IDENTITY_HANDLE authHandle;
  147. hr = CoQueryProxyBlanket(pUnkProxy, &dwAuthnSvc, &dwAuthzSvc, &szServerPrincName,
  148. NULL, NULL, &authHandle, NULL);
  149. if(SUCCEEDED(hr))
  150. {
  151. hr = CoSetProxyBlanket(pUnkProxy, dwAuthnSvc, dwAuthzSvc, szServerPrincName,
  152. dwAuthnLevel, dwImpLevel, authHandle, dwCapabilities);
  153. if(szServerPrincName)
  154. {
  155. CoTaskMemFree(szServerPrincName);
  156. }
  157. }
  158. TraceHr(ttidError, FAL, hr, FALSE, "HrSetProxyBlanketHelper");
  159. return hr;
  160. }
  161. HRESULT HrSetProxyBlanket(
  162. IUnknown * pUnkProxy,
  163. DWORD dwAuthnLevel,
  164. DWORD dwImpLevel,
  165. DWORD dwCapabilities)
  166. {
  167. CHECK_POINTER(pUnkProxy);
  168. HRESULT hr = S_OK;
  169. hr = HrSetProxyBlanketHelper(pUnkProxy, dwAuthnLevel, dwImpLevel, dwCapabilities);
  170. if(SUCCEEDED(hr))
  171. {
  172. IUnknown * pUnk = NULL;
  173. hr = pUnkProxy->QueryInterface(&pUnk);
  174. if(SUCCEEDED(hr))
  175. {
  176. hr = HrSetProxyBlanketHelper(pUnk, dwAuthnLevel, dwImpLevel, dwCapabilities);
  177. pUnk->Release();
  178. }
  179. }
  180. TraceHr(ttidError, FAL, hr, FALSE, "HrSetProxyBlanket");
  181. return hr;
  182. }
  183. HRESULT HrEnableStaticCloakingHelper(IUnknown * pUnkProxy)
  184. {
  185. CHECK_POINTER(pUnkProxy);
  186. HRESULT hr = S_OK;
  187. DWORD dwAuthnLevel = 0;
  188. DWORD dwImpLevel = 0;
  189. DWORD dwCapabilities = 0;
  190. DWORD dwAuthnSvc = 0;
  191. DWORD dwAuthzSvc = 0;
  192. wchar_t * szServerPrincName = NULL;
  193. hr = CoQueryProxyBlanket(pUnkProxy, &dwAuthnSvc, &dwAuthzSvc, &szServerPrincName,
  194. &dwAuthnLevel, &dwImpLevel, NULL, &dwCapabilities);
  195. if(SUCCEEDED(hr))
  196. {
  197. dwCapabilities &= ~EOAC_DYNAMIC_CLOAKING;
  198. dwCapabilities |= EOAC_STATIC_CLOAKING;
  199. hr = CoSetProxyBlanket(pUnkProxy, dwAuthnSvc, dwAuthzSvc, szServerPrincName,
  200. dwAuthnLevel, dwImpLevel, NULL, dwCapabilities);
  201. if(szServerPrincName)
  202. {
  203. CoTaskMemFree(szServerPrincName);
  204. }
  205. }
  206. TraceHr(ttidError, FAL, hr, FALSE, "HrEnableStaticCloakingHelper");
  207. return hr;
  208. }
  209. HRESULT HrEnableStaticCloaking(IUnknown * pUnkProxy)
  210. {
  211. CHECK_POINTER(pUnkProxy);
  212. HRESULT hr = S_OK;
  213. hr = HrEnableStaticCloakingHelper(pUnkProxy);
  214. if(SUCCEEDED(hr))
  215. {
  216. IUnknown * pUnk = NULL;
  217. hr = pUnkProxy->QueryInterface(&pUnk);
  218. if(SUCCEEDED(hr))
  219. {
  220. hr = HrEnableStaticCloakingHelper(pUnk);
  221. pUnk->Release();
  222. }
  223. }
  224. TraceHr(ttidError, FAL, hr, FALSE, "HrEnableStaticCloaking");
  225. return hr;
  226. }
  227. HRESULT HrCopyProxyIdentityHelper(IUnknown * pUnkDest, IUnknown * pUnkSrc)
  228. {
  229. CHECK_POINTER(pUnkDest);
  230. CHECK_POINTER(pUnkSrc);
  231. HRESULT hr = S_OK;
  232. DWORD dwAuthnLevel = 0;
  233. DWORD dwImpLevel = 0;
  234. DWORD dwCapabilities = 0;
  235. DWORD dwAuthnSvc = 0;
  236. DWORD dwAuthzSvc = 0;
  237. wchar_t * szServerPrincName = NULL;
  238. hr = CoQueryProxyBlanket(pUnkDest, &dwAuthnSvc, &dwAuthzSvc, &szServerPrincName,
  239. &dwAuthnLevel, &dwImpLevel, NULL, &dwCapabilities);
  240. if(SUCCEEDED(hr))
  241. {
  242. RPC_AUTH_IDENTITY_HANDLE auth;
  243. hr = CoQueryProxyBlanket(pUnkSrc, NULL, NULL, NULL, NULL, NULL, &auth, NULL);
  244. if(SUCCEEDED(hr))
  245. {
  246. hr = CoSetProxyBlanket(pUnkDest, dwAuthnSvc, dwAuthzSvc, szServerPrincName,
  247. dwAuthnLevel, dwImpLevel, auth, dwCapabilities);
  248. if(szServerPrincName)
  249. {
  250. CoTaskMemFree(szServerPrincName);
  251. }
  252. }
  253. }
  254. TraceHr(ttidError, FAL, hr, FALSE, "HrCopyProxyIdentityHelper");
  255. return hr;
  256. }
  257. HRESULT HrCopyProxyIdentity(IUnknown * pUnkDest, IUnknown * pUnkSrc)
  258. {
  259. CHECK_POINTER(pUnkDest);
  260. CHECK_POINTER(pUnkSrc);
  261. HRESULT hr = S_OK;
  262. hr = HrCopyProxyIdentityHelper(pUnkDest, pUnkSrc);
  263. if(SUCCEEDED(hr))
  264. {
  265. IUnknown * pUnk = NULL;
  266. pUnkDest->QueryInterface(&pUnk);
  267. if(SUCCEEDED(hr))
  268. {
  269. hr = HrCopyProxyIdentityHelper(pUnk, pUnkSrc);
  270. pUnk->Release();
  271. }
  272. }
  273. TraceHr(ttidError, FAL, hr, FALSE, "HrCopyProxyIdentity");
  274. return hr;
  275. }