473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

passing array of structs to unmanaged code

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[i].n;
for (j = 0; j <= n; j++)
p[j] = pbr.pPt[pbr.pPgon[i].ndx + j];
}
C#
[DllImport("xform300.dll", EntryPoint = "GeomBoolGetPgon")]
public static extern void BoolGetPgon(PgonBoolRec pbr, int i, PointF[]
pg, out int n);
List<Polygon2FDoBooleanOp(BooleanType bt, Polygon2F pgon, float tol,
float faraway)
{
List<Polygon2Fpgons = 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[i] = 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;
}
Jun 27 '08 #1
2 4339
Please note, in the code below PgonBoolRec is a class, if I change it to
a struct and pass by 'ref' it works as well, although the array of
PointF structs still returns zeroed values.

Thanks for any help.
jonpb wrote:
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[i].n;
for (j = 0; j <= n; j++)
p[j] = pbr.pPt[pbr.pPgon[i].ndx + j];
}
C#
[DllImport("xform300.dll", EntryPoint = "GeomBoolGetPgon")]
public static extern void BoolGetPgon(PgonBoolRec pbr, int i, PointF[]
pg, out int n);
List<Polygon2FDoBooleanOp(BooleanType bt, Polygon2F pgon, float tol,
float faraway)
{
List<Polygon2Fpgons = 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[i] = 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;
}
Jun 27 '08 #2
jonpb wrote:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++
unmanaged function.
I tried using ref IntPtr and using Marshal.AllocCoTaskMem etc. but again
that crashed the program. I finally got it to work by adding [In, Out]
to the parameter signature.

While, I'm glad it is now working, from what I can tell from the MSDN
site, this is not the correct way to do it you should either use [In]
array[], or, ref IntPtr.

Are [In, Out] array[] parameters supported by P/Invoke? or this going to
work on my machine and blowup on someone else's?

Thanks
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Claire | last post by:
After giving up on passing nested structs to an unmanaged DLL, I planned that I'd pass a simple buffer of bytes (the same size as the struct) to the dll, convert to a memory stream and read in the...
1
by: Steve Baer | last post by:
I'm wrapping some unmanaged C++ classes with managed versions for use in the ..NET world. Everything is going great except I can't figure out a good method for passing simple data type arrays into...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
4
by: Andreas Reiff | last post by:
Hi! I want some communication to take place between a c++ app and a c++ .dll with an intermediate managed/unmanaged c++ dll. Basicall, I want the unmanaged c++ dll to have a callback to the...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
3
by: cskarp | last post by:
I have create a .Net component which exposes an interface with two methods. The first method takes an array of structs as a parameter the other method returns an array of (the same tyoes of) structs....
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.