Call C DLL in C# with in/out pointers

J

J.M.

Hi,

I need help.

I want use this API

LIB_EXTERN_C LIB_API long STDCALL method(
long id,
const double *vIN,
double *vOUT);

I use this syntaxe in c#

[DllImport("LIB.dll", EntryPoint="_method@12")]
public static extern long method(long idMethode, ref double [] vIn,
ref double[] vOUT);

double [] vIn = new double[10];
double [] vOUT= new double[10];

Main.method(1, ref vIn, ref vOUT);

I receive no error but the results it's bad...
vIn.Length == 1 and vOUT.Length == 1 and containt no data.

Thanks
 
B

Ben Voigt

J.M. said:
Hi,

I need help.

I want use this API

LIB_EXTERN_C LIB_API long STDCALL method(
long id,
const double *vIN,
double *vOUT);

I use this syntaxe in c#

[DllImport("LIB.dll", EntryPoint="_method@12")]
public static extern long method(long idMethode, ref double [] vIn,
ref double[] vOUT);

Arrays are already pass-by-reference, you should try without "ref". The
function will not be able to resize the array (how could it? It knows
nothing about memory allocation in .NET)
double [] vIn = new double[10];
double [] vOUT= new double[10];

Main.method(1, ref vIn, ref vOUT);

I receive no error but the results it's bad...
vIn.Length == 1 and vOUT.Length == 1 and containt no data.

Thanks
 
J

J.M.

If I removed the ref key word, I get no errors and no resizes on arrays
but the vOut array is empty.

I need help.
I want use this API
LIB_EXTERN_C LIB_API long STDCALL method(
long id,
const double *vIN,
double *vOUT);
I use this syntaxe in c#
[DllImport("LIB.dll", EntryPoint="_method@12")]
public static extern long method(long idMethode, ref double [] vIn,
ref double[] vOUT);Arrays are already pass-by-reference, you should try without "ref". The
function will not be able to resize the array (how could it? It knows
nothing about memory allocation in .NET)


double [] vIn = new double[10];
double [] vOUT= new double[10];
Main.method(1, ref vIn, ref vOUT);
I receive no error but the results it's bad...
vIn.Length == 1 and vOUT.Length == 1 and containt no data.
 
W

Willy Denoyette [MVP]

J.M. said:
Hi,

I need help.

I want use this API

LIB_EXTERN_C LIB_API long STDCALL method(
long id,
const double *vIN,
double *vOUT);

I use this syntaxe in c#

[DllImport("LIB.dll", EntryPoint="_method@12")]
public static extern long method(long idMethode, ref double [] vIn,
ref double[] vOUT);

double [] vIn = new double[10];
double [] vOUT= new double[10];

Main.method(1, ref vIn, ref vOUT);

I receive no error but the results it's bad...
vIn.Length == 1 and vOUT.Length == 1 and containt no data.

Thanks


1. long in C# is 64 bit, long in C is 32 bit.
2. double *vOUT is a pointer to a double, you are passing a reference to an array which
results in extra dereferencing.

So your signature should look like....

public static extern int method(intidMethode, double [] vIn,
double[] vOUT);
and you should call it using...

Main.method(1, vIn, vOUT);

Willy.
 
J

J.M.

Thanks,

The function execute perfectly.

"long in C == int in C# " ;-)

I need help.
I want use this API
LIB_EXTERN_C LIB_API long STDCALL method(
long id,
const double *vIN,
double *vOUT);
I use this syntaxe in c#
[DllImport("LIB.dll", EntryPoint="_method@12")]
public static extern long method(long idMethode, ref double [] vIn,
ref double[] vOUT);
double [] vIn = new double[10];
double [] vOUT= new double[10];
Main.method(1, ref vIn, ref vOUT);
I receive no error but the results it's bad...
vIn.Length == 1 and vOUT.Length == 1 and containt no data.
Thanks1. long inC#is 64 bit, long inCis 32 bit.
2. double *vOUT is apointerto a double, you are passing a reference to an array which
results in extra dereferencing.

So your signature should look like....

public static extern int method(intidMethode, double [] vIn,
double[] vOUT);
and you should call it using...

Main.method(1, vIn, vOUT);

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top