473,498 Members | 1,907 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(UnmanagedType.ByValArray, 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 8946
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.com

"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:36**********************************@microsof t.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(UnmanagedType.ByValArray, 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.com

"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:36**********************************@microsof t.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(UnmanagedType.ByValArray, 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.com

"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:36**********************************@microsof t.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(UnmanagedType.ByValArray, 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
"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:36**********************************@microsof t.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(UnmanagedType.ByValArray, 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(LayoutKind.Sequential)]
public struct TimeTag
{
public int Day;
public int Hour;
}

[StructLayout(LayoutKind.Sequential)]
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.com

LONGINT Id;
LONGINT Type;
TimeTag StartTime;
TimeTag EndTime;
LONGINT Reserved[2];
};
"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.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.com

"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:36**********************************@microso ft.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(UnmanagedType.ByValArray, 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(dllexport) __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(LayoutKind.Sequential)]
public struct TimeTag
{
public int Day;
public int Hour;
}

[StructLayout(LayoutKind.Sequential)]
public struct Mystruct
{
public int Id;
public int Type;
public TimeTag StartTime;
public TimeTag EndTime;
public fixed int Reserved[2];
}
....
[DllImport("dllname"), SuppressUnmanagedCodeSecurity] 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.AllocHGlobal(Marshal.SizeOf(typeof(Mystruc t )));
GetEmbeddedArray2(ps2, ref size );
Mystruct [] s = new Mystruct [size];
for(int i = 0; i < size ; i++)
s[i] = (Mystruct )Marshal.PtrToStructure(ps[i],
typeof(Mystruct ));

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

Willy.
"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.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.com

"Alexanderfe" <Al*********@discussions.microsoft.comwrote in message
news:36**********************************@microso ft.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(UnmanagedType.ByValArray, 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
7596
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...
2
2505
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
1509
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...
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
2128
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
3656
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...
3
3286
by: shobu | last post by:
passing array checkbox value and update the database <?include 'dbconnect.php'; error_reporting(0);$update_qr="update...
1
9613
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...
2
4345
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...
0
1643
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
7004
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
7167
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,...
0
7208
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...
1
6890
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...
0
5464
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,...
0
4593
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...
0
3095
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
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 ...

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.