Learn Real CoM with the MFC Library (translation)

xiaoxiao2021-03-06  50

Such more simulations, now we will convert SpaceSHIP examples into true rather than simulated COM. Before we start, you need to master a little more knowledge. First you have to learn the CogetherClassObject method, then you must also know how COM is loaded into the component through Windows Registry; then you must know, and you must know the in-process component (a DLL) and processes (OUT) -of-process) Components (an EXE and DLL) differences. Finally, let yourself be familiar with MACRO of MFC because it supports class nested.

CogetClassObject method in COM

In our previous simulation, we use a hypothesis method called getClassObject, and in real COM, we use the CogetClassObject method (COMPONENT OBJECT "). Compare the prototype of our GetClassObject method and the prototype below:

Stdapi CogetherClassObject (Refclsid Rclsid, DWORD DWCLSCONTEXT, COSERVERINF * PSERVERINFO, REFIID RIID, LPVOID * PPVOBJECT)

The pointer of the interface is placed in the PPVObject parameter; PServerInfo points to a machine, the machine stores the class object to be called (assignment null represents the machine); type refclsid and refiid refer to 128 GUID (Global Unique Identifiers) (COM) The global unique identifier of classes and interfaces); stdapi indicates that the method will return a value of the 32-bit HRESULT type.

Define the standard GUID in the Windows library (for example, the GUID of those named interfaces that Microsoft has created) will be dynamic links in your program. Customize the GUID of the class and interface, such as SapceShip object, must be defined in your program:

// {692D03A4-C689-11CE-B337-88EA36DE9E4E} static const same IID_IMOTION = {0x692d03a4, 0xc689, 0x11ce, {0xB3, 0x37, 0x88, 0x4a, 0x36, 0xDE, 0x9E, 0x4E}};

If the dwclscontext parameter is CLSCTX_INPROC_SERVER, the Subsystem's residual root will look for a DLL. If the parameter is CLSCTX_LOCAL_SERVER, COM will look for an EXE. In order to improve the efficiency of the execution, the two parameter values ​​can be combined to consider one of the DLL or EXE. The service DLL in the process, because everyone is directly shared with the same address space, so there is a faster speed. Services of the EXE communication method (outside), due to the copy of the process including the copy of the data and the switching of many different process environments, the speed is slower. The return value is the HRESULT type, and this value is 0 (noerror) if there is no error.

Important: There is also a COM method, COCREATEINSTANCE, which contains CogetClassObject and IclassFactory :: CreateInstance features.

Next: com and the windows registry

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

New Post(0)