COM component development notes-transfer of string Arrays

Source: Internet
Author: User

The advantage of COM programming is that it can be used across languages. Our company uses C # for development, so Developing COM components with C ++ can make up for the lack of graphic rendering performance. However, the COM component also has its disadvantages, that is, the calling overhead of the COM interface itself is relatively large, so I have summarized that it is best to use the COM component to achieve the goal through the least number of interface calls. Therefore, the transfer of arrays is crucial. For example, call setx for 10000 times (
Double X) It takes more time than one-time passing the array X into setx (double [] X.

 An array of common value types, such as int [], short [], and so on. com has corresponding types (vts_pi4 vts_pi2 ), in addition, even in C #, you can use marshal to allocate unmanaged arrays and then pass them to com for operations. However, different string arrays have different lengths, which makes it difficult to pass. So today we will talk about the transmission of string arrays.


 

 To pass arrays in COM, we need to use a stuff called safearray to wrap it. However, if safearray is directly used for transmission, the parameter type in C # Is intptr or a pointer, which makes data acquisition inconvenient. Later, we found that we had to use the variant data structure to wrap the safearray again before passing the array. In this way, after the data is uploaded to C #, the parameter type is object, you only need to use string [] to perform a strong conversion. This is very convenient. As a matter of fact, I have clearly described Baidu encyclopedia in this section. For details, refer:Http://baike.baidu.com/view/1907445.htm

 As we can understand, safearray is the array for storing data, and variant is a shell, just like the packing concept in C #. It is easier to pass variables into objects. I have mentioned enough. The following code is used as an example.

  

 First, if the array is an input parameter, the scheduling declaration must useVts_variant: In the function prototype declaration, the type is variant *; In the return value, the scheduling declaration must use vt_variant. In the function prototype declaration, the type is variant (note that it is not variant *, which is a little different from the input parameter, otherwise, the parameter type in C # is not an object but an intptr). Next, we will mainly discuss the structure and packaging of arrays.

  Variant arywarp;

 Variantinit (& arywarp);

  Arywarp. Vt = vt_array | vt_bstr;
// The specified shell is used to wrap the array and is a string array. Note that the string in COM is BSTR, which is actually a wide string.


 

  Safearray * PSA;

 Safearraybound bounds = {10, 0 };

 PSA = safearraycreate (vt_bstr, 1, & bounds );// Initialize a string array with a length of 10.


 

 Then declare a String ArrayBSTR * bstrarray;And use safearrayaccessdata to bind PSA to bstrarray (I don't know if it is appropriate to bind this word, but it gives me the feeling that it is bound). Then what I need to do is to bind bstrarray [
I] The value is assigned. After the value is assigned, remember to use safearrayunaccessdata to unbind it.

 The last step is the shell,
Arywarp. parray = psa; then you can return arywarp.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.