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