marshal c++ reference to int

J

jonpb

Hi, I cannot find the correct attributes to marshal the following
unmanaged function signature:

void WINAPI GeomVec2AngleD(const Vector2RecD& v1, const Vector2RecD& v2,
double& angle)

I thought this would work:

[DllImport("xform400.dll")]
public static extern void GeomVec2AngleD([In] Point2D v1, [In] Point2D
v2, [In, Out] double angle);

Point2D's layout maps directly to Vector2RecD, there is no problem
there, the problem is, as soon as the C++ code attempts to write to
angle it throws a System.AccessViolationException.

I tried a few more possible solutions but I cannot find the correct one.

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

jonpb,

You can not use references through the P/Invoke layer.

Rather, you will have to expose a function which takes pointers instead,
and marshal that function. Then, your function would just make the call to
the original function.
 
B

Ben Voigt [C++ MVP]

jonpb said:
Hi, I cannot find the correct attributes to marshal the following
unmanaged function signature:

void WINAPI GeomVec2AngleD(const Vector2RecD& v1, const Vector2RecD& v2,
double& angle)

I thought this would work:

[DllImport("xform400.dll")]
public static extern void GeomVec2AngleD([In] Point2D v1, [In] Point2D v2,
[In, Out] double angle);

Point2D's layout maps directly to Vector2RecD, there is no problem there,
the problem is, as soon as the C++ code attempts to write to angle it
throws a System.AccessViolationException.

I tried a few more possible solutions but I cannot find the correct one.

You should use "ref" on the angle parameter.
 

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