Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2009-09-07
This might be a bit geeky, but I just love it. Do you have some old CRT screen lying around at home? That is your new debug window! The CodeProject user Tomzhu has made a class for easy message sending and receiving between Windows programs, the BuildString class. So, I did the simplest ever fullscreen application (ScreenOut.exe) that uses the BuildString class as it is, to display incoming messages. (Yes, it is the simplest fullscreen application ever – consider it a proof of concept, nothing more.)
ScreenOut.exe asks for a screen to use at startup. To the left, I have my CRT running ScreenOut, and to the right I have Visual Studio (and some RSS reader). All I have to do, is make sure ScreenOut is running, if it isn’t nothing will happen.
In the program that I want to send data from, I add the BuildString class, and I do some changes to adapt it to my ScreenOut. First, I change the scope of the existing PostString function from Public to Private. Then I add a new static (Shared in Visual Basic) function, SendString, that will use the PostString function.
Public Shared Sub SendString(ByVal Text As String) Dim hwnd As Integer = FindWindow(vbNullString, "ScreenOut") If Not hwnd = 0 Then Dim Bs As New BuildString() Bs.PostString(hwnd, 1024, 0, Text) End If End Sub
This function asks for the ScreenOut application, and will find it by its name if it’s running. And if so, it will use the original PostString function to send data to ScreenOut. Note that I have to declare the FindWindow function.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) _ As Integer
And now, as if by magic, I have my own CRT screen that I can dump any junk onto, from multiple instances of Visual Studio. Whenever I want to send a message, I just call the SendString function.
#If DEBUG Then BuildString.SendString("Hello, external screen!") #End If
Just for the fun of it, if you use the #err prefix, the text will come out in red color.
#If DEBUG Then BuildString.SendString("#err Oh no!") #End If
Have fun!
Categories: Microsoft .NET
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Pro Tip: let the capturer listen to OutputDebugString (instead), then you can receive messages from System.Diagnostics.Debug* even if there is no debugger attached, just like Sysinternals debugview.