VS2010 Calling COM components

Source: Internet
Author: User

The development process of COM components is not much, the data is not much, so the development of a part of the COM component problems.

In this article, we explain how to use VS2010 to create a COM component. Now create a VC + + project to invoke this interface based on the COM component interface created by the article.

Use process

Create a new Win32 console project.

The main file code is as follows:

#include"stdafx.h"#include".. /testcom/testcom_i.h"#include".. /TESTCOM/TESTCOM_I.C"int_tmain (intARGC, _tchar*argv[])    {HRESULT hr; IMyClass*pcom =NULL;    CoInitialize (NULL); HR= CoCreateInstance (Clsid_myclass,null,1, Iid_imyclass, (lpvoid*) &PCom); if(SUCCEEDED (HR)) {LONG res; PCom->add (2,3,&res); printf ("2+3=%d\n", RES);    } couninitialize (); return 0;}

Code Description:

#include " .. /testcom/testcom_i.h"

#include " .. /TESTCOM/TESTCOM_I.C"

is to include the files of the COM component definition interface into this project so that the COM interface functions can be called directly later.

CoInitialize (NULL) must be called before calling COM components;

Release CoUninitialize () after the call is complete;

CoCreateInstance is used to create a COM instance, the function is described as follows:

HRESULT CoCreateInstance (__in refclsid rclsid, __in lpunknown punkouter, __in DWORD dwclscontext, __in RE Fiid riid, __out lpvoid*PPV); Rclsid [inch] The CLSID associated with the data and code that would be used to create theObject. Punkouter [inch] If NULL, indicates thatObject  isNot being created asPart of the aggregate. If Non-null, pointer to the aggregateObject'S IUnknown interface (the controlling IUnknown).dwClsContext [inch] Contextinchwhich the code that manages the newly createdObjectwould run. The values are taken fromThe enumeration Clsctx riid [inch] A Reference to the identifier of theInterfaceTo is used to communicate with theObject. PPV [ out] Address of pointer variable that receives theInterfacePointer requestedinchriid. Upon successfulreturn, * PPV contains the requestedInterfacePointer. Upon failure, * PPV contains NULL.
Related Downloads

Code download

VS2010 Calling COM components

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.