How to construct a safearray with element type struct

Source: Internet
Author: User

In some cases, we need to construct a safearray with the element type struct. The msdn document does not explain how to do this, the following code snippet explains how to construct such a safearray.

Suppose we have the following struct:

Struct mystruct

{

Unsigned char name [255];

Short kind;

};

 

To construct a safearray with the element type mxstruct, we must first obtain the irecordinfo interface corresponding to mxstruct. This can be achieved by calling the getrecordinfofromguids function:

# Import "teststruct. TLB" no_namespace

 

Hresult hr;

Irecordinfo * precordinfo;

HR = getrecordinfofromguids (

_ Uuidof (teststruct ),

1,

0,

Locale_user_default,

_ Uuidof (mxstruct ),

& Precordinfo );

 

Getrecordinfofromguids queries the corresponding record information in the registry. The Registry information is located under hkcr/record, and the corresponding typelib must also be registered under hkcr/typelib, in this way, getrecordinfofromguids can find the corresponding information and return the irecordinfo * pointer. When this pointer is obtained, you can use createsafearrayvectorex to construct safearray:

Safearray * parray = safearraycreatevectorex (vt_record, 0, 3, precordinfo );

 

This row calls safearraycreatevectorex to construct a structure specified by the element precordinfo, that is, the safearray of mystruct, lowbound is 0, and the number of elements is 3.

You can assign values to the elements in this safearray using safearrayaccessdata and safearrayunaccessdata:

Mystruct * pstructs;

Safearrayaccessdata (parray, (void **) & pstructs );

 

Strcpy (char *) & pstructs [0]. name [0], "N1 ");

Pstructs [0]. Kind = 0;

 

Strcpy (char *) & pstructs [1]. name [0], "N2 ");

Pstructs [0]. Kind = 1;

 

Strcpy (char *) & pstructs [2]. name [0], "N3 ");

Pstructs [0]. Kind = 2;

 

Safearrayunaccessdata (parray );

 

Safearrayaccessdata obtains the array pointer, which is used to modify data and lock the safearray to prevent the safearray from being released. While safearrayunaccessdata unlocks this safearray.

Now the safearray construction is complete and can be passed to other COM components. If you are interested, you can refer to a similar question raised in msdn Forum:

Http://forums.microsoft.com/MSDN/ShowPost.aspx? Postid = 1994951 & siteid = 1 & mode = 1

 

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.