Hi,
I would like to pass an array of formatted blittable classes through
P/Invoke to a C++ dll, but it is not working. Here are the definitions:
C++
struct Point2Rec
{
double x;
double y;
};
void WINAPI AddPolyToList(Point2Rec* pg1, int numPts);
C#
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public class Point2D
{
private double _x;
private double _y;
public double X { get { return _x; } set { _x = value; } }
public double Y { get { return _y; } set { _y = value; } }
}
[DllImport("ascBool.dll")]
public static extern void AddPolyToList([In, Out] Point2D[] pg1, int
numPts);
If I change "class" to "struct" it works. I am taking my information
from the following documentation:
http://msdn2.microsoft.com/en-us/library/23acw07k(VS.80).aspx
Is there something I'm missing in the definitions of formatted and
blittable, or is it just not possible to pass an array of formatted
blittable classes to a C++ dll.
Thanks
Jeroen Mostert - 27 Mar 2008 00:48 GMT
> I would like to pass an array of formatted blittable classes through
> P/Invoke to a C++ dll, but it is not working. Here are the definitions:
[quoted text clipped - 26 lines]
>
> If I change "class" to "struct" it works.
Quite so. So your problem is...? Do you feel that using a class here gives
you an advantage over a struct?
The reason it doesn't work when you use "class" is because it turns Point2D
into a reference type. An array of reference types is marshaled as an array
of pointers (to COM proxies, in this case).
> I am taking my information
> from the following documentation:
>
> http://msdn2.microsoft.com/en-us/library/23acw07k(VS.80).aspx
The link to "Blittable and Non-Blittable Types" mentions (indirectly) that
reference types (including most classes) are not blittable and will be
converted in some way.
"Default Marshaling for Classes"
(http://msdn2.microsoft.com/en-us/library/s0968xy8(VS.80).aspx) and "Default
Marshaling vor Value Types"
(http://msdn2.microsoft.com/en-us/library/0t2cwe11(VS.80).aspx) provide more
insight.

Signature
J.