After the introduction of Windows NT4.0 in Microsoft, only distributed processing that can occur on large hosts or workstations can finally work in the world of PC. What caused Windows NT can become an enterprise computing environment In addition to NT, the most important thing is to join the functionality of DCOM (Distributed Component Object Mode1). Microsoft launched the technology in NT4.0, but Microsoft clearly wants DCOM to become a standard communication protocol for communication communication. So the DCOM for Windows 95 is then launched, and the DCOM in Windows 98 has been launched last year, which represents a very large and complex calculation work that uses the PC environment. Through this technology, the PC-level programmers can finally enter the world of distributed objects.
First, DCOM: Distributed Component Object Model
Microsoft's COM model enables various objects in the same computer to communicate with each other and use the services provided by the other party, while DCOM further allows different applications and objects to communicate between different computers in the network. This means that the object can be established in the application or in the Dynamic Library, and provide various services for this object, and then in another different machines can call the method of this object or to access its properties. value. Why is DCOM? Because the current trend of software development is to move towards components-based distributed computing, there is a need for communication protocols that communicate with each other in different components. This is like a variety of adapters in the PC, which is inserted over PCI and ISA and other buss, communicating through bus and other computer components. Components also require a bus that communicates with each other, communicating with a consistent communication protocol, and DCOM is the bus of such components. It is responsible for information transfer between various components. If there is no DCOM, then you will not meet the requirements of the distributed computing environment. DCOM is constructed above COM. In COM, the program can access methods and properties in specific interfaces of this object by specifying an ID (Class GUID) of an object and the interface ID to be accessed. As shown in Figure 1.
Figure 1 Method or attribute in the access object interface in the COM model
As can be seen from Figure 1, in the basic model of COM, different applications and objects can obtain the other party by specifying the object and the interface ID, but this model is in the same computer. What should I do if the application or object you want to access a particular object in Computer B. I can't do this in the basic COM model. Microsoft solves this problem with DCOM. as shown in picture 2:
Figure 2 Method for establishing an object and interface of DCOM
As can be seen from Figure 2, when using DCOM, the way to establish objects using COM is the same, only the parameters of a machine name is added again. If you use the Windows API's COGETCLASSOBJECT to establish an object in COM, you only need to enter the parameters of the machine name to establish an object in the remote specified computer, and the information of the specified interface is obtained. Simply put, DCOM is the extension of COM, which allows objects and interfaces to build different machines and keep compatibility with COM. It is constructed above the technology of RPC (Remote Procedure Call) and uses TCP / IP as a network communication protocol. Currently, NT4.0 and Win98 are directly supported DCOM. You can install DCOM for Windows95 in Win95 to support it. Microsoft is also perfected with DCOM, such as Server Pack 1, Server Pack 2, Server Pack 3, and just released Server Pack 4, which is getting better and better in Server Pack 2. There is a very important feature in which a DCOM is to develop N-Tier applications must be used. Now DCOM has occupied a very important status, Microsoft's SQL Server, Transaction Server, Message Queuing Server, etc., make full use of DCOM to make a distributed calculation in the enterprise. DCOM is also one of the weapons used by Microsoft to fight against CORBA (Common Object Request Broker Architecture). Second, the use of DCOM
In many object-oriented programming languages (such as: Visual C , Delphi, Visual Basic), the compiler uses the Windows API CocreateInstance to create COM or DCOM objects. In fact, in a general tool or intermediary server (such as: MS SQL Server et al.), The standard way to establish a COM or DCOM object is the use of the Windows API's iClassFactory. To establish a DCOM object in this way, first we must obtain the iClassFactory interface that can build a specific DCOM object, and use this interface to establish a DCOM object. In order to obtain the specified iClassFactory interface, the prototype of the API declared by Microsoft declaration can be used as follows: stdapi cogetclassObject (
Refclsid Rclsid, DWORD DWCLSCONTEXT, COSERVERINFO * PSERVERINFO, REFIID RIID, LPVOID * PPV); where second, three or three parameters are very important. The second parameter value may be used as follows: typedef enum tagCLSCTX {CLSCTX_INPROC_SERVER_ = 1, establishing CLSCTX_INPROC_HANDLER = 2 COM objects from the In-Proc Server, established from the Out-Proc Server COM objects CLSCTX_LOCAL__SERVER = 4, CLSCTX_REMOTE_SERVER = 16, away from the use of DCOM} Out-proc server in the CLSCTX program creates COM objects From the CLSCTX declaration, you can create COM and interface objects from the remote machine as long as you use CLSCTX_REMOTE_SERVER in the second parameter of COGETCLASSOBJECT. If the location of the remote COM object is not indicated in the system registry, the value of the third parameter must be specified when the CogetClassObject is in Cogether COM object. The third parameter of COGETCLASSObject is in Ole1.x is a reserved parameter, and Microsoft's file says this parameter must be NULL. But after Ole2x, DCOM must be supported, this parameter is used to specify information about the application server. The prototype of this parameter are as follows: typedef struct_COSERVERINFO {DWORD dwReserved1; LPWSTR pwszName; specify the name of the application server COAUTHINFO * pAuthInfo; security verification information DWORD dwReserved2; COSERVERINFO;} CoGetClassObject with this parameter, you can start the COM object remote computer. Third, example
Assume that the existing application system structure is shown in Figure 3:
In the client's application, you need to create a COM object in the LJServer machine, then you can write the following code in the client's application. Var ClassFactory: IclassFactory; WideCharBuffer: array [.. 256] of WideChar; Info: TcoServerInfo; SwMachineName: WideString; Begin ClassFactory: = nil; StringToWideChar (* LJSERVER *, PwideChar (swMachineName), 256); Info.PwszName: = PwideChat (sqMachineName); OleCheck (CoGetClassObject (ClassID, CLSCTX_REMOTE_SERVER, @ Info, IID_IclassFactory, ClassFactory); If classFactory = nil then ShowMessage ( '! error can not be established IclassFactory Interface') Else ClassFactory.CreateInstance (nil, IID_Iunknown, Unknown); Try Result : = UNKNOW; Finally ClassFactory.Release; unknown.release; end; uses CLSCTX_REMOTE_SERVER and TCO ServerInfo in the above program code to establish COM objects in the LJServer, and get its iClassFactory interface. Note that when using TCOSERVERINFO to set the machine name, you must first convert the machine name to WideChar, because the name of the machine in NT can use Chinese double-byte name, such as LJServer, can also be called "Lu Jin's server" So you must use double-bytes. After getting a ClassFactory interface, you can use it to create specific COM objects and interfaces that can be generated by this interface. The next program code is an iUnknown interface that gets this ClassFactory's COM object. Finally, returns this IUNKNOWN interface. Note that if you use the programming language does not support the COM interface, you cannot specify the IUnknown interface to the Result variable, you must perform a certain conversion. For example: Result: = varFrominterface Unknown). Four, DCOM settings