poco
poco

Reputation: 3005

importing a c dll function into c#

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?

Upvotes: 0

Views: 295

Answers (3)

pstrjds
pstrjds

Reputation: 17448

Try this:

extern short Function(ushort a, UInt32 b, UInt32 c, ushort[] buffer)

Upvotes: 0

Liviu Mandras
Liviu Mandras

Reputation: 6627

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

Upvotes: 0

Pablo Retyk
Pablo Retyk

Reputation: 5758

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);

Upvotes: 2

Related Questions