passing a vector of doubles

  • Thread starter Thread starter Henrique Bucher
  • Start date Start date
H

Henrique Bucher

Hi

Kind of newbie question:

We have a DLL with a function with a prototype like

void mult( int mtxhandle, double* x, double* y, int size );

where "size" is the size of both x and y vectors. What should be the correct
sintax to define its respective DllImport wrapper in order to use it like:

Double x[] = new Double[mysize];
Double y[] = new Double[mysize];

mult( handle, x, y, mysize );

Thank you

Henrique
 
Henrique,
What should be the correct
sintax to define its respective DllImport wrapper

static extern void mult(int mtxhandle, double[] x, double[] y, int
size);



Mattias
 
Hi Mattias,

That does not work since the compiler complains with
C2146: syntax error : missing ')' before identifier 'x'

if you try
double x[] instead of double[] x
then you get
C2664: 'mult':cannot convert parameter 2 from 'double __gc[]' to ' double
[]'

I'm looking into
[DllImport("libbmc")]
extern "C" int mult( int handle, System::IntPtr x, System::IntPtr y, int
sign );

GCHandle xh = GCHandle::Alloc( x, GCHandleType::Pinned );
IntPtr xptr = Marshal::UnsafeAddrOfPinnedArrayElement( x, 0 );
(same for yh)

int result = mult( handle, xptr, yptr, sign );

xh.Free();
yh.Free();

but not sure if this is the right way to do it. Indeed, it looks like the
pair GCHandle::Alloc and GCHandle::Free will have a big impact on
performance.

Henrique

Mattias Sjögren said:
Henrique,
What should be the correct
sintax to define its respective DllImport wrapper

static extern void mult(int mtxhandle, double[] x, double[] y, int
size);



Mattias
 
Henrique,

Since the name of this group is
microsoft.public.dotnet.languages.csharp, I assumed you were coding in
C#. Now I see you are actually writing managed C++ code.

I believe the MC++ syntax would be

int mult( int handle, Double x[], Double y[], int sign );

or

int mult( int handle, double x __gc[], Double y __gc[], int sign );

But you may want to post your question in
microsoft.public.dotnet.languages.vc instead to get a definite answer.

I'm looking into
[DllImport("libbmc")]
extern "C" int mult( int handle, System::IntPtr x, System::IntPtr y, int
sign );

GCHandle xh = GCHandle::Alloc( x, GCHandleType::Pinned );
IntPtr xptr = Marshal::UnsafeAddrOfPinnedArrayElement( x, 0 );
(same for yh)

int result = mult( handle, xptr, yptr, sign );

xh.Free();
yh.Free();

Pinning can be easier than that C++. You should also be able to do

extern "C" int mult( int handle, double *x, double *y, int sign );

and then

Double __pin *xp = &x[0];
Double __pin *yp = &y[0];
int result = mult( handle, xp, yp, sign );



Mattias
 
So, there's no need for explicitly acquiring a handle to this memory?
The simplicity of the c# wrapping was simply astonishing. I was expecting
something a lot more complicated.... :)
that's good though
thanks a lot!
Henrique
 

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

Back
Top