COM agent (Proxy) and stub (STUB)

zhaozj2021-02-16  51

COM agent (Proxy) and stub (STUB)

What is proxy and stub?

For a few more, you go to the ATM; you are the customer, the cash machine is your agent; you will not care

The money is set there, you just want to see enough or more money from the exit (this is the transparency of COM). The operation between you with the bank is completely a cash machine agent implementation. Your withdrawal request passes the cash machine, passes to another, the bank's server, he doesn't need to know where you are paying, what he cares about is your identity, how much is you withdrawal. When he confirms your permissions, the corresponding operation is carried out, return to the results of the results, the cash machine returns the result according to the server, remove the corresponding number of money from the safe. After you take the card, the operation is complete. The cash machine is not directly connected to the server, and there is a "stub" between them, the cash machine and the stub communication, the server and the stub communication. In a sense, the root is the agent of the server.

Figure 1 Inter-component communication

The two components are not directly communicated between the two components above, but are indirectly implemented by communication between proxy and stub. The Channel in the figure is part of the COM library.

In the COM, only the proximity components will be used to use the agent (PROXY) and the stub.

The agent is created in the customer's process, and the root is created in the process of the component COM object. Each function of each interface has its own proxy and stub.

Why use proxy and stub?

Why do customers use proxy and stub, not directly connected to objects? Give you a reason, for the customer, the connection to all COM objects is called by a pointer, and the service to the service, the interface function of the call object is also completed by a pointer, however, the pointer only has the same process It will be effective in it. In this way, the agent and stub are produced in order to complete this mission.

The role of proxy and stub is not only these, he also wants to pack all parameters (including interface pointers), generate RPC (remote process calls), lead to another process, or another machine where the object is run.

Figure 2 Agent structure

The proxy structure supporting parameters of the above figure. The agent of each interface implements the IRPCProxyBuffer interface for mutual communication between each portion. When the agent is ready to pass the column set parameters, he calls the approach to the IRPCChannelBuffer interface (this interface is implemented by the CHANNEL). The Channel calls the RPC Run library to transfer the data to the destination.

Figure 3 Structure of the stub

As shown in the figure above, the stub of each interface is connected to the respective interface of the object. CHNNEL distributes the incoming message to the stub of the appropriate interface. All components communicate with Chnnel through the IRPCChannelBuffer interface, which provides a connection to the RPC running library.

Marshalling

Speaking of proxy and stub, naturally less column, what is the column?

Columns, packed the function parameters, because data such as the pointer must be understood by another component, and the column is completed after the column is completed. It can be said that the column set is a conversion method of a data format.

There are 3 ways columns:

Type library column set

It collects any interface compatible with OleAutomation, meaning that the return value of your interface must be HRESULT, the type of parameters used should also be compatible with the C Variant structure.

2. Create a Stub / Proxy DLL

The source of this DLL is produced by MIDL. You must register this DLL on the server and client (this is a standard Marshal method) When you use a method of eating, it is best to compile STUB / Proxy code as a separate component. 3. Customize Marshaling

Custom Marshal requires the Imarshal interface in your components. When COM requires Marchal, he first looks through QueryInterface you support the imarshal interface, if you implement the interface, that is, controlled all the parameters of your COM and the package, unpacking method mode.

Agent and Stub DLL establishment

Use tool MIDL, for an IDL file, MIDL will analyze the relevant files that automatically generate the corresponding proxy / stub DLL.

How to use proxy and stub

For you, the use of proxy and stub is transparent, you don't have to care about how to use them, COM library knows how to do it.

[Note: This paper takes from MSDN]

Welcome to read this article, and leave valuable comments, the discussion will make problems more deeper, if you want to quote this article, please leave my name. Thank you, make progress together.

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

New Post(0)