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.

538 lines
12 KiB

  1. // MarshalableTI.cpp : Implementation of CMarshalableTI
  2. #include "precomp.h"
  3. #include "MarshalableTI.h"
  4. /////////////////////////////////////////////////////////////////////////////
  5. // CMarshalableTI methods
  6. /////////////////////////////////////////////////////////////////////////////
  7. HRESULT CMarshalableTI::FinalConstruct()
  8. {
  9. HRESULT hr = S_OK;
  10. m_bCreated = false;
  11. return hr;
  12. }
  13. /////////////////////////////////////////////////////////////////////////////
  14. // IMarshalableTI methods
  15. /////////////////////////////////////////////////////////////////////////////
  16. STDMETHODIMP CMarshalableTI::Create(REFIID clsid, REFIID iidLib, LCID lcid, WORD dwMajorVer, WORD dwMinorVer)
  17. {
  18. HRESULT hr = S_OK;
  19. if( !m_bCreated )
  20. {
  21. m_guid = clsid;
  22. m_libid = iidLib;
  23. m_lcid = lcid;
  24. m_TIHolder.m_pguid = &m_guid;
  25. m_TIHolder.m_plibid = &m_libid;
  26. m_TIHolder.m_wMajor = dwMajorVer;
  27. m_TIHolder.m_wMinor = dwMinorVer;
  28. m_TIHolder.m_pInfo = NULL;
  29. m_TIHolder.m_dwRef = 0;
  30. m_TIHolder.m_pMap = NULL;
  31. m_TIHolder.m_nCount = 0;
  32. }
  33. else
  34. {
  35. ATLASSERT(0);
  36. hr = E_UNEXPECTED;
  37. }
  38. return hr;
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // IMarshal Methods
  42. /////////////////////////////////////////////////////////////////////////////
  43. HRESULT CMarshalableTI::GetUnmarshalClass(
  44. /* [in] */ REFIID riid,
  45. /* [unique][in] */ void *pv,
  46. /* [in] */ DWORD dwDestContext,
  47. /* [unique][in] */ void *pvDestContext,
  48. /* [in] */ DWORD mshlflags,
  49. /* [out] */ CLSID *pCid)
  50. {
  51. HRESULT hr = S_OK;
  52. *pCid = CLSID_MarshalableTI;
  53. return hr;
  54. }
  55. HRESULT CMarshalableTI::GetMarshalSizeMax(
  56. /* [in] */ REFIID riid,
  57. /* [unique][in] */ void *pv,
  58. /* [in] */ DWORD dwDestContext,
  59. /* [unique][in] */ void *pvDestContext,
  60. /* [in] */ DWORD mshlflags,
  61. /* [out] */ DWORD *pSize)
  62. {
  63. *pSize = (2 * sizeof(GUID) + 2 * sizeof(ULONG) + sizeof(LCID) );
  64. return S_OK;
  65. }
  66. HRESULT CMarshalableTI::MarshalInterface(
  67. /* [unique][in] */ IStream *pStm,
  68. /* [in] */ REFIID riid,
  69. /* [unique][in] */ void *pv,
  70. /* [in] */ DWORD dwDestContext,
  71. /* [unique][in] */ void *pvDestContext,
  72. /* [in] */ DWORD mshlflags)
  73. {
  74. BYTE buf[(2 * sizeof(GUID) + 2 * sizeof(ULONG) + sizeof(LCID))];
  75. BYTE * pByte = buf;
  76. const GUID* pGuid = m_TIHolder.m_plibid;
  77. DWORD dwVer = m_TIHolder.m_wMajor;
  78. const DWORD* pDword = &dwVer;
  79. // Ugly because it is yanked from tested, shipped system code
  80. for (int i = 0; i < 2; i++) {
  81. DWORD dword = pGuid->Data1;
  82. *pByte++ = (BYTE)(dword);
  83. *pByte++ = (BYTE)(dword >> 8);
  84. *pByte++ = (BYTE)(dword >> 16);
  85. *pByte++ = (BYTE)(dword >> 24);
  86. WORD word = pGuid->Data2;
  87. *pByte++ = (BYTE)(word);
  88. *pByte++ = (BYTE)(word >> 8);
  89. word = pGuid->Data3;
  90. *pByte++ = (BYTE)(word);
  91. *pByte++ = (BYTE)(word >> 8);
  92. const BYTE* pData4 = pGuid->Data4;
  93. for (int j = 0; j < 8; j++) {
  94. *pByte++ = *pData4++;
  95. }
  96. dword = *pDword;
  97. *pByte++ = (BYTE)(dword);
  98. *pByte++ = (BYTE)(dword >> 8);
  99. *pByte++ = (BYTE)(dword >> 16);
  100. *pByte++ = (BYTE)(dword >> 24);
  101. pGuid = m_TIHolder.m_pguid;
  102. dwVer = m_TIHolder.m_wMinor;
  103. }
  104. *pByte++ = (BYTE)(m_lcid);
  105. *pByte++ = (BYTE)(m_lcid >> 8);
  106. *pByte++ = (BYTE)(m_lcid >> 16);
  107. *pByte++ = (BYTE)(m_lcid >> 24);
  108. HRESULT hr = pStm->Write(buf, sizeof(buf), NULL);
  109. return S_OK;
  110. }
  111. HRESULT CMarshalableTI::UnmarshalInterface(
  112. /* [unique][in] */ IStream *pStm,
  113. /* [in] */ REFIID riid,
  114. /* [out] */ void **ppv)
  115. {
  116. // Since we don't know the endian-ness of the other side,
  117. // we use a private wire format for custom marshaling here.
  118. //
  119. BYTE buf[(2 * sizeof(GUID) + 2 * sizeof(ULONG) + sizeof(LCID) )];
  120. HRESULT hr = S_OK;
  121. if( !m_bCreated )
  122. {
  123. *ppv = NULL;
  124. hr = pStm->Read(buf, sizeof(buf), NULL);
  125. if(SUCCEEDED(hr))
  126. {
  127. GUID guidTypeLib;
  128. GUID guidTypeInfo;
  129. LCID lcidTypeLib;
  130. ULONG verMajor;
  131. ULONG verMinor;
  132. BYTE * pByte;
  133. GUID * pGuid;
  134. DWORD * pDword;
  135. pByte = buf;
  136. pGuid = &guidTypeLib;
  137. pDword = &verMajor;
  138. int i;
  139. // Ugly because it is yanked from tested, shipped system code
  140. for (i = 0; i < 2; i++) {
  141. DWORD dword;
  142. WORD word;
  143. dword = (DWORD)(*pByte++);
  144. dword += (DWORD)(*pByte++ << 8);
  145. dword += (DWORD)(*pByte++ << 16);
  146. dword += (DWORD)(*pByte++ << 24);
  147. pGuid->Data1 = dword;
  148. word = (WORD)(*pByte++);
  149. word += (WORD)(*pByte++ << 8);
  150. pGuid->Data2 = word;
  151. word = (WORD)(*pByte++);
  152. word += (WORD)(*pByte++ << 8);
  153. pGuid->Data3 = word;
  154. BYTE * pData4 = pGuid->Data4;
  155. for (int j = 0; j < 8; j++) {
  156. *pData4++ = *pByte++;
  157. }
  158. dword = (DWORD)(*pByte++);
  159. dword += (DWORD)(*pByte++ << 8);
  160. dword += (DWORD)(*pByte++ << 16);
  161. dword += (DWORD)(*pByte++ << 24);
  162. *pDword = dword;
  163. pGuid = &guidTypeInfo;
  164. pDword = &verMinor;
  165. }
  166. lcidTypeLib = (DWORD)(*pByte++);
  167. lcidTypeLib += (DWORD)(*pByte++ << 8);
  168. lcidTypeLib += (DWORD)(*pByte++ << 16);
  169. lcidTypeLib += (DWORD)(*pByte++ << 24);
  170. hr = Create(guidTypeInfo, guidTypeLib,lcidTypeLib, static_cast<WORD>(verMajor), static_cast<WORD>(verMinor));
  171. }
  172. }
  173. if( SUCCEEDED(hr) )
  174. {
  175. hr = QueryInterface(riid, ppv);
  176. }
  177. return hr;
  178. }
  179. HRESULT CMarshalableTI::ReleaseMarshalData(
  180. /* [unique][in] */ IStream *pStm)
  181. {
  182. return S_OK;
  183. }
  184. HRESULT CMarshalableTI::DisconnectObject(
  185. /* [in] */ DWORD dwReserved)
  186. {
  187. return S_OK;
  188. }
  189. /////////////////////////////////////////////////////////////////////////////
  190. // ITypeInfo Methods
  191. /////////////////////////////////////////////////////////////////////////////
  192. HRESULT CMarshalableTI::GetTypeAttr(
  193. TYPEATTR ** ppTypeAttr
  194. )
  195. {
  196. HRESULT hr = S_OK;
  197. CComPtr<ITypeInfo> spTI;
  198. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  199. {
  200. hr = spTI->GetTypeAttr(ppTypeAttr);
  201. }
  202. return hr;
  203. }
  204. HRESULT CMarshalableTI:: GetTypeComp(
  205. ITypeComp ** ppTComp
  206. )
  207. {
  208. HRESULT hr = S_OK;
  209. CComPtr<ITypeInfo> spTI;
  210. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  211. {
  212. hr = spTI->GetTypeComp(ppTComp);
  213. }
  214. return hr;
  215. }
  216. HRESULT CMarshalableTI:: GetFuncDesc(
  217. UINT index,
  218. FUNCDESC ** ppFuncDesc
  219. )
  220. {
  221. HRESULT hr = S_OK;
  222. CComPtr<ITypeInfo> spTI;
  223. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  224. {
  225. hr = spTI->GetFuncDesc(index, ppFuncDesc);
  226. }
  227. return hr;
  228. }
  229. HRESULT CMarshalableTI:: GetVarDesc(
  230. UINT index,
  231. VARDESC ** ppVarDesc
  232. )
  233. {
  234. HRESULT hr = S_OK;
  235. CComPtr<ITypeInfo> spTI;
  236. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  237. {
  238. hr = spTI->GetVarDesc(index, ppVarDesc);
  239. }
  240. return hr;
  241. }
  242. HRESULT CMarshalableTI:: GetNames(
  243. MEMBERID memid,
  244. BSTR * rgBstrNames,
  245. UINT cMaxNames,
  246. UINT * pcNames
  247. )
  248. {
  249. HRESULT hr = S_OK;
  250. CComPtr<ITypeInfo> spTI;
  251. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  252. {
  253. hr = spTI->GetNames(memid, rgBstrNames, cMaxNames, pcNames);
  254. }
  255. return hr;
  256. }
  257. HRESULT CMarshalableTI:: GetRefTypeOfImplType(
  258. UINT index,
  259. HREFTYPE * pRefType
  260. )
  261. {
  262. HRESULT hr = S_OK;
  263. CComPtr<ITypeInfo> spTI;
  264. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  265. {
  266. hr = spTI->GetRefTypeOfImplType(index, pRefType);
  267. }
  268. return hr;
  269. }
  270. HRESULT CMarshalableTI:: GetImplTypeFlags(
  271. UINT index,
  272. INT * pImplTypeFlags
  273. )
  274. {
  275. HRESULT hr = S_OK;
  276. CComPtr<ITypeInfo> spTI;
  277. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  278. {
  279. hr = spTI->GetImplTypeFlags(index, pImplTypeFlags);
  280. }
  281. return hr;
  282. }
  283. HRESULT CMarshalableTI:: GetIDsOfNames(
  284. LPOLESTR * rgszNames,
  285. UINT cNames,
  286. MEMBERID * pMemId
  287. )
  288. {
  289. HRESULT hr = S_OK;
  290. CComPtr<ITypeInfo> spTI;
  291. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  292. {
  293. hr = spTI->GetIDsOfNames(rgszNames, cNames, pMemId);
  294. }
  295. return hr;
  296. }
  297. HRESULT CMarshalableTI:: Invoke(
  298. PVOID pvInstance,
  299. MEMBERID memid,
  300. WORD wFlags,
  301. DISPPARAMS * pDispParams,
  302. VARIANT * pVarResult,
  303. EXCEPINFO * pExcepInfo,
  304. UINT * puArgErr
  305. )
  306. {
  307. HRESULT hr = S_OK;
  308. CComPtr<ITypeInfo> spTI;
  309. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  310. {
  311. hr = spTI->Invoke(pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
  312. }
  313. return hr;
  314. }
  315. HRESULT CMarshalableTI:: GetDocumentation(
  316. MEMBERID memid,
  317. BSTR * pBstrName,
  318. BSTR * pBstrDocString,
  319. DWORD * pdwHelpContext,
  320. BSTR * pBstrHelpFile
  321. )
  322. {
  323. HRESULT hr = S_OK;
  324. CComPtr<ITypeInfo> spTI;
  325. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  326. {
  327. hr = spTI->GetDocumentation(memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
  328. }
  329. return hr;
  330. }
  331. HRESULT CMarshalableTI:: GetDllEntry(
  332. MEMBERID memid,
  333. INVOKEKIND invKind,
  334. BSTR * pBstrDllName,
  335. BSTR * pBstrName,
  336. WORD * pwOrdinal
  337. )
  338. {
  339. HRESULT hr = S_OK;
  340. CComPtr<ITypeInfo> spTI;
  341. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  342. {
  343. hr = spTI->GetDllEntry(memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
  344. }
  345. return hr;
  346. }
  347. HRESULT CMarshalableTI:: GetRefTypeInfo(
  348. HREFTYPE hRefType,
  349. ITypeInfo ** ppTInfo
  350. )
  351. {
  352. HRESULT hr = S_OK;
  353. CComPtr<ITypeInfo> spTI;
  354. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  355. {
  356. hr = spTI->GetRefTypeInfo(hRefType, ppTInfo);
  357. }
  358. return hr;
  359. }
  360. HRESULT CMarshalableTI:: AddressOfMember(
  361. MEMBERID memid,
  362. INVOKEKIND invKind,
  363. PVOID * ppv
  364. )
  365. {
  366. HRESULT hr = S_OK;
  367. CComPtr<ITypeInfo> spTI;
  368. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  369. {
  370. hr = spTI->AddressOfMember(memid, invKind, ppv);
  371. }
  372. return hr;
  373. }
  374. HRESULT CMarshalableTI:: CreateInstance(
  375. IUnknown * pUnkOuter,
  376. REFIID riid,
  377. PVOID * ppvObj
  378. )
  379. {
  380. HRESULT hr = S_OK;
  381. CComPtr<ITypeInfo> spTI;
  382. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  383. {
  384. hr = spTI->CreateInstance(pUnkOuter, riid, ppvObj );
  385. }
  386. return hr;
  387. }
  388. HRESULT CMarshalableTI:: GetMops(
  389. MEMBERID memid,
  390. BSTR * pBstrMops
  391. )
  392. {
  393. HRESULT hr = S_OK;
  394. CComPtr<ITypeInfo> spTI;
  395. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  396. {
  397. hr = spTI->GetMops(memid, pBstrMops);
  398. }
  399. return hr;
  400. }
  401. HRESULT CMarshalableTI:: GetContainingTypeLib(
  402. ITypeLib ** ppTLib,
  403. UINT * pIndex
  404. )
  405. {
  406. HRESULT hr = S_OK;
  407. CComPtr<ITypeInfo> spTI;
  408. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  409. {
  410. hr = spTI->GetContainingTypeLib(ppTLib, pIndex);
  411. }
  412. return hr;
  413. }
  414. void CMarshalableTI::ReleaseTypeAttr(
  415. TYPEATTR * pTypeAttr
  416. )
  417. {
  418. HRESULT hr = S_OK;
  419. CComPtr<ITypeInfo> spTI;
  420. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  421. {
  422. spTI->ReleaseTypeAttr(pTypeAttr);
  423. }
  424. }
  425. void CMarshalableTI::ReleaseFuncDesc(
  426. FUNCDESC * pFuncDesc
  427. )
  428. {
  429. HRESULT hr = S_OK;
  430. CComPtr<ITypeInfo> spTI;
  431. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  432. {
  433. spTI->ReleaseFuncDesc(pFuncDesc);
  434. }
  435. }
  436. void CMarshalableTI::ReleaseVarDesc(
  437. VARDESC * pVarDesc
  438. )
  439. {
  440. HRESULT hr = S_OK;
  441. CComPtr<ITypeInfo> spTI;
  442. if( SUCCEEDED( hr = _GetClassInfo(&spTI) ) )
  443. {
  444. spTI->ReleaseVarDesc(pVarDesc);
  445. }
  446. }
  447. HRESULT CMarshalableTI::_GetClassInfo(ITypeInfo** ppTI)
  448. {
  449. HRESULT hr = S_OK;
  450. hr = m_TIHolder.GetTI(m_lcid, ppTI);
  451. return hr;
  452. }