Preparation and conditions:
The COM server is a server, the DLL name is simpcom.dll, which only has an interface IFOO, which only one method HRESULT SAYHELLO (Void)
Call in the SDK
=====================================
First, the simplest and most common type, imported the type library with #import, using the smart pointer packaging class provided by VC
Demonstration code:
#import "d: /temp/vc/simpcom/debug/simpcom.dll" NO_NAMESPACE
Coinitialize (NULL);
Ifooptr spfoo = null;
SPFOO.CREATEINSTANCE (__ uuidof (foo));
SPFOO-> SayHello ();
SPFOO.RELEASE (); / * Dizzy, the intelligent pointer is to let users don't have to care about this, but I found that if the program exits, the program will have a memory access error, I do test in Console , Which hero knows what happened, please be an advice * /
Couninitialize ();
Second, introduce the * .h, * _ i.c file generated by MIDL.exe, and use the CoCreateInstance function to call
Demonstration code:
/ * Add * _i.c file in the project, such as the sIMPCom_i.c of this example, the file defines the GUID value of the class and interface. If not introduced, the connection error will occur. * /
#include "d: /temp/vc/simpcom/simpcom.h"
Coinitialize (NULL);
IFoo * pfoo = null;
HRESULT HR = CocreateInstance (CLSID_FOO, NULL, CLSCTX_ALL, IID_IFOO, (Void **) & pfoo);
If (ac) && (pfoo! = null))
{
Pfoo-> SayHello ();
Pfoo-> Release ();
}
Couninitialize ();
Third, do not use COCREATEINSTANCE, use CogetherClassobeJCT to get the class factory object interface, then use the method of this interface to generate an instance.
Demonstration code:
/ * Prepared preparation as second method * /
IclassFactory * PCF = NULL;
HRESULT HR = COGETCLASSOBJECT (CLSID_FOO, CLSCTX_ALL, NULL, IID_ICLASSFAACTORY, (VOID **) & PCF);
En (acf! = null))
{
IFoo * pfoo = null;
HR = PCF-> CreateInstance (NULL, IID_IFOO, (VOID **) & PFOO
If (ac) && (pfoo! = null))
{
Pfoo-> SayHello ();
Pfoo-> Release ();
}
PCF-> Release ();
}
Fourth, you don't have CoCreateInstance or COGETCLASSOBJECT, you can get DllgetClassObject from the DLL, then generate a class object and class instance (this method is suitable for you to use a component, but do not want to register the component in the registry)
Demonstration code:
/ * Pre-preparation work as described in the second method, in fact, as long as the definition of CLSID and IID and the definition of the interface * /
Typedef hresult (__stdcall * pfngco) (refclsid, refiid, void **);
Pfngco fngco = NULL;
Hinstance HDLINST = loadingLibrary ("D: //temp//vc//simpcom//debug//simpcom.dll");
FngCO = (Pfngco) GetProcaddress (HDLLINST, "DLLGETCLASSOBJECT");
IF (fngco! = 0)
{
IclassFactory * PCF = NULL;
HRESULT HR = (FNGCO) (CLSID_FOO, IID_ICLASSFAACTORY, (VOID **) & PCF);
En (acf! = null))
{
IFoo * pfoo = null;
HR = PCF-> CreateInstance (NULL, IID_IFOO, (VOID **) & PFOO
If (ac) && (pfoo! = null))
{
Pfoo-> SayHello ();
Pfoo-> Release ();
}
PCF-> Release ();
}
}
Freelibrary (HDLLINST);
Adjustment in the MFC
=====================================
In addition to the above methods in MFC, there is a more convenient way to generate a packaging class through the ClassWizard utilizing type library, but there is a premise that the interface of the COM component must be derived from Idispatch.
specific method:
1. Press CTRL W to adjust the class wizard, press the Add Class button to pop up the new menu, select from a type libarary, then locate the simpcom.dll, then come out to all interfaces in this simpcom, select the interface packages you want to generate After class, the wizard will automatically generate the corresponding .cpp and .h files.
This way you can use COM components like using normal classes in your MFC project.
Demonstration code:
Coinitialize (NULL);
Ifoo foo;
IF (foo.created ")! = 0)
{
Foo.SAYHELLO ();
foo.releaseDispatch ();
}
Couninitialize ();
--------------------------------------- Sorry, accidentally sent it.
_foo, I don't have to call Release, the reason is that the SPFOO smart pointer is caused by the invocation of the CounInitialize () function. If it is called like this, there should be no problem.
#import "d: /temp/vc/simpcom/debug/simpcom.dll" NO_NAMESPACE
Coinitialize (NULL);
{
Ifooptr spfoo = null;
SPFOO.CREATEINSTANCE (__ uuidof (foo));
SPFOO-> SayHello ();
}
Couninitialize ();