using System; using System.Diagnostics; using System.Net.Sockets; using System.Text; using System.Runtime.InteropServices; namespace Utils { /// /// Summary description for XMLDebugger. /// public class UdpXmlDebugger : TraceListener { public override void WriteLine(string Msg) { Write(Msg); } public override void Write(string Msg) { SendMessage(Msg); } public static void SendMessage(string Msg) { const int port = 6767; //Create the UdpClient using (UdpClient client = new UdpClient()) { //Send DataGram UnicodeEncoding encode = new UnicodeEncoding(); byte[] sendData = encode.GetBytes(Msg); //Send to Server client.Send(sendData, sendData.Length, "localhost", port); } } } public class MessageXmlDebugger : TraceListener { // Import the SendMessage method of the User32 DLL. [DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern IntPtr FindWindow(string className, string windowName); struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; public IntPtr lpData; } public override void WriteLine(string Msg) { SendMessage(Msg); } public override void Write(string Msg) { SendMessage(Msg); } public static void SendMessage(string msg) { IntPtr DebugWindow = FindWindow(null, "XML Debugger"); const int WM_COPYDATA = 0x004A; if (DebugWindow != IntPtr.Zero) { IntPtr MsgPtr = Marshal.AllocCoTaskMem(msg.Length*2); Marshal.Copy(msg.ToCharArray(), 0, MsgPtr, msg.Length); COPYDATASTRUCT cds; cds.cbData = msg.Length*2; cds.dwData = IntPtr.Zero; cds.lpData = MsgPtr; IntPtr lpar = Marshal.AllocCoTaskMem(Marshal.SizeOf(cds)); Marshal.StructureToPtr(cds, lpar, false); SendMessage(DebugWindow, WM_COPYDATA, IntPtr.Zero, lpar); Marshal.FreeCoTaskMem(lpar); Marshal.FreeCoTaskMem(MsgPtr); } } } }