How to create a SafeArray C#?

shane87 picture shane87 · May 4, 2011 · Viewed 10.3k times · Source

I need to create a SafeArray to pass to a COM method.
How do I create/maintain/destroy a SafeArray in C#?

I have never came across SafeArrays before and could not find much with a quick google search, so any help is much appreciated.

EDIT: Added Sample Code:

The COM method signature

[id(0x000000d5)]
HRESULT GetTags(
                [in] SAFEARRAY(long) buffer, 
                [out, retval] long* retval);

The generated interop method in C#

int GetTags(System.Array buffer)
    Member of Cwise.IUser

So in this case do I have to create a SafeArray or can I simply pass a normal .Net Array to the COM method GetTags?

Answer

Eugen picture Eugen · May 4, 2011

use such a code for this

Array ar = Array.CreateInstance(typeof (int), 500);

instead of typeof(int) use your own data type, your COM object must say you what type is expecting.