0

I am trying to use a function in C# from an unmanged C dll. I'm new to C# and am unsure if I'm doing this correctly. The function in C looks something like this:

unsigned short Function(unsigned short, unsigned long, unsigned long, unsigned short*);


[DllImport("cDLLfile.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern short Function(ushort a, UInt32 b, UInt32 c, IntPtr buffer);

The buffer is an array something like

ushort[] = new ushort[7];

I fill the array then try to pass it to Function and am getting a error. I know IntPtr is not right. What is the correct way to do this?

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
poco
  • 2,935
  • 6
  • 37
  • 54

3 Answers3

2

It should work with ushort[]

[DllImport("cDLLfile.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true) ]
private static extern short Function(ushort a, UInt32 b, UInt32 c, ushort[] buffer);
Pablo Retyk
  • 5,690
  • 6
  • 44
  • 59
0

Try this:

extern short Function(ushort a, UInt32 b, UInt32 c, ushort[] buffer)
pstrjds
  • 16,840
  • 6
  • 52
  • 61
0

Here you will find details and sample code on how to marshal arrays.

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65