What I've done so far is a simple message system that seems to work so far. I don't want to output the debug content inside a designer generated window. I could create a Form Window, I know. Or even better I could create an other application to communicate with the designer. For the moment I'm satisfied with a very simple solution. Just to get any output at all.
I made a few interesting experiences, e.g. to get the solution / project folder at design time.
It's a bit of a tinker, but I'm pretty happy with it. So I had the absurd idea to simply output the text internally into a notepad window. I have no "using" namespaces to make it more flexible and clearly expressed. There is also a dispose implementation. I'm not sure if that makes sense. But my idea was to avoid a persistent thread if the class is deconstructed during design time. Or just to be prepared for the case. Here's the code:
public class SendToNotepad : System.IDisposable
{
private bool _disposed;
public SendToNotepad() { }
public static void Text(string text, bool d) { Send.SendText(text, d); }
~SendToNotepad() { Dispose(false); }
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) { return; }
if (disposing)
{
// Dispose managed state
}
Send.Dispose();
_disposed = true;
}
private static class Send
{
private const int STARTF_USESHOWWINDOW = 1;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOWMINNOACTIVE = 7;
private const int CREATE_NEW_CONSOLE = 0x000010;
private const int EM_SETSEL = 0x00B1;
private const int EM_REPLACESEL = 0x00C2;
private const int WM_SETTEXT = 0x000C;
private const int WM_GETTEXTLENGTH = 0x000E;
private const int WM_COMMAND = 0x0111;
private const int WM_APPCOMMAND = 0x0319;
private const int WM_QUIT = 0x0012;
private const int APPCOMMAND_SAVE = 0x201000;
private const int SleepTime = 10;
private static UnsafeNativeMethods.STARTUPINFO _si;
private static UnsafeNativeMethods.PROCESS_INFORMATION _pi;
private static System.Diagnostics.Process _p;
private static System.Threading.Timer _timer;
private static bool _isWaiting;
private static string _waitingCatcher;
private static string _debugFileName;
private static readonly string Namespace = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;
private static readonly string[] TitleLoopAniChars = new[] { "|", "/", "––", "\\" };
private static readonly int TitleLoopAniCharsLength = TitleLoopAniChars.Length - 1;
public static void Dispose()
{
if (_p != null)
{
try
{
UnsafeNativeMethods.CloseHandle(_pi.hProcess);
UnsafeNativeMethods.CloseHandle(_pi.hThread);
}
catch { }
_p.Dispose();
_p = null;
}
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
public static void SendText(string text, bool d)
{
if (!d) { return; }
text = System.DateTime.Now.TimeOfDay + ": " + text + System.Environment.NewLine;
if (_isWaiting)
{
_waitingCatcher += text;
return;
}
_isWaiting = true;
int maxWait = 200; // Max timeout over all (* SleepTime)
if (_p == null)
{
_debugFileName = GetDebugFileName();
if (System.String.IsNullOrEmpty(_debugFileName))
{
_waitingCatcher += text;
_isWaiting = false;
return;
}
if (!System.IO.File.Exists(_debugFileName))
{
try { System.IO.File.Create(_debugFileName).Dispose(); }
catch { }
}
if (!System.IO.File.Exists(_debugFileName))
{
_waitingCatcher += text;
_isWaiting = false;
return;
}
_si = new UnsafeNativeMethods.STARTUPINFO
{
dwFlags = STARTF_USESHOWWINDOW,
wShowWindow = SW_SHOWMINNOACTIVE,
cb = System.Runtime.InteropServices.Marshal.SizeOf(_si)
};
bool success = UnsafeNativeMethods.CreateProcess(null, "notepad /W \"" + _debugFileName + "\"", System.IntPtr.Zero, System.IntPtr.Zero, true, 0, System.IntPtr.Zero, null, ref _si, out _pi);
while (maxWait-- > 0 && success)
{
System.Threading.Thread.Sleep(SleepTime);
try { _p = System.Diagnostics.Process.GetProcessById(_pi.dwProcessId); } // grab Process to handle WaitForExit()
catch { }
if (_p != null) { break; }
}
if (_p == null)
{
_waitingCatcher += text;
_isWaiting = false;
return;
}
while (maxWait-- > 0 && (!_p.Responding || !_p.WaitForInputIdle())) { System.Threading.Thread.Sleep(SleepTime); }
_timer = new System.Threading.Timer(NotifyOnProcessExits);
_timer.Change(0, 0);
}
else
{
while (maxWait-- > 0 && (!_p.Responding || !_p.WaitForInputIdle())) { System.Threading.Thread.Sleep(SleepTime); }
}
System.IntPtr fwx = UnsafeNativeMethods.FindWindowEx(_p.MainWindowHandle, System.IntPtr.Zero, "Edit", null);
UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, 0, -1);
UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, -1, -1);
UnsafeNativeMethods.SendMessageW(fwx, EM_REPLACESEL, 1, text);
if (!System.String.IsNullOrEmpty(_waitingCatcher))
{
UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, 0, -1);
UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, -1, -1);
UnsafeNativeMethods.SendMessageW(fwx, EM_REPLACESEL, 1, _waitingCatcher);
_waitingCatcher = "";
}
UnsafeNativeMethods.SendMessage(_p.MainWindowHandle, WM_COMMAND, 0x0003, 0x0); // first menu, item 3 (save)
_isWaiting = false;
}
private static void NotifyOnProcessExits(object timer)
{
if (_p == null) { return; }
// _p.WaitForExit();
// This is just for fun as response feedback
int i = 0;
while (!_p.HasExited)
{
System.Threading.Thread.Sleep(500);
UnsafeNativeMethods.SetWindowText(_p.MainWindowHandle, Namespace + " –> " + _debugFileName + " " + TitleLoopAniChars[i]);
i = i == TitleLoopAniCharsLength ? 0 : i + 1;
}
Dispose();
}
private static string GetDebugFileName() // Hack to get solution path while design time
{
string s;
try
{
var trace = new System.Diagnostics.StackTrace(true);
var frame = trace.GetFrame(0);
s = System.IO.Path.GetDirectoryName(frame.GetFileName());
}
catch { return null; }
return s == null ? null : s + "Debug.txt";
}
[System.Security.SuppressUnmanagedCodeSecurity]
private static class UnsafeNativeMethods
{
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct STARTUPINFO
{
public System.Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public System.Int32 dwX;
public System.Int32 dwY;
public System.Int32 dwXSize;
public System.Int32 dwYSize;
public System.Int32 dwXCountChars;
public System.Int32 dwYCountChars;
public System.Int32 dwFillAttribute;
public System.Int32 dwFlags;
public System.Int16 wShowWindow;
public System.Int16 cbReserved2;
public System.IntPtr lpReserved2;
public System.IntPtr hStdInput;
public System.IntPtr hStdOutput;
public System.IntPtr hStdError;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
public System.IntPtr hProcess;
public System.IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
System.IntPtr lpProcessAttributes,
System.IntPtr lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
System.IntPtr lpEnvironment,
string lpCurrentDirectory,
[System.Runtime.InteropServices.In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation
);
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool CloseHandle(System.IntPtr hObject);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(System.IntPtr hWnd, int uMsg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern System.IntPtr SendMessage(System.IntPtr hWnd, int uMsg, System.IntPtr wParam, System.IntPtr lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
public static extern int SendMessageW(System.IntPtr hWnd, int uMsg, int wParam, string lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern System.IntPtr DefWindowProc(System.IntPtr hWnd, int msg, System.IntPtr wParam, System.IntPtr lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern System.IntPtr FindWindowEx(System.IntPtr hwndParent, System.IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SetWindowText(System.IntPtr hWnd, string text);
}
}
}
And inside a component class:
private static readonly bool DesignTime = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
SendToNotepad.Text("Debug text", DesignTime);
That has the nice (unexcepted) side effect that if VS crashes the debug content is retained.

And now I have the still more absurd idea of hooking into the vs process and sending the text back. I think I'm digging myself in a hole deeper and deeper. ;)