By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

How to Marshal a Variable Length Structure

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.