Home > Back-end >  Show external process debug output
Show external process debug output

Time:08-29

I'm working on an application that is a sound editor for a video game.

This application has an option to test the sounds in the game, to do this I use Process.Start with some special parameters to open the game .exe.

The game uses OutputDebugStringA() to print debug messages. The idea is to have a multiline textbox inside this form to show the debug messages only of the game process.

I've been doing some research and I know about the existence of DebugView from sysinternals, but is not useful for this case, I've also found DbMon.NET in code project, but for some reason causes the game to run very slow (with DebugView doesn't happen).

Any proposal?

CodePudding user response:

Try this, you can specify the process ID inside the do while loop before printing it in the textbox.

public partial class Form1 : Form
{
    private readonly IntPtr _bufferReadyEvent;
    private readonly IntPtr _dataReadyEvent;
    private readonly IntPtr _mapping;
    private readonly IntPtr _file;
    private const int WaitTimeout = 500;
    private const uint WAIT_OBJECT_0 = 0;
    private readonly Thread _reader;

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetEvent(IntPtr hEvent);

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpFileMappingAttributes, FileMapProtection flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern uint WaitForSingleObject(IntPtr handle, uint milliseconds);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);

    [Flags]
    private enum FileMapAccess
    {
        FileMapRead = 0x0004,
    }

    [Flags]
    private enum FileMapProtection
    {
        PageReadWrite = 0x04,
    }

    public Form1()
    {
        InitializeComponent();

        _bufferReadyEvent = CreateEvent(IntPtr.Zero, false, false, "DBWIN_BUFFER_READY");
        if (_bufferReadyEvent == IntPtr.Zero)
            Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());

        _dataReadyEvent = CreateEvent(IntPtr.Zero, false, false, "DBWIN_DATA_READY");
        if (_dataReadyEvent == IntPtr.Zero)
            Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());

        _mapping = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, FileMapProtection.PageReadWrite, 0, 4096, "DBWIN_BUFFER");
        if (_mapping == IntPtr.Zero)
            Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());

        _file = MapViewOfFile(_mapping, FileMapAccess.FileMapRead, 0, 0, 1024);
        if (_file == IntPtr.Zero)
            Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());

        _reader = new Thread(Read)
        {
            IsBackground = true
        };
        _reader.Start();
    }

    private void Read()
    {
        try
        {
            do
            {
                SetEvent(_bufferReadyEvent);

                uint wait = WaitForSingleObject(_dataReadyEvent, WaitTimeout);
                if (wait == WAIT_OBJECT_0) // we don't care about other return values
                {
                    int pid = Marshal.ReadInt32(_file);
                    string text = Marshal.PtrToStringAnsi(new IntPtr(_file.ToInt32()   Marshal.SizeOf(typeof(int)))).TrimEnd(null);
                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }
                    Textbox_DebugConsole.Invoke((MethodInvoker)delegate
                    {
                        Textbox_DebugConsole.AppendText(string.Format("{0} - {1}", pid, text   Environment.NewLine));
                    });
                }
            }
            while (true);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
  • Related