* This document is selected from: Visual C ++/mfc Development Guide. |
|
Our dream is to use com to recreate our software -- Xiangjia keywords: com Ole Active X container automation Controle iunknown idispatch self_contained class factory dcom com + mts msmq an application is usually composed of a binary file. After the compiler generates this file, the application will not change until the next version is re-translated and the new version value is released. Changes to the operating system, hardware, and customer needs must be recognized after the entire application is re-compiled. The whole software industry is moving towards the future with the increasingly aging of released software. At present, this situation has changed. One solution found by developers is to split a single application into multiple independent parts. That is, components. That is, the software component is regarded as an object. The advantage of this approach is that it can replace existing components with new components as technology continues to develop. At this time, the application is no longer an object that was destined to be obsolete before release, but can be improved as new components are constantly replaced. This is not very tempting for us. Everyone knows that we cannot release new versions to users every day. But you must like it. When you change some implementation functions in the DLL as needed and generate the DLL, you can use new performance without re-compiling the entire program. Isn't that incredible. Using the com model will have a huge impact on the software. It is the revolution of the software industry. For a software programmer, who doesn't want to be able to use classes as conveniently as MFC in programming, rather than looking at boring and embedded functions, dazzled global variables. It takes a lot of time to understand the source program before modifying the program. Com provides a binary standard, object-oriented, extensible, and reusable communication protocol. If you continue to read it, you will find that the benefits of building a com-based testing software will be huge. Spending time and money will get twice the result with half the effort. I am afraid every programmer has this experience: when modifying a large program, the complexity of the program structure makes it very difficult to implement some functions. How can we construct a logic model with clear logic, complete structure, relatively independent functions, and easy to expand? Microsoft provides COM, Apple has its opendoc, and IBM has som. The current software industry is on the road of hardware 20 years ago. Two decades ago, people created integrated circuit blocks. They used small chips that could complete specific work to construct new and larger chips to complete new functions. This is the problem facing the software industry. The two most popular new technologies in the computer industry today are com and DCOM. Com seems to be a good solution to all the drawbacks of software development. Of course, this does not mean that you will use com once and for all. The software industry has no silver bullet myth. But Com has a better solution to the new problems we face today. For example, distributed program development, transaction processing security, and cross-platform reusability ------ advantages of using components: the advantage of using components is that applications can evolve over time. In addition, the use of components makes it easier and more flexible to upgrade existing applications. In essence, the component architecture is customizable. Users can replace a component with components that better meet their needs. Like a class, COM is a construction model of software programming, rather than a mathematical model. One component technology evolution observation component technology evolution is the beginning of understanding COM and DCOM, although object-oriented technology has gone through a complete process of its own evolution, however, component technology, as a school creature of object-oriented technology, has its own evolution process. The main foundation of component technology evolution is Windows OS. Of course, other technologies have also played a major role. 1. Custom Control in Windows 3.0 ). A custom control is a dynamic link library that can output a defined function set. However, unlike the common DLL, a custom control can perform feature operations and be able to trigger events caused by user or program input. In 2.1991, Microsoft launched vbx, which fully reflected the important role of small reusable components. Unfortunately, there are too many constraints in the vbx package, 16-bit structure, and there is no open interface, which limits the application of vbx. 3. microsof developed the ole1.0 specification in the same year. (Object Linking and Embedding ). Ole1.0 strives to develop towards the file center, rather than the traditional application-centric development direction. "Composite document" is the product of this idea. It is a method for storing data in a single file in multiple formats, such as text, graphics, video, graphics, and sound. The word in the office suite is too familiar to us. However, ole1.0 is not mature in terms of its complexity and technologies. This is a cold spot in the market. 4. Microsoft is not intimidated by ole1.0's difficulties, but is committed to improving Ole. In 1993, the ole2.0 specification was released, and a fully object-based service structure was launched, which is easy to expand, customize, and enhance. Through this structure, we can get the following services: COM clipboard (Clipboard) drag and drop embedded (embedding) Fixed Point activation (in-place activation) Link (linking) tag (permanent name) (moniker (persistent naming) OLE Automation (OLE Automation) ole control (OLE Control) Ole document (OLE document) Structured Storage (structrued storage) same data transmission (uniform data transfer, UDT) 5. the word Active X was proposed at the March 1996 Internet Professional Developers Seminar (Internet PDC), which was originally a call rather than a specific application development technology. Known as: active the Internet with the rapid development of the Internet, Active X becomes the core of defining all content from web pages to Ole controls. On the one hand, it represents a small, fast, reusable component that connects users to Microsoft, Internet, and new technologies in the industry. On the other hand, Active X represents the policy for integrating the Internet with applications. there is a saying: "Active X is the product of Ole's collision with the Internet, and the Internet makes it look like today ". today's Active X includes: active X file Active X control cominternet tag Active X hyperlink Active X Conference Active X Server Extension Active X script code signature HTML extension activity movie 2 Preliminary Study of COM Component Object Model (COM) is the binary standard for Windows objects. Provides a unified, object-oriented, and scalable communication protocol for Windows. This means that the executable code (. dll or. EXE file code) describing an object can be executed by other objects. Even if two objects are written in different languages, they can communicate with each other using the com standard. I personally think: COM is the product of Microsoft's development. Most of the concepts proposed by Microsoft recently are based on COM. Com continues to improve with the growth of Microsoft, Microsoft is also proud of COM. Today, Microsoft has developed into a software family. Office series, Visual Basic, Visual C ++, and Visual J ++ have become the standard for Windows program design. In order to exchange data between different applications and share resources, an interface rule and protocol are urgently needed. Establish a connection between a software module and another software module and describe it. After this connection is established, the two modules can communicate through an interface mechanism. Based on the binary layer, COM is a language-independent interface specification. Problems with the data exchange technology used in the past: * Windows APIs have too many programming interfaces (more than 350 independent functions) * VBA cannot be used in Win32 environments. * DDE is a complex system for applications, topics, and projects. * DLL is a fully application-oriented com feature. * It provides a standard language-independent method for Win32 EXE to load and call Win32 DLL. * Provides a common method for an EXE control on the same machine. (DDE can be replaced) * use Active X instead of vbx. * Provides a new method for interaction between applications and operating systems. * Scalability is provided to adapt to new protocols, such as Microsoft's ole db data interface. * Step-by-Step COM (DCOM) allows one EXE to communicate with different and other EXE files. * COM + combines COM, DCOM, MTS, and MSMQ. 1. The basic design concept of COM: We usually describe an object to divide it into several methods and attributes. By inheriting this mechanism, you can obtain the right to use methods and access attributes to process data. The COM module does not allow customers to directly call object methods (including attributes, constructor, and destructor). com only provides one standard global function to access objects. This maximizes the separation of customers and services. The customer connects to the server through interfaces and uses the memory structure of the virtual function table (vtbl) to implement a set of pointers to the virtual function implementation. 2. How does com exchange data? Interface. This interface defines the function call method, standard structure-based data transmission technology, and some standard function calls. An interface is a collection of virtual functions, essentially a collection of function names. It is a C ++ class without data. It is a pure virtual function. It inherits from this class and provides function code. Other functions call these functions to obtain code. All component objects must have an interface called iunknow. The iunknown interface aims to find other interfaces. It has a QueryInterface () function, which uses an interface ID to return a pointer to this object interface. All other interfaces are inherited from iunknown. Therefore, all interfaces used have query interface (). Interface iunknown {public: begin_interfacevirtual hresult implements QueryInterface (refiid riid, required * _ rpc_far * ppvobject) = 0; virtual ulong extends addref (void) = 0; virtual ulong stdmethodcalltype release (void) = 0; end_interface}; addref () and release () are used to connect with the applications that are using interfaces. all three functions are inherited by all interfaces. interface implementation: multiple inheritance is supported. However, the problem with multiple inheritance is that the symbol names of multiple interface methods conflict. That is, multiple interfaces contain methods of the same name, but different interfaces have different methods. The compiler only allows programmers to implement one method. Obviously, there is a problem with implementing interfaces through multi-inheritance. In MFC, classes are nested. The MFC Library has a set of macros that can automatically implement this process. Interface_part macros generate Nested classes, generate prototypes for specific interface functions, and add the prototypes of QueryInterface, addref, and release. The declare_interface_map macro generates a table declaration that contains the IDs of all class interfaces. The csf-target: externalqueryinterface () function uses this table to obtain the interface pointer. 3. Another useful interface, class factory, is a type used to support special com interfaces named iclassfactory. Iclassfactory is derived from iunknown. Its main member function is createinstance (). It is generated because we cannot directly call the constructor in COM, so we had to let the component decide how to construct the object, so the component provided a class factory to encapsulate the specific creation process. Create multiple objects using the class factory. The MFC library uses the dynamic creation technology to solve this problem. A class named coleobjectfactory can create any class at runtime. Use the dyncreate macro to set the standard dynamic creation mechanism. Olecreate macro declares and defines a Global Object of the coleobjectfactory class, using the specified unique clsid. In the DLL component, the dllgetclassobject function is used to find a specific class factory object based on the global variables set by the olecreate macro and return a pointer to the object. When the DLL is initialized, regesterall is called. In the EXE component, the initialization code calls the static coleobjectfactory: registerall function, which searches for all factory objects, and call coregisterclassobject to register each object. Relationship between interface sets, interfaces, interface functions, and attributes. I personally think that the interface set, interface, interface function (method), and attribute have a certain logical relationship. The interface set contains multiple interfaces. An Interface contains a class of interface functions, each of which corresponds to a class of attributes. You are concerned about the number of interface functions. You can only use the interface functions to change the attributes. Both attributes and interface functions abstract component objects. 4. Automation & Active xautomation & Active X is an important part of the Active X component. The Active X control is also a small Automation server. Automation tells the application what to do. It reveals attributes and methods. If we reveal all the methods and attributes of our application, any programming language of the automation application can become the writing language of our application. There is always a program under control to know the most important thing about interaction with automation. Applications under control are called automation controllers, and applications that reveal methods and properties are called automation servers. Automation Communication actually clearly defines the master-slave relationship. The master is an automated Customer program, from an automated component (control ). By constructing a component object or establishing a connection with an existing object in a running program, the customer program can initialize the interaction process, and then the customer program can call the interface functions in the component program, and release the completed interfaces. This means that automation-enabled applications such as Word, Excel, access, and VB can be connected to components compatible with auto mation. We can use VC ++ to call any control in Windows (even if it is included in word or other programs), or we can use VC ++ to write components by ourselves, use a VBA macro to call it. (1) idispatch is the core of automation. It collects communication between all internal modules on idispatch: invoke (). Like iunknown and iclassfactory, it is fully supported by com scheduling. From the aspect of component 1, we need to have a com class with the idispatch interface (including the necessary class factory). From the user perspective, we use the standard COM technology to obtain pointers. Interface idispatch: Public iunknown {public: Virtual hresult implements gettypeinfo (uint itinfo, lcid, itypeinfo _ rpc_far * pptinfo) = 0; virtual hresult implements partial (refiid riid, lpolestr _ issue * rgsznames, lcid, dispid _ issue * rgdispid) = 0; virtual hresult initiate invoke Arams, variant _ rpc_far * pvarresult, interval info _ rpc_far * p1_info, uint _ rpc_far * puaargerr) = 0 ;}; Note: The invoke () function is the most important function, used to call functions and properties of the Automation server. (2) Implementation of idispatch: The component program can implement the idispatch interface in several ways. The most common practice is to hand over most of the work to the Windows com dll, which can be implemented by calling the com function createstddispatch or entrusting the invoke call to the itypeinfo interface, it is used to process the Type Library of components ). A Type Library is a table that allows you to find the symbolic names of object methods and attributes in components. MFC supports the Type Library, but the type library is not used in the implementation of idispatch. The implementation of idispatch is driven by the Distribution ing (dispatch map. The MFC program does not call createstddispatch at all, nor does it use a Type Library to implement idispatch: getidsofname. (3) How is the distribution ing of MFC related to idispatch and invoke member functions? The distribution ing macro generates a data table internally. The invoke function in MFC can read this table. The controller can obtain the idispatch pointer of the class. In addition, an array of pointers is used as a parameter to call invoke. The execution of the invoke function of MFC decodes the provided pointer using class distribution ing and can call a member function. (4) variant type: Automation customers and components use the variant type. It is a common data type and is a 64-bit data. Idspatch: invoke uses it to pass parameters and return values. Partition (cwwddctrl, colecontrol) // {partition (cwwddctrl) Partition (cwwddctrl, "wdlowline", m_wdlowline, partition, vt_r4) Partition (cwwddctrl, "wdcaption", m_wdcaption, onwdcaptionchanged, vt_bstr) values (cwwddctrl, "wdxmean", m_wdxmean, onwdxmeanchanged, vt_bstr) disp_property_policy (cwwddctrl, "wdymean", m_wdymean, onwdymeanchanged, vt_bs Tr) disp_function (cwwddctrl, "success", setwdobject, failed, failed) disp_function (cwwddctrl, "setwdproper", setwdproper, failed, failed) disp_function (cwwddctrl, "deletelist", deletelist, partition, vts_none) disp_function (cwwddctrl, "partition", partition, partition, vts_r4) disp_function (cwwddctrl, "setambie", setambie, partition, vts_r4 vts_r4 vts_r4) Partition () 2. A typical space of the Active X control includes The user interface at design time and runtime. The unique idispatch interface defines the methods and properties of the control. The unique iconnectionpoint interface is used for events that can be triggered by the control. In addition, a widget can also support the entire lifecycle, as well as support for user interface features such as cutting, drag and drop. In terms of structure, a control has a large number of COM interfaces that must be supported to take advantage of these features. The Active X control is always run in the volume it is placed in. The extension of the space is. ocx, but from the perspective of the running module, it is just a standard Windows DLL file. Properties and events are the communication between applications and controls in the container. Events are used for communication between containers and controls. For Activex Controls, events are the idispatch interfaces displayed at the container end. The underlying mechanism of the event is called the connection point. A connection point is the type description of the interface required to communicate with the container. The connection point is not limited to the idispatch interface, but can also be implemented by any COM. The control is only the first one to use them. ActiveX is an independent object that can trigger and handle messages with unique attributes and multi-thread capabilities. It also supports bidirectional communication and message transmission between other containers. the most amazing thing about the Active X control is its programmability and G reusability. it is open to external environments and can be used in various programming Hefei programming environments. the control has three attribute sets. (attribute, object, method)
* This document is selected from: Visual C ++/mfc Development Guide. |
|