Using ref Array parameters in C # with COM interoperability

I have a third-party COM library that I consume and I have problems with array parameters.

The signature of the method I'm calling is as follows:

int GetItems(ref System.Array theArray)

The documentation says that the return value of the method is the number of elements that it will fill in the array, but when it is called, all the values ​​in the array are the default values ​​(they are structures), although the method returns a non-zero return value.

I know that there is some kind of funky COM material here, but I really don’t know much about this and can’t understand. This is how I tried to access it:

Array items = Array.CreateInstance(typeof(structItem), 100);
int numberOfItems = instance.GetItems(items);

Array items = Array.CreateInstance(typeof(structItem), 100);
int numberOfItems = instance.GetItems(ref items);

structItem[] items = new structItem[100];
int numberOfItems = instance.GetItems(items);

structItem[] items = new structItem[100];
int numberOfItems = instance.GetItems(ref items);

What am I doing wrong?

UPDATE: , - SafeArrays, : http://www.west-wind.com/Weblog/posts/464427.aspx , ref, . , , .

+5
1

, - Interop, , , COM. Marshal, Marshal.AllocHGlobal (, , FreeHGlobal, , ).

- :

IntPtr p = Marshal.AlloHGlobal(items.Length * Marshal.SizeOf(typeof(structItem));  
Marshal.Copy(items, 0, p, items.Length);  
0

All Articles