Marshaling a SAFEARRAY of Managed Structures by COM Interop

0

i am trying to pass an array of struct exported from a c# library to c++ code. the objective is to pass SAFEARRAY of struct from c++ to c#.

I have followed instructions from

http://limbioliong.wordpress.com/2011/07/16/marshaling-a-safearray-of-managed-structures-by-com-interop-part-2/

but getting this error with the call GetRecordInfoFromTypeInfo

0x80028019 Old format or invalid type library.

c#
c++
com
safearray
asked on Stack Overflow Sep 21, 2012 by TrustyCoder

2 Answers

1

If you don't need to have a dispinterface, following should work:

void MyMethod([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] MyStruct[] data, long size);

If you need to use SAFEARRAY, I would expect following to work (but I am not 100% sure, as I don't have Windows machine available at the moment):

void MyMethod([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_USERDEFINED)] MyStruct[] data);

If you can afford to change your struct to class then this will also work, and save you from UDT hassle:

void MyMethod([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_UNKNOWN)] MyStruct[] data);
answered on Stack Overflow Sep 21, 2012 by Zdeslav Vojkovic
1

Does the struct contains strings? If so, make sure to tag them [MarshalAs(UnmanagedType.BStr)]. There is a limitation in COM: strings in arrays of structs must be BSTRs. TLBExp defaults to LPWSTR and then the call dies. See here: http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.100).aspx

answered on Stack Overflow Oct 16, 2012 by JurekM

User contributions licensed under CC BY-SA 3.0