473,581 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("xfor m300.dll", EntryPoint = "GeomBoolGetPgo n")]
public static extern void BoolGetPgon(Pgo nBoolRec pbr, int i, PointF[]
pg, out int n);
List<Polygon2FD oBooleanOp(Bool eanType bt, Polygon2F pgon, float tol,
float faraway)
{
List<Polygon2Fp gons = new List<Polygon2F> ();

Geom.PgonBoolRe c pbr = new Geom.PgonBoolRe c();

int i = 0;
PointF[] pg1 = new PointF[pgon.Points.Cou nt];
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.BoolPgonPg on(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.BoolGetPgo n(pbr, i, pg, out n);
pgons.Add(new Polygon2F(pg));
}
}
}
finally
{
Geom.BoolCleanu p(pbr);
}

return pgons;
}
Jun 27 '08 #1
2 4352
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("xfor m300.dll", EntryPoint = "GeomBoolGetPgo n")]
public static extern void BoolGetPgon(Pgo nBoolRec pbr, int i, PointF[]
pg, out int n);
List<Polygon2FD oBooleanOp(Bool eanType bt, Polygon2F pgon, float tol,
float faraway)
{
List<Polygon2Fp gons = new List<Polygon2F> ();

Geom.PgonBoolRe c pbr = new Geom.PgonBoolRe c();

int i = 0;
PointF[] pg1 = new PointF[pgon.Points.Cou nt];
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.BoolPgonPg on(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.BoolGetPgo n(pbr, i, pg, out n);
pgons.Add(new Polygon2F(pg));
}
}
}
finally
{
Geom.BoolCleanu p(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.AllocCo TaskMem 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
2514
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 fields from that. I've 2 possible DLls to use, one is emulated and the other one is the "real" one The emulated one (that I have to use on a...
1
1513
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 unmanaged classes. Say I have a function void UnManagedClass::FillOutArray(int size, int* data); where data is an array of unmanaged ints and...
17
3669
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 compile i get the following error: Attempted to read or write protected memory the C function should not be manipulating the input arra, only...
7
6418
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 now thown out of the array released properly by the CLI?
4
5666
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 unmanaged c++ app. Also, the managed c++ part should call the c# dll. I don't mind about where the array is created, but I need to be able to pass it...
17
7231
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 to show the array data to the end user. Can I do that? How?
3
4417
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. When I get the array structures from my second method in unmanaged c++, I cannot regonize any of the parameters in the structures. But if I pass...
6
8958
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 must be allocated (with known max length = 10) before the call to the dll function (the dll just fills it ,with no allocations). The definitions of...
0
1651
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 it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of...
0
7882
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5683
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.