J
jonpb
Using .NET 3.5, I need to pass an array of structs as parameter to a C++
unmanaged function.
The C++ dll stores some data in an unmanaged cache, the function writes
the values into the array of structs. The array of structs are allocated
by C#.
If I pass the array without 'ref' it works fine on the C++ side, but
after returning to C# the array struct values are all zero. If I pass
with 'ref' the application crashes. If I use a class instead of a
struct, the application crashes as well. The struct array I am passing
is an array of:
System.Drawing.PointF
Also, note that the structs passed not in an array work and the values
are updated.
Here is some code:
C++:
void WINAPI GeomBoolGetPgon(const PgonBoolRec & pbr, int i, Point2Rec *
p, int & n)
{
int j;
if ((i < 0) || (i > pbr.np))
{
n = -1;
return;
}
n = pbr.pPgon.n;
for (j = 0; j <= n; j++)
p[j] = pbr.pPt[pbr.pPgon.ndx + j];
}
C#
[DllImport("xform300.dll", EntryPoint = "GeomBoolGetPgon")]
public static extern void BoolGetPgon(PgonBoolRec pbr, int i, PointF[]
pg, out int n);
List<Polygon2F> DoBooleanOp(BooleanType bt, Polygon2F pgon, float tol,
float faraway)
{
List<Polygon2F> pgons = new List<Polygon2F>();
Geom.PgonBoolRec pbr = new Geom.PgonBoolRec();
int i = 0;
PointF[] pg1 = new PointF[pgon.Points.Count];
foreach (Point2F pt in pgon.Points)
pg1[i++] = new PointF(pt.X, pt.Y);
i = 0;
PointF[] pg2 = new PointF[_pts.Count];
foreach (Point2F pt in _pts)
pg2[i++] = new PointF(pt.X, pt.Y);
try
{
int btype = (int)bt;
int n = pbr.maxN;
int ret = Geom.BoolPgonPgon(btype, pg1, pg1.Length, pg2, pg2.Length, pbr);
if ((pbr.np > -1) && (ret == 0))
{
PointF[] pg = new PointF[pbr.maxN];
for (i = 0; i < pbr.maxN; ++i)
pg = new PointF();
for (i = 0; i <= pbr.np; ++i)
{
n = pbr.maxN;
Geom.BoolGetPgon(pbr, i, pg, out n);
pgons.Add(new Polygon2F(pg));
}
}
}
finally
{
Geom.BoolCleanup(pbr);
}
return pgons;
}
unmanaged function.
The C++ dll stores some data in an unmanaged cache, the function writes
the values into the array of structs. The array of structs are allocated
by C#.
If I pass the array without 'ref' it works fine on the C++ side, but
after returning to C# the array struct values are all zero. If I pass
with 'ref' the application crashes. If I use a class instead of a
struct, the application crashes as well. The struct array I am passing
is an array of:
System.Drawing.PointF
Also, note that the structs passed not in an array work and the values
are updated.
Here is some code:
C++:
void WINAPI GeomBoolGetPgon(const PgonBoolRec & pbr, int i, Point2Rec *
p, int & n)
{
int j;
if ((i < 0) || (i > pbr.np))
{
n = -1;
return;
}
n = pbr.pPgon.n;
for (j = 0; j <= n; j++)
p[j] = pbr.pPt[pbr.pPgon.ndx + j];
}
C#
[DllImport("xform300.dll", EntryPoint = "GeomBoolGetPgon")]
public static extern void BoolGetPgon(PgonBoolRec pbr, int i, PointF[]
pg, out int n);
List<Polygon2F> DoBooleanOp(BooleanType bt, Polygon2F pgon, float tol,
float faraway)
{
List<Polygon2F> pgons = new List<Polygon2F>();
Geom.PgonBoolRec pbr = new Geom.PgonBoolRec();
int i = 0;
PointF[] pg1 = new PointF[pgon.Points.Count];
foreach (Point2F pt in pgon.Points)
pg1[i++] = new PointF(pt.X, pt.Y);
i = 0;
PointF[] pg2 = new PointF[_pts.Count];
foreach (Point2F pt in _pts)
pg2[i++] = new PointF(pt.X, pt.Y);
try
{
int btype = (int)bt;
int n = pbr.maxN;
int ret = Geom.BoolPgonPgon(btype, pg1, pg1.Length, pg2, pg2.Length, pbr);
if ((pbr.np > -1) && (ret == 0))
{
PointF[] pg = new PointF[pbr.maxN];
for (i = 0; i < pbr.maxN; ++i)
pg = new PointF();
for (i = 0; i <= pbr.np; ++i)
{
n = pbr.maxN;
Geom.BoolGetPgon(pbr, i, pg, out n);
pgons.Add(new Polygon2F(pg));
}
}
}
finally
{
Geom.BoolCleanup(pbr);
}
return pgons;
}