Using array parameters in COM - Overview
Keywords: DCOM, array, custom type, Marshal, SafeArray, iCollection
This article describes three methods using arrays as parameters in the interface of COM. They are: array pointers, SafeArray and Icollection. The article analyzes the advantages and disadvantages of various methods. The purpose of this paper is not to describe the basic principles and development methods of COM. In order to better understand the content in this article, the reader needs to have basic COM programming knowledge.
1 related fundamental concept
In COM, if the call to the method in the interface is a hurdle, the call must be serialized. Sequentially, which Kangzui dumps are fascinating, 谡 准 准 蟹 ⑸ ⑸ 蛄 蛄 蛩阆 枋鎏 浜 浜 浜 浜 浜 蛄 蛄 睦 浜 浜 浜 浜 浜 睦 郏 郏 睦 郏 睦 睦 睦Xingchao? Lt; / span>
1.1 suites (Apartment)
In one process, multiple suites can be included, each suite can contain one or more threads [1]. A suite of a single thread is called a single-line program; a set of sets of multiple threads is called multi-threaded suites. In a process, you can include a multi-threaded suit, but you can have 0 or more single-wires. Each use of COM's thread, whether the client is or the COM program, you need to enter the set of Coinitialize or the CoinitializeEx function, and determine the type of the suite. You cannot use the COM function before entering the socket, otherwise it will result in an error result.
1.2 serialization (Marshal)
In the COM architecture, the caller and the caller can not be called directly, but must be called by the proxy and placeholder (STUB) program. The agent and caller are in the same suite, while the placeholder program and the caller are in the same suite. The agent mimics the behavior of the COM component, accepts call, and the occupancy program mimics the caller's behavior to send calls. This will ensure that the call is carried out in the same suite. The method, parameter, and return value passed through a specific network communication protocol between proxy and placeholders. The process of converting the call into a network protocol is called serialization. Due to the parameters of the passage of the pointer or array type, the serialization process is very complicated. Fortunately, we have a simple way to generate efficiency is also a good agent and placeholder program.
For pointer types, the specific pointer addresses are not important, and the key is the data in memory pointed to by the pointer. Therefore, when the parameter of the pointer type is transmitted, the pointed data must be passed. There is also a similar case for array type data. If there is a double or multiple pointer, the situation will become more complicated.
For example, a line * type parameter. In serialization, the agent passes the long integer value points to the parameter to the placeholder program, and the placeholder program is applied for memory for the parameter, and then puts the long-integrated value to the application, use this new The memory address is called the target function as a parameter. When the function returns, the data change in the memory is passed back to the agent. The agent copies the data back to the call, and then returns. If the data pointed to by the pointer is not a single value, but a unproductable size memory, when serialization, determine the length of the data to pass. In addition, in the case of multiple pointers, it is not a piece of data to be transmitted, but may be multi-block data segments.
In the discussion later, we will explain how to generate the correct proxy and placeholder program in detail.
1.3 Agent (Proxy) and Stubs (STUB)
COM enables serialization functions through proxy and stub. Each interface that may be invoked across a set must have a corresponding proxy and stub program. The proxy and stub program are in the same dynamic connection library. Designers per COM interface are responsible for implementing their own proxy and stub. Under normal circumstances, the code of the agent and the stub program can be automatically generated by a tool called MIDL, and we have to do it just to compile it.
The method of working with the agency and stub is as follows:
The role of the agent is "camouflage" into a COM object in the customer suite for customer programs. The role of the stub is "camouflage" into a customer program in the suite in which the COM object is located, issues a call request. The call request and return result is exchanged between the proxy and stubs [2]. 2 outline
Use an array in COM to use three methods: array pointers, SafeArray, and Icollection. The modal pointer and the method of passing the array in the familiar C / C program are the same, SafeArray is a method of standard the standard storage array in VB. It is also a standard method in Automation. The iCollection method is passed through a separate COM object. These three methods have advantages and disadvantages, which should be determined which method used in accordance with the specific needs.
2.1 array pointer
An array pointer is the array parameter transmission mode in the standard C / C . The array pointer is actually the first address when the array element serialization is stored. The operation of array pointers is very simple, so it is also the highest efficiency. However, this way cannot be used in VB. An array pointer can pass a one-dimensional array or a multi-dimensional array. This is the best way to transfer if the client client is a VC program.
When the array pointer is used as a call parameter between the sprinkles, Marshal is required. Therefore, you should compile and register Proxy / Stub.
2.2 SafeArray
SafeArray is a standard VB array store. Similar to array pointers, SafeArray can pass a one-dimensional array or pass a multi-dimensional array. Since SafeArray has a more complex structure than array pointers, the programming is complicated than using array pointers [3], and the program running efficiency is relatively low.
Using the array passed in the SafeArray mode, you can call from the VB program or call from the VC program. Moreover, since SafeArray is the standard data of Automation, it is possible to make a cross-sleeve-based call through the default TLB-based proxy / stub without having to compile and register your own proxy / stub.
2.3 ICollection
The iCollection method is the most complicated and the most widely used. ICollection is not a name of an interface, but refers to an IDSPATCH interface that implements an enumerator and an index property. This array transmission method is characterized by achieving array objects, so there is greatest flexibility, you can implement advanced features such as array elements on demand.
The array objects transmitted by Icollection are no longer a normal pointer or a specific structure, but a separate COM object. Since the interface is transmitted, the parameter has an object-oriented polymorphic feature, that is, the array element can be implemented, or other people can be implemented, as long as the IDispatch interface with a specific attribute can be used as a parameter. On the other hand, since the COM object used as an array can be designed separately, it can be implemented using a more reasonable implementation, such as using a list, a Hash table, or a balance tree.
Some array implementations, only need to access a few elements, or the number of elements is inferior, or the generation of each array element requires a lot of resources. At this point, you should use the iCollection to implement array delivery.
3 Comparison of various methods
Array pointer
SafeArray
ICollection
Coded quantity is less compatible with Automation is not compatible with compatible development tools VC VC / VB VC reusability low and low high-aspiration generation element can not be able to model design difficulty easy to easily work speed is slow, depending on the actual situation analysis distribution Applying RPC RPC RPC or object replication design flexibility can be high
[1] In the latest COM standards, some threads may cross the suit, and this is not discussed here. [2] actually based on a protocol in the Remote Process Call Protocol (RPC).
[3] Refers to programming with VC , if COM is implemented with VB, this is the only way to pass.