private string GetOutputDebugString(OUTPUT_DEBUG_STRING_INFO debugString) { if (_processInformation == null) { return("<no process handle>"); } // Note: Sometimes when debugging unit tests, we get AccessViolationException reading bytes from the debuggee. #if false return("<no processed>"); #else var isUnicode = (debugString.fUnicode != 0); var byteCount = (uint)(debugString.nDebugStringLength * (isUnicode ? sizeof(char) : sizeof(byte))); var bytes = new byte[byteCount]; uint bytesRead; if (!Win32.Processes.NativeMethods.ReadProcessMemory(_processInformation.ProcessHandle, debugString.lpDebugStringData, bytes, byteCount, out bytesRead)) { return(string.Format("<error getting debug string from process: {0}>", new LastWin32ErrorException().Message)); } var message = (isUnicode ? Conversion.Utf16ToString(bytes) : Conversion.Utf8ToString(bytes)); message = message.TrimEnd('\r', '\n'); return(message); #endif }
public void PrintDebugString(OUTPUT_DEBUG_STRING_INFO outputDbgStrInfo) { using (SafeProcessHandle hProcess = Process.OpenProcess(ProcessAccessFlags.VmRead, false, pid)) { if (hProcess.IsInvalid) { throw new ArgumentException(String.Format("Unable to open process {0}, error {x:8}", pid, Marshal.GetLastWin32Error())); } var dbgString = new byte[outputDbgStrInfo.nDebugStringLength]; uint numberOfBytesRead; var result = Process.ReadProcessMemory(hProcess, outputDbgStrInfo.lpDebugStringData, dbgString, outputDbgStrInfo.nDebugStringLength, out numberOfBytesRead); if (result) { if (outputDbgStrInfo.fUnicode == 0) { Console.WriteLine("Debug String: {0}", Encoding.ASCII.GetString(dbgString)); } else { Console.WriteLine("Debug String: {0}", Encoding.Unicode.GetString(dbgString)); } } } }
// returns a message from the event static string GetMessageFromDebugEvent(CorProcess corProcess, OUTPUT_DEBUG_STRING_INFO eventOds) { bool isUnicode = eventOds.fUnicode != 0; byte[] buffer = new byte[isUnicode?eventOds.nDebugStringLenght * 2:eventOds.nDebugStringLenght]; int bytesRead = corProcess.ReadMemory(eventOds.lpDebugStringData.ToInt64(), buffer); Debug.Assert(buffer.Length == bytesRead); System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < buffer.Length; i++) { int val; if (isUnicode) { val = (int)buffer[i] + ((int)buffer[i + 1] << 8); i++; } else { val = buffer[i]; } sb.Append((char)val); } return(sb.ToString()); }
public void PrintDebugString(OUTPUT_DEBUG_STRING_INFO outputDbgStrInfo) { var dbgString = new byte[outputDbgStrInfo.nDebugStringLength]; uint numberOfBytesRead; var result = ProcessNativeMethod.ReadProcessMemory(hProcess, outputDbgStrInfo.lpDebugStringData, dbgString, outputDbgStrInfo.nDebugStringLength, out numberOfBytesRead); if (result) { if (outputDbgStrInfo.fUnicode == 0) { Console.WriteLine("Debug String: {0}", Encoding.ASCII.GetString(dbgString)); } else { Console.WriteLine("Debug String: {0}", Encoding.Unicode.GetString(dbgString)); } } }