Several methods of components VC Using COM

xiaoxiao2021-03-05  29

With the rapid development of Internet and intranet applications, Com (Component Object Model, component object model) penetrates into various fields of software disciplines with its huge potential. Under the Windows operation platform, there are many component modules, such as DirectX multimedia packages, OLE DB / ADO database component systems, etc., and OLECTX multimedia packages, OLE DB / ADO database components, etc., which greatly enriches the functionality of the operating system. Since the COM mechanism allows any two components to communicate with each other without having to worry about what is operated in the computer, it is not necessary to care about what language to use, which makes COM technology have powerful vitality. Especially the combination of Windows 2000 with the next generation of COM is more close, which will make COM / COM technology more widely. This article describes three ways to write COM clients in VC 6.0. Although each method can achieve the purpose of using code components, understanding and mastering all methods to provide a greater room for selecting the appropriate method according to specific situations. To the narrative, the program framework used herein is the MFC EXE project of dialog mode. Before programming, first determine if the code component to be operated is registered in the system. If the code component is not registered, you can register it through the Regsvr32. EXE program under the Windows / System directory. The COM library function uses the COM library function to use code components to implement the most troublesome and difficult methods in the three methods described in this article. It requires developers to have an in-depth understanding of COM principles. The method implementation steps are as follows: 1. First add COM initial and terminate code. Add the following code in the initialization instance function initInstance (); ... coulling (); the above statement runs in the MFC framework / non-MFC framework, but because this program uses the MFC framework, it is also possible It is initialized using the AFXoleinit () function. 2. Then use a #include statement to include references to the component header file and create a component object. In the header file contains the C definition of the interface and the symbolic constant of the interface ID IID and class ID CLSID. Create a job in the initialization dialog function: Iaccount paccount = null; ... COCREATEINSTANCE (CLSID_ACCOUNT, NULL, CLSCTX_INPROC_SERVER, IID_ID_ID_IACCOUNT, ReinterPret_cast (& PACCOUNT)); 3. Last release component object. This work should be done before the program exit, such as in the response function of the message WM_CLOSE: if (paccount! = Null) paccount-> realease (); call to other function functions in this code component, can pass the component object Interface pointer paccount to: ... bstr bstrresult; paccount-> post (100, bstrresult); sysfreestring (bstrresult); ...... Since the COM support class is defined in COMDEF. H, it is also included in the header file. Can make the program run normally. The class wizard can directly read the type library of the component through class wizard, and generate a class of each interface in the package type library. The method and properties of the component interface can be accessed through these classes, and some are some similar to the method using the ActiveX control. First add code to the COM component. We can join the component's .tlb type library file through the class wizard, and introduce its interface class from it. In this type of library file introduced in this example, only one component package class IACCOUNT from ColedgeDispatchDriver is distributed.

Through a member of the packaging class, you can learn which services can be provided by the component interface, and can access the method and properties of the component interface. Create an Account component object in the CreateDispatch () member function of the COLEDISPATCHDRIVER class in the initialization dialog function: Iaccount M_account; ... m_account.created, ("atlsample.account.1)); where the progID value" Atlsample. Account. 1 "can The OLE VIEW tool in Microsoft Visual Studio Tools 6.0 is found, and it is that the component has been successfully registered. Release the Account component object can also be done with the ReleaseDispatch () function of the COLLEDISPATCH-DRIVER class. For the POST method used in the COM library function method, use the following method to call: CString str = m_account. POST (100); It can be seen that this method implements the same function but implemented is simpler than the previous method. And it is not high for understanding COM. #Import instruction #import instruction method is very easy. It is very suitable for the type library file to adopt this instruction because whether it is a debug version or an release version, for the type library file, its path is fixed. #Import instructions will extract two files from the type library to be introduced when executed: one .tlh file and a .tli file, the latter is only the function of the packaging class, and the former contains many important importance information. The intelligent interface pointer is also defined in: _COM_SMARTPTR_TYPEDEF (Iaccount, __ uuidof (Iaccount)); When actually compiled, the compiler will expand it into the following code, and define a smart pointer IACCOUNTPTR by the _com_ptr_t template class. The reason why it is a smart pointer, because it replaces Iaccount, it will automatically handle cocreate-instance and all iUnknow methods. It is very convenient to use: typedef _com_ptr_t <_com_iiid > IaccountPtr; due to it Intelligent pointer, we can call the _COM_PTR_T template class CREATEINSTANCE () function to complete the creation of the interface pointer: IaccountPtr m_account = null; m_account.createInstance (__ uuidof (account)); due to the structure in the generated .tlh file Declaration and Declspec (UUID (")) declaration, so you can easily get the GUID of the interface with __UUIDOF (Account). DECLSPEC (UUID (")) Declaries linking the GUID and class and each interface, allowing developers to get the GUID of classes and interfaces in a UUIDOF operator. It is important to point out that in order to prevent the name conflict between the original code and the newly introduced code, the compiler defines a namespace identified by the type library name and add an identifier within any name declared. In order to avoid specifying a namespace ID, you can add using name after the #import statement, and you can also use Rename_NameSpace to change the namespace.

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

New Post(0)