473,563 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing an array of structs to unmanaged dll

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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error was at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error may
be a bug in the CLR or in the unsafe or non-verifiable portions of user code.
Common sources of this bug include user marshaling errors for COM-interop or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?
Nov 11 '07 #1
6 8955
Alexanderfe,

Can you give the original function and structure declaration in C? The
reason I ask is because I want to know if the Reserved field on the MyStruct
structure is embedded in the structure, or a pointer to another array.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alexanderf e" <Al*********@di scussions.micro soft.comwrote in message
news:36******** *************** ***********@mic rosoft.com...
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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed
struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error was
at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error
may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code.
Common sources of this bug include user marshaling errors for COM-interop
or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?
Nov 11 '07 #2
Thanks for the reply,
It's embedded

typedef struct Mystruct{
LONGINT Id;
LONGINT Type;
TimeTag StartTime;
TimeTag EndTime;
LONGINT Reserved[2];
};

"Nicholas Paldino [.NET/C# MVP]" wrote:
Alexanderfe,

Can you give the original function and structure declaration in C? The
reason I ask is because I want to know if the Reserved field on the MyStruct
structure is embedded in the structure, or a pointer to another array.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alexanderf e" <Al*********@di scussions.micro soft.comwrote in message
news:36******** *************** ***********@mic rosoft.com...
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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed
struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error was
at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error
may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code.
Common sources of this bug include user marshaling errors for COM-interop
or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?
Nov 11 '07 #3
I've forgot to mention that when I use structs that consist only of Int32
fields in the same manor, all works fine and no exception is thrown.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Alexanderfe,

Can you give the original function and structure declaration in C? The
reason I ask is because I want to know if the Reserved field on the MyStruct
structure is embedded in the structure, or a pointer to another array.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alexanderf e" <Al*********@di scussions.micro soft.comwrote in message
news:36******** *************** ***********@mic rosoft.com...
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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed
struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error was
at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error
may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code.
Common sources of this bug include user marshaling errors for COM-interop
or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?
Nov 11 '07 #4
"Alexanderf e" <Al*********@di scussions.micro soft.comwrote in message
news:36******** *************** ***********@mic rosoft.com...
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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed
struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error was
at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error
may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code.
Common sources of this bug include user marshaling errors for COM-interop
or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?


The caller allocates the array of structs, however, you pass it as "out
MyStruct[]", this ain't gonna work, by this you are expecting the callee to
create the struct array and pass a pointer back to the caller (what you
effectively pass is a null pointer). You need to pass your struct array by
ref.

f(ref MyStruct[] arr, out int num);

....
int num = 0;
MyStruct[] ms = new MyStruct[10]; // allocate array of structs
f(ref ms, out int num); // pass the array address, callee fills the array
...

Willy.

Nov 11 '07 #5
Alexanderfe,

Given that, your structure declarations should look like this:

[StructLayout(La youtKind.Sequen tial)]
public struct TimeTag
{
public int Day;
public int Hour;
}

[StructLayout(La youtKind.Sequen tial)]
public struct Mystruct
{
public int Id;
public int Type;
public TimeTag StartTime;
public TimeTag EndTime;
public fixed int Reserved[2];
}

Notice the fixed keyword on the array field in the Mystruct declaration.
This causes the array to be embedded in the struct, and not a pointer to the
array. This is why it works when you declare two separate integers in the
struct, because the layout in memory is the same (one integer after
another).

Also note Willy's comments as well.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

LONGINT Id;
LONGINT Type;
TimeTag StartTime;
TimeTag EndTime;
LONGINT Reserved[2];
};
"Alexanderf e" <Al*********@di scussions.micro soft.comwrote in message
news:B1******** *************** ***********@mic rosoft.com...
Thanks for the reply,
It's embedded

typedef struct Mystruct{
LONGINT Id;
LONGINT Type;
TimeTag StartTime;
TimeTag EndTime;
LONGINT Reserved[2];
};

"Nicholas Paldino [.NET/C# MVP]" wrote:
>Alexanderfe,

Can you give the original function and structure declaration in C?
The
reason I ask is because I want to know if the Reserved field on the
MyStruct
structure is embedded in the structure, or a pointer to another array.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alexanderfe " <Al*********@di scussions.micro soft.comwrote in message
news:36******* *************** ************@mi crosoft.com...
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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed
struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error
was
at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error
may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code.
Common sources of this bug include user marshaling errors for
COM-interop
or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?
Nov 11 '07 #6
Marshaling arrays of structs is problematic (unless they are holding
blit-able types only) in .NET, so you'll have to take care about the
marshaling yourself.
Basically what you need to do is pass the struct array as an array of
IntPtr's each pointing to an unmanaged struct, on return from unmanaged code
you'll have to marshal the IntPtr's back to their managed struct
representation.

Following illustrates the process.

// C function, takes a pointer to a pointer of type MyStruct and a ref int
holding the size of the input array
extern "C" void __declspec(dlle xport) __stdcall F(Mystruct **p, int &size)
{
int n;
Mystruct * ps;
for(int n = 0; n < size; n++)
{
ps = *p++;
//set the struct fields using pointer arithmetics
ps->Id = 1;
..
}
size = n;
}
// C# snip

...
[StructLayout(La youtKind.Sequen tial)]
public struct TimeTag
{
public int Day;
public int Hour;
}

[StructLayout(La youtKind.Sequen tial)]
public struct Mystruct
{
public int Id;
public int Type;
public TimeTag StartTime;
public TimeTag EndTime;
public fixed int Reserved[2];
}
....
[DllImport("dlln ame"), SuppressUnmanag edCodeSecurity] static extern void
F( IntPtr[] p, ref int size);
....
int size = 10;
IntPtr[] ps = new IntPtr[size ];
for(int n = 0; n <size ; n++)
ps[n] =
Marshal.AllocHG lobal(Marshal.S izeOf(typeof(My struct )));
GetEmbeddedArra y2(ps2, ref size );
Mystruct [] s = new Mystruct [size];
for(int i = 0; i < size ; i++)
s[i] = (Mystruct )Marshal.PtrToS tructure(ps[i],
typeof(Mystruct ));

// use array of structs here...
....

Willy.
"Alexanderf e" <Al*********@di scussions.micro soft.comwrote in message
news:15******** *************** ***********@mic rosoft.com...
I've forgot to mention that when I use structs that consist only of Int32
fields in the same manor, all works fine and no exception is thrown.

"Nicholas Paldino [.NET/C# MVP]" wrote:
>Alexanderfe,

Can you give the original function and structure declaration in C?
The
reason I ask is because I want to know if the Reserved field on the
MyStruct
structure is embedded in the structure, or a pointer to another array.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alexanderfe " <Al*********@di scussions.micro soft.comwrote in message
news:36******* *************** ************@mi crosoft.com...
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 Mystruct and :
public struct Mystruct TimeTag
{
Int32 Id;
Int32 Type;
TimeTag StartTime;
TimeTag EndTime;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 2)]
Int32[] Reserved;
}

public struct TimeTag
{
int Day;
Int Hour;
}
I allocate the array , init all fields , allocate and init all needed
struct
fields , pass it to the dll and get an exception:

"The runtime has encountered a fatal error. The address of the error
was
at
0x79e95b95, on thread 0x28348. The error code is 0xc0000005. This error
may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code.
Common sources of this bug include user marshaling errors for
COM-interop
or
PInvoke, which may corrupt the stack".

I can't change nor the dll neither it's API.

What is the problem and what should I do to avoid it?

Nov 11 '07 #7

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

Similar topics

5
7604
by: Raju V.K | last post by:
I am developing a web site which requires a set og unique values be transferred between pages and server many times. There fore i decided to put the values in an array so that only one reference to the array is needed ratherthan idividual reference. The situation is like this: When the page is loaded based on the user input say 6 values are...
2
2513
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...
0
264
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
1
2141
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
17
3665
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...
3
3293
by: shobu | last post by:
passing array checkbox value and update the database <?include 'dbconnect.php'; error_reporting(0);$update_qr="update vv_menu set publish='inactive' "; $re_qr=mysql_query($update_qr) or die (mysql_error()); foreach($_POST as $check=>$value) {...
1
9636
by: fahd | last post by:
Hi, I'm trying to communicate with an unmanaged c++ dll that takes two 2d float arrays, one as input with data in it and the other will have the result of the operation in it as follows: bool Evaluate(float** input,float** output,int nFrames, int featureWidth); note: nFrames and featureWidth will determine the sizes of input and
2
4352
by: jonpb | last post by:
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...
0
1649
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
7579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7877
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
8101
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
7631
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
6238
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
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
3631
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
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.