none
一个编写调试器的问题 RRS feed

  • 问题

  • 我在编写调试器时,有一个很大的麻烦。代码如下:

        Public Const EXCEPTION_DEBUG_EVENT As UInt32 = 1
        Public Const CREATE_THREAD_DEBUG_EVENT As UInt32 = 2
        Public Const CREATE_PROCESS_DEBUG_EVENT As UInt32 = 3
        Public Const EXIT_THREAD_DEBUG_EVENT As UInt32 = 4
        Public Const EXIT_PROCESS_DEBUG_EVENT As UInt32 = 5
        Public Const LOAD_DLL_DEBUG_EVENT As UInt32 = 6
        Public Const UNLOAD_DLL_DEBUG_EVENT As UInt32 = 7
        Public Const OUTPUT_DEBUG_STRING_EVENT As UInt32 = 8
        Public Const RIP_EVENT As UInt32 = 9
        <DllImport("kernel32.dll")> _
        Public Function ContinueDebugEvent(ByVal dwProcessId As UInt32, ByVal dwThreadId As UInt32, ByVal dwContinueStatus As UInt32) As Boolean
        End Function
        Public Const DBG_CONTINUE As UInt32 = &H10002
        <DllImport("kernel32.dll")> _
        Public Function CreateProcess(ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Object, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
        End Function
        <DllImport("kernel32.dll")> _
        Public Function GetLastError() As Long
        End Function
        <DllImport("kernel32.dll")> _
        Public Sub SetLastError(ByVal dwErrCode As Integer)
        End Sub
        <DllImport("kernel32.dll")> _
        Public Function DebugActiveProcess(ByVal dwProcessId As Long) As Long
        End Function
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure PROCESS_INFORMATION
            Public hProcess As Long
            Public hThread As Long
            Public dwProcessId As Long
            Public dwThreadId As Long
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure STARTUPINFO
            Public cb As Long
            Public lpReserved As String
            Public lpDesktop As String
            Public lpTitle As String
            Public dwX As Long
            Public dwY As Long
            Public dwXSize As Long
            Public dwYSize As Long
            Public dwXCountChars As Long
            Public dwYCountChars As Long
            Public dwFillAttribute As Long
            Public dwFlags As Long
            Public wShowWindow As Integer
            Public cbReserved2 As Integer
            Public lpReserved2 As Long
            Public hStdInput As Long
            Public hStdOutput As Long
            Public hStdError As Long
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure SECURITY_ATTRIBUTES
            Public nLength As Long
            Public lpSecurityDescriptor As Long
            Public bInheritHandle As Long
        End Structure
        <DllImport("kernel32.dll")> _
        Public Function WaitForDebugEvent(<[In]()> ByRef lpDebugEvent As DEBUG_EVENT, ByVal dwMilliseconds As UInt32) As Boolean
        End Function
        Public INFINITE As UInt32 = UInt32.MaxValue
        <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 UInt32
            Public nDebugInfoSize As UInt32
            Public lpThreadLocalBase As IntPtr
            Public lpStartAddress As PTHREAD_START_ROUTINE
            Public lpImageName As IntPtr
            Public fUnicode As UInt16
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure CREATE_THREAD_DEBUG_INFO
            Public hThread As IntPtr
            Public lpThreadLocalBase As IntPtr
            Public lpStartAddress As PTHREAD_START_ROUTINE
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure DEBUG_EVENT
            Public dwDebugEventCode As UInt32
            Public dwProcessId As UInt32
            Public dwThreadId As UInt32
            Public u As Union
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure EXCEPTION_DEBUG_INFO
            Public ExceptionRecord As EXCEPTION_RECORD
            Public dwFirstChance As UInt32
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure EXCEPTION_RECORD
            Public ExceptionCode As UInt32
            Public ExceptionFlags As UInt32
            Public ExceptionRecord As IntPtr
            Public ExceptionAddress As IntPtr
            Public NumberParameters As UInt32
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=15, ArraySubType:=UnmanagedType.U4)> _
            Public ExceptionInformation As UInt32()
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure EXIT_PROCESS_DEBUG_INFO
            Public dwExitCode As UInt32
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure EXIT_THREAD_DEBUG_INFO
            Public dwExitCode As UInt32
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure LOAD_DLL_DEBUG_INFO
            Public hFile As IntPtr
            Public lpBaseOfDll As IntPtr
            Public dwDebugInfoFileOffset As UInt32
            Public nDebugInfoSize As UInt32
            Public lpImageName As IntPtr
            Public fUnicode As UInt16
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure OUTPUT_DEBUG_STRING_INFO
            <MarshalAs(UnmanagedType.LPStr)> _
            Public lpDebugStringData As String
            Public fUnicode As UInt16
            Public nDebugStringLength As UInt16
        End Structure
        Public Delegate Function PTHREAD_START_ROUTINE(ByVal lpThreadParameter As IntPtr) As UInt32
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RIP_INFO
            Public dwError As UInt32
            Public dwType As UInt32
        End Structure
        <StructLayout(LayoutKind.Explicit, pack:=1)> _
        Public Structure Union
            <FieldOffset(0)> _
            Public CreateThread As CREATE_THREAD_DEBUG_INFO
            <FieldOffset(0)> _
            Public Exception As EXCEPTION_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 UNLOAD_DLL_DEBUG_INFO
            Public lpBaseOfDll As IntPtr
        End Structure

    一定义DEBUG_EVENT就会冒出来TypeLoadException

    “未能从程序集“多重编辑器, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“Union”,因为它在 0 偏移位置处包含一个对象字段,该字段已由一个非对象字段不正确地对齐或重叠。”

    如何解决?

    2014年11月9日 13:01

答案

全部回复