以文本方式查看主题 - 金字塔客服中心 - 专业程序化交易软件提供商 (http://222.73.7.161/bbs/index.asp) -- 高级功能研发区 (http://222.73.7.161/bbs/list.asp?boardid=5) ---- 请问怎么在金字塔中运行这个debug 的程序啊 (http://222.73.7.161/bbs/dispbbs.asp?boardid=5&id=2627) |
-- 作者:xzhero -- 发布时间:2010/8/26 11:47:14 -- 请问怎么在金字塔中运行这个debug 的程序啊 http://www.codeproject.com/KB/vb/VisualTraceListener.aspx
IntroductionI was tooling around in the documentation pertaining to the This got me thinking - an application that can receive these To receive this output you need to attach to the running process as a debugger and then whenever the process needs a trace message your debugger will receive an Writing a rudimentary debuggerThere are a couple of API calls used in Windows NT for creating a debugger application:
<DllImport("kernel32", CallingConvention:=CallingConvention.Winapi, _
EntryPoint:="DebugActiveProcess", _
ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function DebugActiveProcess( <InAttribute()> _
ByVal ProcessHandle As Int32) As Boolean
End Function
<DllImport("kernel32", CallingConvention:=CallingConvention.Winapi, _
EntryPoint:="WaitForDebugEvent", _
ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function WaitForDebugEvent( _
<OutAttribute(), MarshalAs(UnmanagedType.LPStruct)> _
ByVal DebugEvent As DebugApi.Structures.DEBUG_EVENT, _
<InAttribute()> ByVal dwMilliseconds As Int32) As Boolean
End Function
and <DllImport("kernel32", CallingConvention:=CallingConvention.Winapi, _
EntryPoint:="ContinueDebugEvent", _
ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function ContinueDebugEvent( _
<InAttribute()> ByVal dwProcessId As Int32, _
<InAttribute()> ByVal dwThreadId As Int32, _
<InAttribute(), MarshalAs(UnmanagedType.U4)> _
ByVal dwContinueStatus As DebugAPIConstatnts.DebugStates) As Boolean
End Function
To debug an application you need to attach to the process and then run a loop that waits for a debug event, handles the debug event and then resumes the debuggee. Attaching to the processThe .NET Waiting for a debug eventCalling the <StructLayout(LayoutKind.Sequential)> _
Public Class DEBUG_EVENT
<MarshalAs(UnmanagedType.U4)> Public DebugEventCode _
As DebugApi.DebugAPIConstatnts.DebugEventTypes
Public ProcessId As Int32
Public ThreadId As Int32
Public lpDebugStringData As UInt32
Public IsUnicode As Int16
Public DebugStringLength As Int16
End Class
Handling the OUTPUT_DEBUG_STRING eventWhen we receive an Getting a string from another application\'s memoryTo get at the data in another application\'s memory we need to use the <DllImport("kernel32", CallingConvention:=CallingConvention.Winapi, _
EntryPoint:="ReadProcessMemory", _
ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function ReadProcessMemory( _
<InAttribute()> ByVal hProcess As Int32, _
<InAttribute()> ByVal lpBaseAddress As UInt32, _
<OutAttribute()> ByVal lpBuffer As IntPtr, _
<InAttribute()> ByVal nSize As Int32, _
<OutAttribute()> ByRef lpNumberOfBytesRead As UInt32 _
) As Boolean
End Function
For a bit of OO design I have implemented this as a function in the Public Function GetString(ByVal ProcessHandle As Int32) As String
Dim sRet As String
Dim hProcess As Int32
If DebugStringLength.Equals(0) OrElse lpDebugStringData.Equals(0) Then
sRet = ""
Else
hProcess = _
DebugApiDeclarations.OpenProcess(_
DebugAPIConstatnts.ProcessAccessPriviledges.PROCESS_VM_READ,_
False, Me.ProcessId)
If hProcess <> 0 Then
Dim bytesReturned As UInt32
Dim lpBuffer As IntPtr
\'Allocate enough space to put the string into
If Not IsUnicode Then
lpBuffer = Marshal.AllocHGlobal(DebugStringLength)
Else
lpBuffer = Marshal.AllocHGlobal(DebugStringLength * 2)
End If
If DebugApiDeclarations.ReadProcessMemory(hProcess, _
lpDebugStringData, lpBuffer, _
DebugStringLength, bytesReturned) Then
\'Make this buffer into a string...
If IsUnicode Then
sRet = Marshal.PtrToStringUni(lpBuffer, DebugStringLength)
Else
sRet = Marshal.PtrToStringAnsi(lpBuffer, DebugStringLength)
End If
Marshal.FreeHGlobal(lpBuffer)
Call DebugApiDeclarations.CloseHandle(hProcess)
Else
If Not lpBuffer.Equals(IntPtr.Zero) Then
Marshal.FreeHGlobal(lpBuffer)
End If
Throw New Win32Exception
End If
Else
Throw New Win32Exception
End If
End If
Return sRet
End Function
And with that you have a basic debugger that can watch an application and take note of its trace messages... <!-- Main Page Contents End --> LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) About the Author |
-- 作者:xzhero -- 发布时间:2010/8/26 11:49:03 -- ??????? |
-- 作者:wattwei -- 发布时间:2010/8/26 12:26:09 -- 不懂,帮顶 |
-- 作者:ch3coohqb -- 发布时间:2010/8/30 23:42:28 -- 看了第一行 <DllImport("kernel32", CallingConvention:=CallingConvention.Winapi, _ |