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