Use of COM smart pointers

Source: Internet
Author: User

Speak with code

ComTest implements two interfaces: ICmd, IStr, and ICmd. There is a function -- Add, and IStr has a function -- Cat, which is used to describe the usage of smart pointers, so it is not listed, the attachment contains the project code. The COM component does not provide the source code.

 
 
  1. # Include <atlbase. h>
  2. # Include <atlcom. h>
  3.  
  4. # If 0
  5. # Import "ComTest. tlb" no_namespace // The third method uses this line to import the tlb Type Library without namespaces
  6. # Endif
  7.  
  8. // Use either of the following two methods:
  9. # Include "ComTest_ I .c"
  10. # Include "ComTest. h"
  11.  
  12. # Include <stdio. h>
  13. # Include <stdlib. h>
  14.  
  15. Using namespace ATL;
  16.  
  17. Int main ()
  18. {
  19. CoInitialize (NULL );
  20. # If 0
  21. // Method 3
  22. ICmdPtr cmd = NULL; // declare an ICmdPtr smart pointer and create an instance that is similar to the one used by ADO.
  23. Cmd. CreateInstance (_ uuidof (Cmd ));
  24. Int sum;
  25. Cmd-> Add (1, 2, & sum );
  26. Printf ("% d \ n", sum );
  27. Cmd. Release (); // fo Yue: Indispensable
  28.  
  29. // Method 1
  30. CComPtr <IUnknown> pUnk; // defines a smart pointer
  31. CComQIPtr <ICmd> pCmd;
  32. HRESULT hr;
  33.  
  34. Hr = pUnk. CoCreateInstance (CLSID_Cmd, NULL, CLSCTX_INPROC); // create an IUnknown instance on the pointer's COM. It's just a bit difficult to understand.
  35. Hr = pUnk. QueryInterface (& pCmd); // equivalent to the object for creating ICmd
  36. Int sum;
  37. Hr = pCmd-> Add (1, 2, & sum );
  38. Printf ("% d \ n", sum );
  39. // Remember to Release
  40. PUnk. Release ();
  41. PCmd. Release ();
  42. # Endif
  43. // Method 2
  44. CComPtr <IUnknown> pUnk;
  45. PUnk. CoCreateInstance (CLSID_Cmd, NULL, CLSCTX_INPROC); // same as the first method
  46. CComQIPtr <ICmd> pCmd = pUnk; // create an ICmd object
  47. CComQIPtr <IStr> pStr = pUnk; // create an IStr object
  48. Int sum;
  49.  
  50. PCmd-> Add (1, 2, & sum );
  51. Printf ("% d \ n", sum );
  52. CComBSTR bstr;
  53. PStr-> Cat (BSTR) "d", (BSTR) "e", & bstr );
  54. Printf ("% S \ n", bstr );
  55. // Release is indispensable.
  56. PStr. Release ();
  57. PCmd. Release ();
  58. PUnk. Release ();
  59.  
  60. CoUninitialize ();
  61. System ("pause ");
  62. Return EXIT_SUCCESS;
  63. }

 

This article is from the "Studoc" blog, please be sure to keep this source http://studoc.blog.51cto.com/1284909/282721

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.