Requirement: 1. Create mycom.dll, which only one component, two interface Igetres - methods hello (), iGetRex - method helloEx () 2. In Engineering Components or Type Library #import "Components Sitting / Mycom.dll "no_namespace or #import" Type library location /mycom.tlb "Using Namespace mycom; - Method 1 ------------------------------------------------------------------------------------------------------ ------------------------------ Coinitialize (NULL); CLSID CLSID; CLSIDFROMPROGID (Olestr ("Mycom.getres", & clsid); ccomptr
// create a component using CoCreateClassObject (especially mutilThreads) when a plurality of objects, more efficient IClassFactory * p_classfactory;. Hr = CoGetClassObject (clsid, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, (LPVOID *) & p_classfactory); p_classfactory-> CreateInstance (NULL, __UIDOF (IGETRES), (LPVOID *) & PTR); P_ClassFactory-> CreateInstance (NULL, __UIDOF (iGetRevex), (lpvoid *) & ptrex); Ptr-> Hello (); ptrex-> helloEx (); couninitialize ); - Method 4 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------ Directly get DllgetClassObject from DLL, then generate class objects and class instances (this method can make the component not to register in the registry, this is the most original method, but there is nothing Significance, at least lost COM's transparency of users), not recommended.
typedef HRESULT (__stdcall * pfnHello) (REFCLSID, REFIID, void **); pfnHello fnHello = NULL; HINSTANCE hdllInst = LoadLibrary ( "component directory /myCom.dll"); fnHello = (pfnHello) GetProcAddress (hdllInst, "DllGetClassObject" ); If (fnhello! = 0) {iClassFactory * PCF = null; hResult HR = (fnhello) (CLSID_GETRES, IID_ICLASSFAACTORY, (VOID **) & PCF); if (successded (HR) && (PCF! = Null) { IGetres * pgetres = null; hr = pcf-> createinstance (null, iid_ifoo, (void **) & pgetres); if (succeeded (hr) && (pfoo! = Null) {pgetres-> hello (); pgetres-> Release ();} pcf-> release ();}} freeelibrary (hdllinst); - Method 5 --------------------------- ---------------------------- ClassWizard uses the type library to generate a packaging class, but the premise is that the interface of the COM component must be derived from Idispatch, Specific method: Toned the Add Class Wizard (.NET), select the MFC class in the type library, open, select "File", select "Mycom.dll" or "Mycom.tlb", then you will come out of this Mycom all Interface, select After the interface package you want to generate, the wizard will automatically generate the corresponding .h file. So you can use the components like using a normal class in your MFC.