COMIP.H 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /***
  2. * comip.h - Native C++ compiler COM support - COM interface pointers header
  3. *
  4. * Copyright (C) 1996-1997 Microsoft Corporation
  5. * All rights reserved.
  6. *
  7. ****/
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif
  11. #if !defined(_INC_COMIP)
  12. #define _INC_COMIP
  13. #include <ole2.h>
  14. #include <malloc.h>
  15. #include <comutil.h>
  16. #pragma warning(push)
  17. #pragma warning(disable: 4290)
  18. class _com_error;
  19. void __stdcall _com_issue_error(HRESULT);
  20. struct __declspec(uuid("00000000-0000-0000-c000-000000000046")) IUnknown;
  21. // Provide Interface to IID association
  22. //
  23. template<typename _Interface, const IID* _IID /*= &__uuidof(_Interface)*/> class _com_IIID {
  24. public:
  25. typedef _Interface Interface;
  26. static _Interface* GetInterfacePtr() ALLEGIANCE_THROW0()
  27. {
  28. return NULL;
  29. }
  30. static _Interface& GetInterface() ALLEGIANCE_THROW0()
  31. {
  32. return *GetInterfacePtr();
  33. }
  34. static const IID& GetIID() ALLEGIANCE_THROW0()
  35. {
  36. return *_IID;
  37. }
  38. };
  39. template<typename _IIID> class _com_ptr_t {
  40. public:
  41. // Declare interface type so that the type may be available outside
  42. // the scope of this template.
  43. //
  44. typedef _IIID ThisIIID;
  45. typedef typename _IIID::Interface Interface;
  46. // When the compiler supports references in template parameters,
  47. // _CLSID will be changed to a reference. To avoid conversion
  48. // difficulties this function should be used to obtain the
  49. // CLSID.
  50. //
  51. static const IID& GetIID() ALLEGIANCE_THROW0()
  52. {
  53. return ThisIIID::GetIID();
  54. }
  55. // Constructs a smart-pointer from any interface pointer.
  56. //
  57. template<typename _InterfacePtr> _com_ptr_t(const _InterfacePtr& p) ALLEGIANCE_THROW(_com_error)
  58. : m_pInterface(NULL)
  59. {
  60. if (p) {
  61. HRESULT hr = _QueryInterface(p);
  62. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  63. _com_issue_error(hr);
  64. }
  65. }
  66. }
  67. // Disable conversion using _com_ptr_t* specialization of
  68. // template<typename _InterfacePtr> _com_ptr_t(const _InterfacePtr& p)
  69. template<> explicit _com_ptr_t(_com_ptr_t* const & p) ALLEGIANCE_THROW(_com_error)
  70. {
  71. if (p != NULL) {
  72. _com_issue_error(E_POINTER);
  73. }
  74. else {
  75. m_pInterface = p->m_pInterface;
  76. AddRef();
  77. }
  78. }
  79. // Default constructor.
  80. //
  81. _com_ptr_t() ALLEGIANCE_THROW0()
  82. : m_pInterface(NULL)
  83. {
  84. }
  85. // This constructor is provided to allow NULL assignment. It will issue
  86. // an error if any value other than null is assigned to the object.
  87. //
  88. _com_ptr_t(int null) ALLEGIANCE_THROW(_com_error)
  89. : m_pInterface(NULL)
  90. {
  91. if (null != 0) {
  92. _com_issue_error(E_POINTER);
  93. }
  94. }
  95. // Copy the pointer and AddRef().
  96. //
  97. template<> _com_ptr_t(const _com_ptr_t& cp) ALLEGIANCE_THROW0()
  98. : m_pInterface(cp.m_pInterface)
  99. {
  100. _AddRef();
  101. }
  102. // Saves the interface.
  103. //
  104. _com_ptr_t(Interface* pInterface) ALLEGIANCE_THROW0()
  105. : m_pInterface(pInterface)
  106. {
  107. _AddRef();
  108. }
  109. // Copies the pointer. If fAddRef is TRUE, the interface will
  110. // be AddRef()ed.
  111. //
  112. _com_ptr_t(Interface* pInterface, bool fAddRef) ALLEGIANCE_THROW0()
  113. : m_pInterface(pInterface)
  114. {
  115. if (fAddRef) {
  116. _AddRef();
  117. }
  118. }
  119. // Construct a pointer for a _variant_t object.
  120. //
  121. template<> _com_ptr_t(const _variant_t& varSrc) ALLEGIANCE_THROW(_com_error)
  122. : m_pInterface(NULL)
  123. {
  124. HRESULT hr = QueryStdInterfaces(varSrc);
  125. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  126. _com_issue_error(hr);
  127. }
  128. }
  129. // Calls CoCreateClass with the provided CLSID.
  130. //
  131. explicit _com_ptr_t(const CLSID& clsid, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) ALLEGIANCE_THROW(_com_error)
  132. : m_pInterface(NULL)
  133. {
  134. HRESULT hr = CreateInstance(clsid, pOuter, dwClsContext);
  135. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  136. _com_issue_error(hr);
  137. }
  138. }
  139. // Calls CoCreateClass with the provided CLSID retrieved from
  140. // the string.
  141. //
  142. explicit _com_ptr_t(LPOLESTR str, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) ALLEGIANCE_THROW(_com_error)
  143. : m_pInterface(NULL)
  144. {
  145. HRESULT hr = CreateInstance(str, pOuter, dwClsContext);
  146. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  147. _com_issue_error(hr);
  148. }
  149. }
  150. // Calls CoCreateClass with the provided SBCS CLSID retrieved from
  151. // the string.
  152. //
  153. explicit _com_ptr_t(LPCSTR str, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) ALLEGIANCE_THROW(_com_error)
  154. : m_pInterface(NULL)
  155. {
  156. HRESULT hr = CreateInstance(str, pOuter, dwClsContext);
  157. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  158. _com_issue_error(hr);
  159. }
  160. }
  161. // Queries for interface.
  162. //
  163. template<typename _InterfacePtr> _com_ptr_t& operator=(const _InterfacePtr& p) ALLEGIANCE_THROW(_com_error)
  164. {
  165. HRESULT hr = _QueryInterface(p);
  166. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  167. _com_issue_error(hr);
  168. }
  169. return *this;
  170. }
  171. // Saves the interface.
  172. //
  173. _com_ptr_t& operator=(Interface* pInterface) ALLEGIANCE_THROW0()
  174. {
  175. if (m_pInterface != pInterface) {
  176. Interface* pOldInterface = m_pInterface;
  177. m_pInterface = pInterface;
  178. _AddRef();
  179. if (pOldInterface != NULL) {
  180. pOldInterface->Release();
  181. }
  182. }
  183. return *this;
  184. }
  185. // Copies and AddRef()'s the interface.
  186. //
  187. template<> _com_ptr_t& operator=(const _com_ptr_t& cp) ALLEGIANCE_THROW0()
  188. {
  189. return operator=(cp.m_pInterface);
  190. }
  191. // This operator is provided to permit the assignment of NULL to the class.
  192. // It will issue an error if any value other than NULL is assigned to it.
  193. //
  194. _com_ptr_t& operator=(int null) ALLEGIANCE_THROW(_com_error)
  195. {
  196. if (null != 0) {
  197. _com_issue_error(E_POINTER);
  198. }
  199. return operator=(reinterpret_cast<Interface*>(NULL));
  200. }
  201. // Construct a pointer for a _variant_t object.
  202. //
  203. template<> _com_ptr_t& operator=(const _variant_t& varSrc) ALLEGIANCE_THROW(_com_error)
  204. {
  205. HRESULT hr = QueryStdInterfaces(varSrc);
  206. if (FAILED(hr) && (hr != E_NOINTERFACE)) {
  207. _com_issue_error(hr);
  208. }
  209. return *this;
  210. }
  211. // If we still have an interface then Release() it. The interface
  212. // may be NULL if Detach() has previously been called, or if it was
  213. // never set.
  214. //
  215. ~_com_ptr_t() ALLEGIANCE_THROW0()
  216. {
  217. _Release();
  218. }
  219. // Saves/sets the interface without AddRef()ing. This call
  220. // will release any previously acquired interface.
  221. //
  222. void Attach(Interface* pInterface) ALLEGIANCE_THROW0()
  223. {
  224. _Release();
  225. m_pInterface = pInterface;
  226. }
  227. // Saves/sets the interface only AddRef()ing if fAddRef is TRUE.
  228. // This call will release any previously acquired interface.
  229. //
  230. void Attach(Interface* pInterface, bool fAddRef) ALLEGIANCE_THROW0()
  231. {
  232. _Release();
  233. m_pInterface = pInterface;
  234. if (fAddRef) {
  235. if (pInterface != NULL) {
  236. pInterface->AddRef();
  237. }
  238. }
  239. }
  240. // Simply NULL the interface pointer so that it isn't Released()'ed.
  241. //
  242. Interface* Detach() ALLEGIANCE_THROW0()
  243. {
  244. Interface* const old=m_pInterface;
  245. m_pInterface = NULL;
  246. return old;
  247. }
  248. // Return the interface. This value may be NULL.
  249. //
  250. operator Interface*() const ALLEGIANCE_THROW0()
  251. {
  252. return m_pInterface;
  253. }
  254. // Queries for the unknown and return it
  255. // Provides minimal level error checking before use.
  256. //
  257. operator Interface&() const ALLEGIANCE_THROW(_com_error)
  258. {
  259. if (m_pInterface == NULL) {
  260. _com_issue_error(E_POINTER);
  261. }
  262. return *m_pInterface;
  263. }
  264. // Allows an instance of this class to act as though it were the
  265. // actual interface. Also provides minimal error checking.
  266. //
  267. Interface& operator*() const ALLEGIANCE_THROW(_com_error)
  268. {
  269. if (m_pInterface == NULL) {
  270. _com_issue_error(E_POINTER);
  271. }
  272. return *m_pInterface;
  273. }
  274. // Returns the address of the interface pointer contained in this
  275. // class. This is useful when using the COM/OLE interfaces to create
  276. // this interface.
  277. //
  278. Interface** operator&() ALLEGIANCE_THROW0()
  279. {
  280. _Release();
  281. m_pInterface = NULL;
  282. return &m_pInterface;
  283. }
  284. // Allows this class to be used as the interface itself.
  285. // Also provides simple error checking.
  286. //
  287. Interface* operator->() const ALLEGIANCE_THROW(_com_error)
  288. {
  289. if (m_pInterface == NULL) {
  290. _com_issue_error(E_POINTER);
  291. }
  292. return m_pInterface;
  293. }
  294. // This operator is provided so that simple boolean expressions will
  295. // work. For example: "if (p) ...".
  296. // Returns TRUE if the pointer is not NULL.
  297. //
  298. operator bool() const ALLEGIANCE_THROW0()
  299. {
  300. return m_pInterface != NULL;
  301. }
  302. // Compare two pointers
  303. //
  304. template<typename _InterfacePtr> bool operator==(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  305. {
  306. return _CompareUnknown(p) == 0;
  307. }
  308. // Compare with other interface
  309. //
  310. template<> bool operator==(Interface* p) ALLEGIANCE_THROW(_com_error)
  311. {
  312. return (m_pInterface == p) ? true : _CompareUnknown(p) == 0;
  313. }
  314. // Compares 2 _com_ptr_t's
  315. //
  316. template<> bool operator==(_com_ptr_t& p) ALLEGIANCE_THROW0()
  317. {
  318. return operator==(p.m_pInterface);
  319. }
  320. // For comparison to NULL
  321. //
  322. template<> bool operator==(int null) ALLEGIANCE_THROW(_com_error)
  323. {
  324. if (null != 0) {
  325. _com_issue_error(E_POINTER);
  326. }
  327. return m_pInterface == NULL;
  328. }
  329. // Compare two pointers
  330. //
  331. template<typename _InterfacePtr> bool operator!=(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  332. {
  333. return !(operator==(p));
  334. }
  335. // Compare with other interface
  336. //
  337. template<> bool operator!=(Interface* p) ALLEGIANCE_THROW(_com_error)
  338. {
  339. return !(operator==(p));
  340. }
  341. // Compares 2 _com_ptr_t's
  342. //
  343. template<> bool operator!=(_com_ptr_t& p) ALLEGIANCE_THROW(_com_error)
  344. {
  345. return !(operator==(p));
  346. }
  347. // For comparison to NULL
  348. //
  349. template<> bool operator!=(int null) ALLEGIANCE_THROW(_com_error)
  350. {
  351. return !(operator==(null));
  352. }
  353. // Compare two pointers
  354. //
  355. template<typename _InterfacePtr> bool operator<(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  356. {
  357. return _CompareUnknown(p) < 0;
  358. }
  359. // Compare two pointers
  360. //
  361. template<typename _InterfacePtr> bool operator>(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  362. {
  363. return _CompareUnknown(p) > 0;
  364. }
  365. // Compare two pointers
  366. //
  367. template<typename _InterfacePtr> bool operator<=(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  368. {
  369. return _CompareUnknown(p) <= 0;
  370. }
  371. // Compare two pointers
  372. //
  373. template<typename _InterfacePtr> bool operator>=(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  374. {
  375. return _CompareUnknown(p) >= 0;
  376. }
  377. // Provides error-checking Release()ing of this interface.
  378. //
  379. void Release() ALLEGIANCE_THROW(_com_error)
  380. {
  381. if (m_pInterface == NULL) {
  382. _com_issue_error(E_POINTER);
  383. }
  384. m_pInterface->Release();
  385. m_pInterface = NULL;
  386. }
  387. // Provides error-checking AddRef()ing of this interface.
  388. //
  389. void AddRef() ALLEGIANCE_THROW(_com_error)
  390. {
  391. if (m_pInterface == NULL) {
  392. _com_issue_error(E_POINTER);
  393. }
  394. m_pInterface->AddRef();
  395. }
  396. // Another way to get the interface pointer without casting.
  397. //
  398. Interface* GetInterfacePtr() const ALLEGIANCE_THROW0()
  399. {
  400. return m_pInterface;
  401. }
  402. // Loads an interface for the provided CLSID.
  403. // Returns an HRESULT. Any previous interface is released.
  404. //
  405. HRESULT CreateInstance(const CLSID& rclsid, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) ALLEGIANCE_THROW0()
  406. {
  407. HRESULT hr;
  408. _Release();
  409. if (dwClsContext & (CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER)) {
  410. IUnknown* pIUnknown;
  411. hr = CoCreateInstance(rclsid, pOuter, dwClsContext, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown));
  412. if (FAILED(hr)) {
  413. return hr;
  414. }
  415. hr = OleRun(pIUnknown);
  416. if (SUCCEEDED(hr)) {
  417. hr = pIUnknown->QueryInterface(GetIID(), reinterpret_cast<void**>(&m_pInterface));
  418. }
  419. pIUnknown->Release();
  420. }
  421. else {
  422. hr = CoCreateInstance(rclsid, pOuter, dwClsContext, GetIID(), reinterpret_cast<void**>(&m_pInterface));
  423. }
  424. return hr;
  425. }
  426. // Creates the class specified by clsidString. clsidString may
  427. // contain a class id, or a prog id string.
  428. //
  429. HRESULT CreateInstance(LPOLESTR clsidString, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) ALLEGIANCE_THROW0()
  430. {
  431. if (clsidString == NULL) {
  432. return E_INVALIDARG;
  433. }
  434. CLSID clsid;
  435. HRESULT hr;
  436. if (clsidString[0] == '{') {
  437. hr = CLSIDFromString(clsidString, &clsid);
  438. }
  439. else {
  440. hr = CLSIDFromProgID(clsidString, &clsid);
  441. }
  442. if (FAILED(hr)) {
  443. return hr;
  444. }
  445. return CreateInstance(clsid, pOuter, dwClsContext);
  446. }
  447. // Creates the class specified by SBCS clsidString. clsidString may
  448. // contain a class id, or a prog id string.
  449. //
  450. HRESULT CreateInstance(LPCSTR clsidStringA, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) ALLEGIANCE_THROW0()
  451. {
  452. if (clsidStringA == NULL) {
  453. return E_INVALIDARG;
  454. }
  455. int size = lstrlenA(clsidStringA) + 1;
  456. LPOLESTR clsidStringW = static_cast<LPOLESTR>(_alloca(size * 2));
  457. clsidStringW[0] = '\0';
  458. if (MultiByteToWideChar(CP_ACP, 0, clsidStringA, -1, clsidStringW, size) == 0) {
  459. return HRESULT_FROM_WIN32(GetLastError());
  460. }
  461. return CreateInstance(clsidStringW, pOuter, dwClsContext);
  462. }
  463. // Attach to the active object specified by rclsid.
  464. // Any previous interface is released.
  465. //
  466. HRESULT GetActiveObject(const CLSID& rclsid) ALLEGIANCE_THROW0()
  467. {
  468. _Release();
  469. IUnknown* pIUnknown;
  470. HRESULT hr = ::GetActiveObject(rclsid, NULL, &pIUnknown);
  471. if (FAILED(hr)) {
  472. return hr;
  473. }
  474. hr = pIUnknown->QueryInterface(GetIID(), reinterpret_cast<void**>(&m_pInterface));
  475. if (FAILED(hr)) {
  476. return hr;
  477. }
  478. pIUnknown->Release();
  479. return hr;
  480. }
  481. // Attach to the active object specified by clsidString.
  482. // First convert the LPOLESTR to a CLSID.
  483. //
  484. HRESULT GetActiveObject(LPOLESTR clsidString) ALLEGIANCE_THROW0()
  485. {
  486. if (clsidString == NULL) {
  487. return E_INVALIDARG;
  488. }
  489. CLSID clsid;
  490. HRESULT hr;
  491. if (clsidString[0] == '{') {
  492. hr = CLSIDFromString(clsidString, &clsid);
  493. }
  494. else {
  495. hr = CLSIDFromProgID(clsidString, &clsid);
  496. }
  497. if (FAILED(hr)) {
  498. return hr;
  499. }
  500. return GetActiveObject(clsid);
  501. }
  502. // Attach to the active object specified by clsidStringA.
  503. // First convert the LPCSTR to a LPOLESTR.
  504. //
  505. HRESULT GetActiveObject(LPCSTR clsidStringA) ALLEGIANCE_THROW0()
  506. {
  507. if (clsidStringA == NULL) {
  508. return E_INVALIDARG;
  509. }
  510. int size = lstrlenA(clsidStringA) + 1;
  511. LPOLESTR clsidStringW = static_cast<LPOLESTR>(_alloca(size * 2));
  512. clsidStringW[0] = '\0';
  513. if (MultiByteToWideChar(CP_ACP, 0, clsidStringA, -1, clsidStringW, size) == 0) {
  514. return HRESULT_FROM_WIN32(GetLastError());
  515. }
  516. return GetActiveObject(clsidStringW);
  517. }
  518. // Performs the QI for the specified IID and returns it in p.
  519. // As with all QIs, the interface will be AddRef'd.
  520. //
  521. template<typename _InterfaceType> HRESULT QueryInterface(const IID& iid, _InterfaceType*& p) ALLEGIANCE_THROW0()
  522. {
  523. if (m_pInterface != NULL) {
  524. return m_pInterface->QueryInterface(iid, reinterpret_cast<void**>(&p));
  525. }
  526. return E_POINTER;
  527. }
  528. // Performs the QI for the specified IID and returns it in p.
  529. // As with all QIs, the interface will be AddRef'd.
  530. //
  531. template<typename _InterfaceType> HRESULT QueryInterface(const IID& iid, _InterfaceType** p) ALLEGIANCE_THROW0()
  532. {
  533. return QueryInterface(iid, *p);
  534. }
  535. private:
  536. // The Interface.
  537. //
  538. Interface* m_pInterface;
  539. // Releases only if the interface is not null.
  540. // The interface is not set to NULL.
  541. //
  542. void _Release() ALLEGIANCE_THROW0()
  543. {
  544. if (m_pInterface != NULL) {
  545. m_pInterface->Release();
  546. }
  547. }
  548. // AddRefs only if the interface is not NULL
  549. //
  550. void _AddRef() ALLEGIANCE_THROW0()
  551. {
  552. if (m_pInterface != NULL) {
  553. m_pInterface->AddRef();
  554. }
  555. }
  556. // Performs a QI on pUnknown for the interface type returned
  557. // for this class. The interface is stored. If pUnknown is
  558. // NULL, or the QI fails, E_NOINTERFACE is returned and
  559. // _pInterface is set to NULL.
  560. //
  561. template<typename _InterfacePtr> HRESULT _QueryInterface(const _InterfacePtr& p) ALLEGIANCE_THROW0()
  562. {
  563. HRESULT hr;
  564. // Can't QI NULL
  565. //
  566. if (p) {
  567. // Query for this interface
  568. //
  569. Interface* pInterface;
  570. hr = p->QueryInterface(GetIID(), reinterpret_cast<void**>(&pInterface));
  571. if (FAILED(hr)) {
  572. // If failed initialize interface to NULL and return HRESULT.
  573. //
  574. Attach(NULL);
  575. return hr;
  576. }
  577. // Save the interface without AddRef()ing.
  578. //
  579. Attach(pInterface);
  580. }
  581. else {
  582. operator=(static_cast<Interface*>(NULL));
  583. hr = E_NOINTERFACE;
  584. }
  585. return hr;
  586. }
  587. // Compares the provided pointer with this by obtaining IUnknown interfaces
  588. // for each pointer and then returning the difference.
  589. //
  590. template<typename _InterfacePtr> int _CompareUnknown(_InterfacePtr p) ALLEGIANCE_THROW(_com_error)
  591. {
  592. IUnknown* pu1, *pu2;
  593. if (m_pInterface != NULL) {
  594. HRESULT hr = m_pInterface->QueryInterface(__uuidof(IUnknown), reinterpret_cast<void**>(&pu1));
  595. if (FAILED(hr)) {
  596. _com_issue_error(hr);
  597. }
  598. pu1->Release();
  599. }
  600. else {
  601. pu1 = NULL;
  602. }
  603. if (p) {
  604. HRESULT hr = p->QueryInterface(__uuidof(IUnknown), reinterpret_cast<void**>(&pu2));
  605. if (FAILED(hr)) {
  606. _com_issue_error(hr);
  607. }
  608. pu2->Release();
  609. }
  610. else {
  611. pu2 = NULL;
  612. }
  613. return pu1 - pu2;
  614. }
  615. // Try to extract either IDispatch* or an IUnknown* from
  616. // the VARIANT
  617. //
  618. HRESULT QueryStdInterfaces(const _variant_t& varSrc) ALLEGIANCE_THROW0()
  619. {
  620. if (V_VT(&varSrc) == VT_DISPATCH) {
  621. return _QueryInterface(V_DISPATCH(&varSrc));
  622. }
  623. if (V_VT(&varSrc) == VT_UNKNOWN) {
  624. return _QueryInterface(V_UNKNOWN(&varSrc));
  625. }
  626. // We have something other than an IUnknown or an IDispatch.
  627. // Can we convert it to either one of these?
  628. // Try IDispatch first
  629. //
  630. VARIANT varDest;
  631. VariantInit(&varDest);
  632. HRESULT hr = VariantChangeType(&varDest, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc)), 0, VT_DISPATCH);
  633. if (SUCCEEDED(hr)) {
  634. hr = _QueryInterface(V_DISPATCH(&varSrc));
  635. }
  636. if (FAILED(hr) && (hr == E_NOINTERFACE)) {
  637. // That failed ... so try IUnknown
  638. //
  639. VariantInit(&varDest);
  640. hr = VariantChangeType(&varDest, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc)), 0, VT_UNKNOWN);
  641. if (SUCCEEDED(hr)) {
  642. hr = _QueryInterface(V_UNKNOWN(&varSrc));
  643. }
  644. }
  645. VariantClear(&varDest);
  646. return hr;
  647. }
  648. };
  649. // Reverse comparison operators for _com_ptr_t
  650. //
  651. template<typename _InterfaceType> bool operator==(int null, _com_ptr_t<_InterfaceType>& p) ALLEGIANCE_THROW(_com_error)
  652. {
  653. if (null != 0) {
  654. _com_issue_error(E_POINTER);
  655. }
  656. return p == NULL;
  657. }
  658. template<typename _Interface, typename _InterfacePtr> bool operator==(_Interface* i, _com_ptr_t<_InterfacePtr>& p) ALLEGIANCE_THROW(_com_error)
  659. {
  660. return p == i;
  661. }
  662. template<typename _Interface> bool operator!=(int null, _com_ptr_t<_Interface>& p) ALLEGIANCE_THROW(_com_error)
  663. {
  664. if (null != 0) {
  665. _com_issue_error(E_POINTER);
  666. }
  667. return p != NULL;
  668. }
  669. template<typename _Interface, typename _InterfacePtr> bool operator!=(_Interface* i, _com_ptr_t<_InterfacePtr>& p) ALLEGIANCE_THROW(_com_error)
  670. {
  671. return p != i;
  672. }
  673. template<typename _Interface> bool operator<(int null, _com_ptr_t<_Interface>& p) ALLEGIANCE_THROW(_com_error)
  674. {
  675. if (null != 0) {
  676. _com_issue_error(E_POINTER);
  677. }
  678. return p > NULL;
  679. }
  680. template<typename _Interface, typename _InterfacePtr> bool operator<(_Interface* i, _com_ptr_t<_InterfacePtr>& p) ALLEGIANCE_THROW(_com_error)
  681. {
  682. return p > i;
  683. }
  684. template<typename _Interface> bool operator>(int null, _com_ptr_t<_Interface>& p) ALLEGIANCE_THROW(_com_error)
  685. {
  686. if (null != 0) {
  687. _com_issue_error(E_POINTER);
  688. }
  689. return p < NULL;
  690. }
  691. template<typename _Interface, typename _InterfacePtr> bool operator>(_Interface* i, _com_ptr_t<_InterfacePtr>& p) ALLEGIANCE_THROW(_com_error)
  692. {
  693. return p < i;
  694. }
  695. template<typename _Interface> bool operator<=(int null, _com_ptr_t<_Interface>& p) ALLEGIANCE_THROW(_com_error)
  696. {
  697. if (null != 0) {
  698. _com_issue_error(E_POINTER);
  699. }
  700. return p >= NULL;
  701. }
  702. template<typename _Interface, typename _InterfacePtr> bool operator<=(_Interface* i, _com_ptr_t<_InterfacePtr>& p) ALLEGIANCE_THROW(_com_error)
  703. {
  704. return p >= i;
  705. }
  706. template<typename _Interface> bool operator>=(int null, _com_ptr_t<_Interface>& p) ALLEGIANCE_THROW(_com_error)
  707. {
  708. if (null != 0) {
  709. _com_issue_error(E_POINTER);
  710. }
  711. return p <= NULL;
  712. }
  713. template<typename _Interface, typename _InterfacePtr> bool operator>=(_Interface* i, _com_ptr_t<_InterfacePtr>& p) ALLEGIANCE_THROW(_com_error)
  714. {
  715. return p <= i;
  716. }
  717. #pragma warning(pop)
  718. #endif // _INC_COMIP