473,416 Members | 1,762 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,416 software developers and data experts.

How to Marshal a Variable Length Structure

Can anyone suggest how I would marshal a variable length structure back from
an API call. Specifically, I am looking at the WaitForDebugEvent function,
which returns a DEBUG_EVENT structure.

However, the DEBUG_EVENT structure is defined as a union, and the size and
contents vary depending on the event code contained in the header.

typedef struct _DEBUG_EVENT {
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union {
EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString;
RIP_INFO RipInfo;
} u;
} DEBUG_EVENT, *LPDEBUG_EVENT;

I can translate this to VB.NET, but the marshalling eludes me.

TIA

Charles
Nov 20 '05 #1
8 3303
On Wed, 28 Jul 2004 16:19:17 +0100, Charles Law wrote:
Can anyone suggest how I would marshal a variable length structure back from
an API call. Specifically, I am looking at the WaitForDebugEvent function,
which returns a DEBUG_EVENT structure.

However, the DEBUG_EVENT structure is defined as a union, and the size and
contents vary depending on the event code contained in the header.

typedef struct _DEBUG_EVENT {
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union {
EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString;
RIP_INFO RipInfo;
} u;
} DEBUG_EVENT, *LPDEBUG_EVENT;

I can translate this to VB.NET, but the marshalling eludes me.

TIA

Charles


Charles, you'll want to do something like this:

<structlayout(layoutkind.sequential)> _
public structure exception_debug_info
....
end structure

<structlayout(layoutkind.sequential)> _
public structure create_thread_debug_info
...
end structure

.....

<structlayout(layoutkind.explicit)> _
public structure union
<fieldoffset(0)> _
public Exception as exception_debug_info
<fieldoffset(0)> _
public CreateThread as create_thread_debug_info
...
end structure

<sturctlayout(layoutkind.sequential)>
public structure debug_event
public dwDebugEventCode As Integer
public dwProcessId As IntPtr
public dwThreadId As IntPtr
public u As union
end structure

then you can use something like this to access the values:

if debugEvent.dwDebugEventCode = EXCEPTION_DEBUG_EVENT Then
debugEvent.u.Exception.blah_blah_blah
end if

Make sense?
--
Tom Shelton [MVP]
Nov 20 '05 #2
Hi Tom

Perfect sense. Thanks.

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:xq******************************@40tude.net.. .
On Wed, 28 Jul 2004 16:19:17 +0100, Charles Law wrote:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure.

However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header.

typedef struct _DEBUG_EVENT {
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union {
EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString;
RIP_INFO RipInfo;
} u;
} DEBUG_EVENT, *LPDEBUG_EVENT;

I can translate this to VB.NET, but the marshalling eludes me.

TIA

Charles


Charles, you'll want to do something like this:

<structlayout(layoutkind.sequential)> _
public structure exception_debug_info
....
end structure

<structlayout(layoutkind.sequential)> _
public structure create_thread_debug_info
...
end structure

....

<structlayout(layoutkind.explicit)> _
public structure union
<fieldoffset(0)> _
public Exception as exception_debug_info
<fieldoffset(0)> _
public CreateThread as create_thread_debug_info
...
end structure

<sturctlayout(layoutkind.sequential)>
public structure debug_event
public dwDebugEventCode As Integer
public dwProcessId As IntPtr
public dwThreadId As IntPtr
public u As union
end structure

then you can use something like this to access the values:

if debugEvent.dwDebugEventCode = EXCEPTION_DEBUG_EVENT Then
debugEvent.u.Exception.blah_blah_blah
end if

Make sense?
--
Tom Shelton [MVP]

Nov 20 '05 #3
Tom

It was all going so well, until

<Exception>
An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.

Additional information: Could not load type union from assembly MyAssembly,
Version=1.0.1670.28085, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 4 that is incorrectly aligned or
overlapped by a non-object field.
</Exception>

I have defined the structures as you suggest, but get the exception above.
It occurs because of the following statement

Dim de As DEBUG_EVENT

Here are the complete definitions

<Definitions>
<StructLayout(LayoutKind.Sequential)> Public Structure EXCEPTION_RECORD
Public ExceptionCode As ExceptionCodes
Public ExceptionFlags As ExceptionFlags
Public ExceptionRecord As IntPtr
Public ExceptionAddress As IntPtr
Public NumberParameters As Integer
Public ExceptionInformation() As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> Public Structure
EXCEPTION_DEBUG_INFO
Public ExceptionRecord As EXCEPTION_RECORD
Public dwFirstChance As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
CREATE_THREAD_DEBUG_INFO
Public hThread As IntPtr
Public lpThreadLocalBase As IntPtr
Public lpStartAddress As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
CREATE_PROCESS_DEBUG_INFO
Public hfile As IntPtr
Public hProcess As IntPtr
Public hThread As IntPtr
Public lpBaseOfImage As IntPtr
Public dwDebugInfoFileOffset As Integer
Public nDebugInfoSize As Integer
Public lpThreadLocalBase As IntPtr
Public lpStartAddress As IntPtr
Public lpImageName As IntPtr
Public fUnicode As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
EXIT_THREAD_DEBUG_INFO
Public dwExitCode As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
EXIT_PROCESS_DEBUG_INFO
Public dwExitCode As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
LOAD_DLL_DEBUG_INFO
Public hFile As IntPtr
Public lpBaseOfDll As IntPtr
Public dwDebugInfoFileOffset As Integer
Public nDebugInfoSize As Integer
Public lpImageName As IntPtr
Public fUnicode As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
UNLOAD_DLL_DEBUG_INFO
Public lpBaseOfDll As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
OUTPUT_DEBUG_STRING_INFO
Public lpDebugStringData As IntPtr
Public fUnicode As Int16
Public nDebugStringLength As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure RIP_INFO
Dim dwError As Integer
Dim dwType As Integer
End Structure

<StructLayout(LayoutKind.Explicit)> Public Structure union
<FieldOffset(0)> Public Exception As EXCEPTION_DEBUG_INFO
<FieldOffset(0)> Public CreateThread As CREATE_THREAD_DEBUG_INFO
<FieldOffset(0)> Public CreateProcessInfo As
CREATE_PROCESS_DEBUG_INFO
<FieldOffset(0)> Public ExitThread As EXIT_THREAD_DEBUG_INFO
<FieldOffset(0)> Public ExitProcess As EXIT_PROCESS_DEBUG_INFO
<FieldOffset(0)> Public LoadDll As LOAD_DLL_DEBUG_INFO
<FieldOffset(0)> Public UnloadDll As UNLOAD_DLL_DEBUG_INFO
<FieldOffset(0)> Public DebugString As OUTPUT_DEBUG_STRING_INFO
<FieldOffset(0)> Public RipInfo As RIP_INFO
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure DEBUG_EVENT
Public dwDebugEventCode As Integer
Public dwProcessId As IntPtr
Public dwThreadId As IntPtr
Public u As union
End Structure
</Definitions>

Can you spot the mistake?

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:xq******************************@40tude.net.. .
On Wed, 28 Jul 2004 16:19:17 +0100, Charles Law wrote:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure.

However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header.

typedef struct _DEBUG_EVENT {
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union {
EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString;
RIP_INFO RipInfo;
} u;
} DEBUG_EVENT, *LPDEBUG_EVENT;

I can translate this to VB.NET, but the marshalling eludes me.

TIA

Charles


Charles, you'll want to do something like this:

<structlayout(layoutkind.sequential)> _
public structure exception_debug_info
....
end structure

<structlayout(layoutkind.sequential)> _
public structure create_thread_debug_info
...
end structure

....

<structlayout(layoutkind.explicit)> _
public structure union
<fieldoffset(0)> _
public Exception as exception_debug_info
<fieldoffset(0)> _
public CreateThread as create_thread_debug_info
...
end structure

<sturctlayout(layoutkind.sequential)>
public structure debug_event
public dwDebugEventCode As Integer
public dwProcessId As IntPtr
public dwThreadId As IntPtr
public u As union
end structure

then you can use something like this to access the values:

if debugEvent.dwDebugEventCode = EXCEPTION_DEBUG_EVENT Then
debugEvent.u.Exception.blah_blah_blah
end if

Make sense?
--
Tom Shelton [MVP]

Nov 20 '05 #4
The offending line seems to be

Public ExceptionInformation() As IntPtr

in the EXCEPTION_RECORD structure. Changing it to

Public ExceptionInformation As IntPtr

allows the code to run. However, I now get gibberish whenever a
EXCEPTION_DEBUG_EVENT occurs. It seems like the values in the
EXCEPTION_RECORD are misaligned. For example, the ExceptionCode is zero, the
ExceptionAddress is 3, and the NumberParameters is some large -ve number.

Can you see what else might be wrong with the definition?

Charles
"Charles Law" <bl***@nowhere.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Tom

It was all going so well, until

<Exception>
An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.

Additional information: Could not load type union from assembly MyAssembly, Version=1.0.1670.28085, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 4 that is incorrectly aligned or
overlapped by a non-object field.
</Exception>

I have defined the structures as you suggest, but get the exception above.
It occurs because of the following statement

Dim de As DEBUG_EVENT

Here are the complete definitions

<Definitions>
<StructLayout(LayoutKind.Sequential)> Public Structure EXCEPTION_RECORD Public ExceptionCode As ExceptionCodes
Public ExceptionFlags As ExceptionFlags
Public ExceptionRecord As IntPtr
Public ExceptionAddress As IntPtr
Public NumberParameters As Integer
Public ExceptionInformation() As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> Public Structure
EXCEPTION_DEBUG_INFO
Public ExceptionRecord As EXCEPTION_RECORD
Public dwFirstChance As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
CREATE_THREAD_DEBUG_INFO
Public hThread As IntPtr
Public lpThreadLocalBase As IntPtr
Public lpStartAddress As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
CREATE_PROCESS_DEBUG_INFO
Public hfile As IntPtr
Public hProcess As IntPtr
Public hThread As IntPtr
Public lpBaseOfImage As IntPtr
Public dwDebugInfoFileOffset As Integer
Public nDebugInfoSize As Integer
Public lpThreadLocalBase As IntPtr
Public lpStartAddress As IntPtr
Public lpImageName As IntPtr
Public fUnicode As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
EXIT_THREAD_DEBUG_INFO
Public dwExitCode As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
EXIT_PROCESS_DEBUG_INFO
Public dwExitCode As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
LOAD_DLL_DEBUG_INFO
Public hFile As IntPtr
Public lpBaseOfDll As IntPtr
Public dwDebugInfoFileOffset As Integer
Public nDebugInfoSize As Integer
Public lpImageName As IntPtr
Public fUnicode As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
UNLOAD_DLL_DEBUG_INFO
Public lpBaseOfDll As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
OUTPUT_DEBUG_STRING_INFO
Public lpDebugStringData As IntPtr
Public fUnicode As Int16
Public nDebugStringLength As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure RIP_INFO
Dim dwError As Integer
Dim dwType As Integer
End Structure

<StructLayout(LayoutKind.Explicit)> Public Structure union
<FieldOffset(0)> Public Exception As EXCEPTION_DEBUG_INFO
<FieldOffset(0)> Public CreateThread As CREATE_THREAD_DEBUG_INFO
<FieldOffset(0)> Public CreateProcessInfo As
CREATE_PROCESS_DEBUG_INFO
<FieldOffset(0)> Public ExitThread As EXIT_THREAD_DEBUG_INFO
<FieldOffset(0)> Public ExitProcess As EXIT_PROCESS_DEBUG_INFO
<FieldOffset(0)> Public LoadDll As LOAD_DLL_DEBUG_INFO
<FieldOffset(0)> Public UnloadDll As UNLOAD_DLL_DEBUG_INFO
<FieldOffset(0)> Public DebugString As OUTPUT_DEBUG_STRING_INFO
<FieldOffset(0)> Public RipInfo As RIP_INFO
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure DEBUG_EVENT
Public dwDebugEventCode As Integer
Public dwProcessId As IntPtr
Public dwThreadId As IntPtr
Public u As union
End Structure
</Definitions>

Can you spot the mistake?

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:xq******************************@40tude.net.. .
On Wed, 28 Jul 2004 16:19:17 +0100, Charles Law wrote:
Can anyone suggest how I would marshal a variable length structure
back
from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure.

However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header.

typedef struct _DEBUG_EVENT {
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union {
EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString;
RIP_INFO RipInfo;
} u;
} DEBUG_EVENT, *LPDEBUG_EVENT;

I can translate this to VB.NET, but the marshalling eludes me.

TIA

Charles


Charles, you'll want to do something like this:

<structlayout(layoutkind.sequential)> _
public structure exception_debug_info
....
end structure

<structlayout(layoutkind.sequential)> _
public structure create_thread_debug_info
...
end structure

....

<structlayout(layoutkind.explicit)> _
public structure union
<fieldoffset(0)> _
public Exception as exception_debug_info
<fieldoffset(0)> _
public CreateThread as create_thread_debug_info
...
end structure

<sturctlayout(layoutkind.sequential)>
public structure debug_event
public dwDebugEventCode As Integer
public dwProcessId As IntPtr
public dwThreadId As IntPtr
public u As union
end structure

then you can use something like this to access the values:

if debugEvent.dwDebugEventCode = EXCEPTION_DEBUG_EVENT Then
debugEvent.u.Exception.blah_blah_blah
end if

Make sense?
--
Tom Shelton [MVP]


Nov 20 '05 #5
In article <OS**************@TK2MSFTNGP10.phx.gbl>, Charles Law wrote:
The offending line seems to be

Public ExceptionInformation() As IntPtr

in the EXCEPTION_RECORD structure. Changing it to

Public ExceptionInformation As IntPtr

allows the code to run. However, I now get gibberish whenever a
EXCEPTION_DEBUG_EVENT occurs. It seems like the values in the
EXCEPTION_RECORD are misaligned. For example, the ExceptionCode is zero, the
ExceptionAddress is 3, and the NumberParameters is some large -ve number.

Can you see what else might be wrong with the definition?

Try changeing it to:

<MarshalAs (UnmanagedType.ByValArray, SizeConst=15)> _
Public ExceptionInformation() As IntPtr

I looked up the definition of this structure in msdn and it has it as :

ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS];

I looked up EXCEPTION_MAXIMUM_PARAMETERS and it is defined as:

#define EXCEPTION_MAXIMUM_PARAMETERS 15 // maximum number of exception
parameters

In WinNT.h - so, it is expecting a fixed size array there. The above
suggestion should work.

--
Tom Shelton [MVP]
Nov 20 '05 #6
Hi Tom

Sadly, I get the same error, that is the one about

<Exception>
An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.

Additional information: Could not load type union from assembly MyAssembly,
Version=1.0.1671.24596, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 4 that is incorrectly aligned or
overlapped by a non-object field.
</Exception>

It would seem to be a conflict between reference and value types in the same
structure. All the other entries are value types, but I think this is now a
reference type, and that is not allowed. Do you think it could be that?

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
In article <OS**************@TK2MSFTNGP10.phx.gbl>, Charles Law wrote:
The offending line seems to be

Public ExceptionInformation() As IntPtr

in the EXCEPTION_RECORD structure. Changing it to

Public ExceptionInformation As IntPtr

allows the code to run. However, I now get gibberish whenever a
EXCEPTION_DEBUG_EVENT occurs. It seems like the values in the
EXCEPTION_RECORD are misaligned. For example, the ExceptionCode is zero, the ExceptionAddress is 3, and the NumberParameters is some large -ve number.
Can you see what else might be wrong with the definition?

Try changeing it to:

<MarshalAs (UnmanagedType.ByValArray, SizeConst=15)> _
Public ExceptionInformation() As IntPtr

I looked up the definition of this structure in msdn and it has it as :

ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS];

I looked up EXCEPTION_MAXIMUM_PARAMETERS and it is defined as:

#define EXCEPTION_MAXIMUM_PARAMETERS 15 // maximum number of exception
parameters

In WinNT.h - so, it is expecting a fixed size array there. The above
suggestion should work.

--
Tom Shelton [MVP]

Nov 20 '05 #7
In article <eU**************@tk2msftngp13.phx.gbl>, Charles Law wrote:
Hi Tom

Sadly, I get the same error, that is the one about

<Exception>
An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.

Additional information: Could not load type union from assembly MyAssembly,
Version=1.0.1671.24596, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 4 that is incorrectly aligned or
overlapped by a non-object field.
</Exception>

It would seem to be a conflict between reference and value types in the same
structure. All the other entries are value types, but I think this is now a
reference type, and that is not allowed. Do you think it could be that?

Charles


It appears that the problem is the embeded array inside of the union...
This appears to work for me, but it is sort of ugly:

<StructLayout(LayoutKind.Sequential)> _
Structure EXCEPTION_RECORD
Public ExceptionCode As Integer
Public ExceptionFlags As Integer
Public ExceptionRecord As IntPtr
Public ExceptionAddress As IntPtr
Public NumberParameters As Integer
Public ExceptionInformation01 As IntPtr
Public ExceptionInformation02 As IntPtr
Public ExceptionInformation03 As IntPtr
Public ExceptionInformation04 As IntPtr
Public ExceptionInformation05 As IntPtr
Public ExceptionInformation06 As IntPtr
Public ExceptionInformation07 As IntPtr
Public ExceptionInformation08 As IntPtr
Public ExceptionInformation09 As IntPtr
Public ExceptionInformation10 As IntPtr
Public ExceptionInformation11 As IntPtr
Public ExceptionInformation12 As IntPtr
Public ExceptionInformation13 As IntPtr
Public ExceptionInformation14 As IntPtr
Public ExceptionInformation15 As IntPtr
End Structure

Basically, I unrolled the array...
--
Tom Shelton [MVP]
Nov 20 '05 #8
Hi Tom
Basically, I unrolled the array...
I see what you did ;-)

I suppose I should be grateful that EXCEPTON_MAXIMUM_PARAMETERS isn't
defined as 1,000!

I have got round it, for now, by making EXCEPTION_DEBUG_EVENT a special
case, and leaving it out of the union. If an exception event is notified, I
marshal the pointer to a structure that I have defined on its own, so that
it doesn't upset anything else.

<Definition>
<StructLayout(LayoutKind.Sequential)> Public Structure EXCEPTION_RECORD
Public ExceptionCode As ExceptionCodes
Public ExceptionFlags As ExceptionFlags
Public ExceptionRecord As IntPtr
Public ExceptionAddress As IntPtr
Public NumberParameters As Integer
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=15)> _
Public ExceptionInformation() As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> Public Structure
EXCEPTION_DEBUG_INFO
Public ExceptionRecord As EXCEPTION_RECORD
Public dwFirstChance As Boolean
End Structure

<StructLayout(LayoutKind.Sequential)> Public Structure
EXCEPTION_DEBUG_EVENT
Public dwDebugEventCode As Integer
Public dwProcessId As IntPtr
Public dwThreadId As IntPtr
Public Exception As EXCEPTION_DEBUG_INFO
End Structure
</Defintion>

It works, so I will probably leave it unless I encounter other problems with
it.

Speaking of other problems ...

I have defined the following exception code, that seems to consistently crop
up, but which is not defined anywhere

EXCEPTION_CLR = &HE0434F4D

I have no idea what this code means, but when I throw an exception in my
application and get notified of it in my debugger it is always the same.
Google has a couple of hits on this number, but no one seems to know what it
means, just that it happens.

Any clues welcome.

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl... In article <eU**************@tk2msftngp13.phx.gbl>, Charles Law wrote:
Hi Tom

Sadly, I get the same error, that is the one about

<Exception>
An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.

Additional information: Could not load type union from assembly MyAssembly, Version=1.0.1671.24596, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 4 that is incorrectly aligned or
overlapped by a non-object field.
</Exception>

It would seem to be a conflict between reference and value types in the same structure. All the other entries are value types, but I think this is now a reference type, and that is not allowed. Do you think it could be that?

Charles


It appears that the problem is the embeded array inside of the union...
This appears to work for me, but it is sort of ugly:

<StructLayout(LayoutKind.Sequential)> _
Structure EXCEPTION_RECORD
Public ExceptionCode As Integer
Public ExceptionFlags As Integer
Public ExceptionRecord As IntPtr
Public ExceptionAddress As IntPtr
Public NumberParameters As Integer
Public ExceptionInformation01 As IntPtr
Public ExceptionInformation02 As IntPtr
Public ExceptionInformation03 As IntPtr
Public ExceptionInformation04 As IntPtr
Public ExceptionInformation05 As IntPtr
Public ExceptionInformation06 As IntPtr
Public ExceptionInformation07 As IntPtr
Public ExceptionInformation08 As IntPtr
Public ExceptionInformation09 As IntPtr
Public ExceptionInformation10 As IntPtr
Public ExceptionInformation11 As IntPtr
Public ExceptionInformation12 As IntPtr
Public ExceptionInformation13 As IntPtr
Public ExceptionInformation14 As IntPtr
Public ExceptionInformation15 As IntPtr
End Structure

Basically, I unrolled the array...
--
Tom Shelton [MVP]

Nov 20 '05 #9

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

Similar topics

0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
7
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
2
by: Ananas | last post by:
Hi, Please give me an idea how to send a static array from dll written on C++ to C# application. This is a C++ code: const SIGNATURE_LENGTH = 50; struct Info {
1
by: nicewenyan | last post by:
I want to pass a managed c# byte (8 bit) array into a unmanaged c++ function: extern "C" void AddData(unsigned int* data); I use P/Invoke on managed side to do the marshaling as following: ...
5
by: active | last post by:
The structue below contains: <MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> which might work for some examples but in general the size needs to be set at run time (to ...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: RobbieJosefson | last post by:
I want to marshal a structure containing a variable length array which will be populated within a C++ dll. the structure is the following Public Structure myStructure{ Public int size;...
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
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
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,...
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,...
0
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...
0
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
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...
0
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...

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.