The COM server is an in-process server, and the DLL name is simpcom. dll. This component has only one interface ifoo, and this interface has only one method hresult sayhello (void)
Call in SDK
============================================
1. The simplest and most common method is to use the # Import Type Library and the smart pointer packaging class provided by VC.
DemoCode:
# Import "D: \ temp \ Vc \ simpcom \ debug \ simpcom. dll" no_namespace
Coinitialize (null );
Ifooptr spfoo = NULL;
Spfoo. createinstance (_ uuidof (FOO ));
Spfoo-> sayhello ();
Spfoo. Release ();/* dizzy. Originally, the smart pointer was designed to prevent users from worrying about this, but I found that if you do not call it manually,ProgramA memory access error will occur after exiting. I tried it on the console. Which of the following heroes knows what is going on */
Couninitialize ();
2. Import the *. H, * _ I. c file generated by midl.exe and call it using the cocreateinstance function.
DEMO code:
/* Add the * _ I. c file to the project. For example, the simpcom_ I .c file in this example defines the guid value of the class and interface. If it is not introduced, a connection error occurs. */
# Include "D: \ temp \ Vc \ simpcom. H"
Coinitialize (null );
Ifoo * pfoo = NULL;
Hresult hR = cocreateinstance (clsid_foo, null, clsctx_all, iid_ifoo, (void **) & pfoo );
If (succeeded (HR) & (pfoo! = NULL ))
{
Pfoo-> sayhello ();
Pfoo-> release ();
}
Couninitialize ();
3. Use cocreateinstance to obtain the class factory object interface directly using cogetclassobejct, and then use the createinstance method of this interface to generate an instance.
DEMO code:
/* Preparations are described in method 2 */
Iclassfactory * PCF = NULL;
Hresult hR = cogetclassobject (clsid_foo, clsctx_all, null, iid_iclassfactory, (void **) & PCF );
If (succeeded (HR) & (PCF! = NULL ))
{
Ifoo * pfoo = NULL;
HR = PCF-> createinstance (null, iid_ifoo, (void **) & pfoo );
If (succeeded (HR) & (pfoo! = NULL ))
{
Pfoo-> sayhello ();
Pfoo-> release ();
}
PCF-> release ();
}
4. You do not need to use cocreateinstance or cogetclassobject to directly obtain dllgetclassobject from the DLL, and then generate class objects and class instances (this method is suitable for you to use a component, but do not want to register this component in the Registry)
DEMO code:
/* Preparations are described in method 2. In fact, you only need to get the definition of CLSID and IID and interface */
Typedef hresult (_ stdcall * pfngco) (refclsid, refiid, void **);
Pfngco fngco = NULL;
Hinstance hdllinst = loadlibrary ("d :\\ Temp \ Vc \ simpcom \ debug \ simpcom. dll ");
Fngco = (pfngco) getprocaddress (hdllinst, "dllgetclassobject ");
If (fngco! = 0)
{
Iclassfactory * PCF = NULL;
Hresult hR = (fngco) (clsid_foo, iid_iclassfactory, (void **) & PCF );
If (succeeded (HR) & (PCF! = NULL ))
{
Ifoo * pfoo = NULL;
HR = PCF-> createinstance (null, iid_ifoo, (void **) & pfoo );
If (succeeded (HR) & (pfoo! = NULL ))
{
Pfoo-> sayhello ();
Pfoo-> release ();
}
PCF-> release ();
}
}
Freelibrary (hdllinst );
Call in MFC
============================================
In addition to the above methods, there is also a more convenient method in MFC, that is, using classwizard to generate a packaging class using the type library, however, the premise is that the COM Component Interface must be derived from idispatch.
Specific Method:
1. Press Ctrl + W to bring up the Class Wizard, press add class to bring up a new menu, select from a type Libarary, and then go to simpcom. DLL, and then all the interfaces in the simpcom will come out. After selecting the interface packaging class you want to generate, The Wizard will automatically generate the corresponding. CPP and. h file.
In this way, you can use the COM component like a common class in your MFC project.
DEMO code:
Coinitialize (null );
Ifoo Foo;
If (FOO. createdispatch ("simpcom. foo ")! = 0)
{
Foo. sayhello ();
Foo. releasedispatch ();
}
Couninitialize ();