> Hi
>
[quoted text clipped - 6 lines]
> EXCEPTION_RECORD upsets this scheme, as it contains a fixed array that needs
> to be marshalled as a reference type; unless you have any suggestions?
So _EXCEPTION_DEBUG_INFO contains an embedded EXCEPTION_RECORD, not a
pointer, so that's not a problem :
<StructLayout(LayoutKind.Sequential)> _
Public Structure _ExceptionDebugInfo
Public ExceptionRecord As _ExceptionRecord
Public FirstChance As uint
End Structure
Now, EXCEPTION_RECORD has a pointer to other records, so declare it as an
IntPtr :
<StructLayout(LayoutKind.Sequential)> _
Public Structure _ExceptionRecord
Public ExceptionCode As UInt32
Public ExceptionFlags As UInt32
Public pExceptionRecord As IntPtr ' pointer to chained records
Public pExceptionAddress As IntPtr
Public NumberParameters As UInt32
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=15)> _
Public ExceptionInformation() As IntPtr
End Structure
....
Dim de As New DebugEvent
WaitForDebugEvent( de, ... )
...
' Now if de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT,
' then you can read the first ExceptionRecord => de.Exception,
' but to get the chained Exceptions you need to do :
Dim p As IntPtr p = de.Exception.pExceptionRecord
Do While ( Not p.Equals( IntPtr.Zero) )
Dim ex As _ExceptionRecord = _
CType(Marshal.PtrToStruct( p, GetType(_ExceptionRecord)),
_ExceptionRecord)
' use ex here (or store it for later use)
' get next pointer
p = ex.pExceptionRecord
Loop
' You can also use Marshal.PtrToStruct on ExceptionInformation(x) if it's a
pointer to struct or use ExceptionInformation(x).ToInt32() if the pointer is
used as a value.
HTH,
greetings
> Charles
>
[quoted text clipped - 57 lines]
> > >
> > > Charles
Charles Law - 30 Jul 2004 00:09 GMT
It's the EXCEPTION_RECORD that is the problem. This is what it doesn't like
> <MarshalAs(UnmanagedType.ByValArray, SizeConst:=15)> _
> Public ExceptionInformation() As IntPtr
The application compiles, but when I call a method containing
> Dim de As New DebugEvent
I get the follwoing exception
<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.41486, 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 never even get into the method.
Do you have this scenario running?
Charles
> > Hi
> >
[quoted text clipped - 124 lines]
> > > >
> > > > Charles
BMermuys - 30 Jul 2004 03:37 GMT
Hi,
> It's the EXCEPTION_RECORD that is the problem. This is what it doesn't like
>
[quoted text clipped - 6 lines]
>
> I get the follwoing exception
Yes there seems to be some problems. A possible workaround is to avoid the
use of a union:
<StructLayout(LayoutKind.Sequential)> _
Public Structure DebugEventHdr
Public DebugEventCode As UInt32
Public ProcessId As UInt32
Public ThreadId As UInt32
End Structure
<DllImport("kernel32.dll")>
Public Shared Function WaitForDebugEvent( pDebugEvent As IntPtr,
MilliSeconds As UInt32 )
End Function
Usage :
Dim pDebugEvent As IntPtr = Marshal.AllocHGlobal(96)
WaitForDebugEvent ( pDebugEvent, Convert.ToUInt32(0) )
Dim hdr As DebugEventHdr =
CType(Marshal.PtrToStruct(pDebugEvent,GetType(DebugEventHdr)),DebugEventHdr)
IntPtr pEventData = new IntPtr( pDebugEvent.ToInt32() + 12 )
Select Case (Convert.ToInt32(hdr.DebugEventCode))
Case Is = 1 'EXCEPTION_DEBUG_EVENT
Dim edi As _ExceptionDebugInfo
edi = CType(Marshal.PtrToStructure(pEventData,
GetType(_ExceptionDebugInfo)), _ExceptionDebugInfo)
Case Is = 2 ' CREATE_THREAD_DEBUG_EVENT
Dim ctde As _CreateThreadDebugInfo
ctde = CType(Marshal.PtrToStructure(pEventData,
GetType(_CreateThreadDebugInfo)), _CreateThreadDebugInfo)
...
End Select
Marshal.FreeHGlobal( pDebugEvent )
Another workaround is to flatten the array inside EXCEPTION_RECORD.
HTH,
greetings
> <Exception>
> An unhandled exception of type 'System.TypeLoadException' occurred in
[quoted text clipped - 145 lines]
> > > > >
> > > > > Charles
Charles Law - 30 Jul 2004 09:21 GMT
I could flatten the array, in this case, as it only has 15 elements, but in
the general case ...
I have ended up making EXCEPTION_DEBUG_EVENT a special case, as you suggest,
and have removed it from the union. It all seems to work now.
Thanks again.
Charles
> Hi,
>
[quoted text clipped - 31 lines]
> WaitForDebugEvent ( pDebugEvent, Convert.ToUInt32(0) )
> Dim hdr As DebugEventHdr =
CType(Marshal.PtrToStruct(pDebugEvent,GetType(DebugEventHdr)),DebugEventHdr)
> IntPtr pEventData = new IntPtr( pDebugEvent.ToInt32() + 12 )
>
[quoted text clipped - 173 lines]
> > > > > >
> > > > > > Charles