What are proxies and stubs?
For example, you go to the ATM to withdraw money. You areCustomerThe ATM is yours.ProxyYou don't care
You just want to see enough or more money from the exit (this is the transparency of com ). The operations between you and the bank are completely implemented by the ATM agent. Your withdrawal request is sent to the server of another bank through a cash machine. You do not need to know where you are taking the money. What you care about is your identity and the amount of money you withdraw. When he confirms your permissions, he performs the corresponding operation and returns the operation resultATMThe ATM will fetch the corresponding amount of money from the safe according to the result returned by the server. After removing the card, the operation is complete. The ATM is not directly connected to the server. There is also a "stub" between them. The ATM communicates with the stub, And the Server communicates with the stub. In a sense, the stub is the proxy of the server.
Figure 1 inter-component communication
For example, the two components do not communicate directly, but indirectly through communication between proxies and stubs. The channel in the figure is part of the com library.
COM, only the out-of-process components will use proxy and stub ).
The proxy is created in the customer's process, and the stub is created in the process of the component com object. Each function of each interface has its own proxy and stub.
Why use a proxy and a stub?
Why Should customers use proxies and stubs instead of directly connecting to objects? For a reason, the connection between the client and all com objects is called through pointers. For the service, the interface functions of the called object are also implemented through pointers, however, pointers are valid only in the same process. In this way, proxies and stubs are generated to fulfill this mission.
The role of the proxy and the stub is more than this. It also needs to package all the parameters (including interface pointers) to generate RPC (Remote Process calls) and lead to another process, or another machine where the object is running.
Figure 2 proxy Structure
The displayed proxy structure supports standard column sets of parameters. The proxy of each interface implementsIRpcProxyBufferInterface, used for intercommunication between each part of the cohesion. When you want to pass the parameters of the column set over the process boundaryIRpcChannelBufferInterface Method (this interface is implemented by the channel ). Channel calls the RPC Runtime Library to transmit data to the destination.
Figure 3 structure of the stub
As shown in, each interface's stub is connected to the corresponding interface of the object. The chnnel distributes incoming messages to the stubs of the appropriate interface. All components pass throughIRpcChannelBufferInterfaces communicate with chnnel, which provides connections to the RPC Runtime Library.
Grouping alling)
When it comes to proxies and stubs, column sets are indispensable. What is a column set?
Column setTo package and process function parameters. pointer and other data must be converted to be understood by another component. After the column set is complete, RPC is called. It can be said that the column set is a data format conversion method.
There are three ways to create a column set:
1. Type Library column set
It can be any interface compatible with OLEAUTOMATION, meaning that the returned value of your interface must be HRESULT, and the type of the parameter used should also be compatible with the VARIANT structure of C ++.
2. Create Stub/proxy DLL
The DLL source generation is generated by MIDL. You must register this DLL on both the server and client (this is the standard marshal method). When using this method, it is best to compile the stub/proxy code as an independent component.
3. Custom ingress aling
Custom marshal requires you to implement the IMarshal interface in your components. When COM needs marchal, it first checks whether you support the IMarshal interface through QueryInterface. If you implement this interface, that is, you control all the parameters of your COM and the method mode for packaging and unpacking the returned values.
Establish proxy and stub dll
Using the MIDL tool, for an IDL file, MIDL will analyze and automatically generate relevant files for the corresponding proxy/stub DLL.
How to use proxies and stubs
For you, the use of proxies and stubs is transparent, so you don't have to worry about how to use them. The com library will know how to do it.