How to create a SafeArray C#?

4

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?

c#
com
interop
com-interop
safearray
asked on Stack Overflow May 4, 2011 by shane87 • edited May 5, 2011 by shane87

1 Answer

8

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.

answered on Stack Overflow May 4, 2011 by Eugen

User contributions licensed under CC BY-SA 3.0