How to use COM components

xiaoxiao2021-03-06  50

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 pgetres; // Smart pointer pgetres.coCreateInstance (CLSID); pgetres-> hello (); pgetres.release (); // Be careful !! Please see the last "pay attention" couninitialize (); --Method 2 -------------------------------------------------------------------------------------------------------------------------- ----------- Coinitialize (NULL); CLSID CLSID; HRESULT HR = CLSIDFromProgid ("Mycom.getres"), & clsid; iGetres * Ptr; HR = CoCreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __UUIDOF (IGETRES), (LPVOID *) & PTR); PTR-> Hello (); Couninitialize (); - Method 3 --------------------------------------------------------------------------------------------------------------------- -------------------------------- Coinitialize (NULL); HRESULT HR; CLSID CLSID; HR = CLSIDFROMPROGID (OLESTR "mycom.getres"), & clsid; igetres * ptr; IgetRex * Pt rEx; // Create a component used 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 (iGetresex), (LPVOID *) & PTREX); Ptr-> Hello (); ptrex-> helloEx (); Couninitialize (); - Method 4 ---------------------------------------------------------------------------------------------------------------------------------- -------------- Direct DLLGETCLASSOBJECT directly from DLL,

Then generate a class object and class instance (this method can make the component not to register in the registration table, which is the most original method, but this does not make sense, at least lose COM's transparency, 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 (Succeeded (HR) && (PCF! = null) {iGetres * Pgetres = NULL; HR = PCF-> CreateInstance (NULL, IID_IFOO, (Void **) & pgetres); if (succeededed (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", next to all interfaces in this Mycom, After selecting the interface packages you want to generate, the wizard will automatically generate the corresponding .h file. This allows you to use components like usage in your MFC. Coinitialize (NULL); cgetres getrest; if (Getrest. CreateDispatch ("Myco M.GETRES ")! = 0) {getrest.hello (); getrest.releaseDispatch ();} Couninitialize (); - Note -------------------- ------------------------------------------ The smart pointer in COM is actually The class is overloaded -> The purpose is to simplify the reference count, do not need to call AddRef () and Release (), but why we are in Method 1 pgetres.release (), question in, us The end of the intelligent pointer Pgetres life cycle is after Couninitialize (), the suite opened by Coinitialize has been closed after CounInitialize (), and Pgetres is destructed at this time, causing the crash of the program to solve another method of this problem. Coinitialize (NULL); CLSID CLSID; CLSIDFromProgid (Olestr ("Mycom.getres"), & clsid; {ccomptr pgetres;

转载请注明原文地址:https://www.9cbs.com/read-91183.html

New Post(0)