13

I want to use OutputDebugString() in my application and then have the option to show it in a separate viewer when the application is deployed in the field.

That is to say, I don't want to have to change a flag and rebuild my .exe to turn debugging on and off.

Googling around, it seems like DebugView should handle that, but neither it, nor TraceTool show any output from this code.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
   OutputDebugString(PChar('Hello, wurld'));
end;

end.

I have read the documentation, to no avail, and see that others have had similar problems, but have not posted a solution.

Is there a solution?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 3
    [GExperts](http://gexperts.org) has a debug viewer and a unit you add to your app that will do this for you, even in XE2 starter. See the `DebugIntf` unit and the `GExpertsDebugWindow.exe` application that it includes. These work under XE2 Starter (and pretty much all other versions of Delphi). – Ken White Jun 27 '12 at 03:03
  • +1 I will check out http://www.gexperts.org/tour/index.html?debug_window.html – Mawg says reinstate Monica Jun 27 '12 at 03:26
  • If you posted that as an answer, I would probably award it, despite the sterling help from @RRUZ This is the solution that I will be going with - FOR EXE2 STARTER edition. It seems to be the only solution that works both in and out of the IDE. For other editions, I might prefer the greate roptions of TraceToool, but Gexperts looks more than fine for my needs. – Mawg says reinstate Monica Jun 27 '12 at 03:43
  • 4
    That's why I wrote this DbWinUnit: http://bo.codeplex.com/SourceControl/changeset/view/78543#1494219 it allows you to write both using OutputDebugString (that is catched by the currently running debugger) and uses the DBWIN protocol so that DebugView can also catch the output. – Jeroen Wiert Pluimers Jun 27 '12 at 05:21
  • 3
    have you tried this - http://www.codeproject.com/Articles/23776/Mechanism-of-OutputDebugString – RBA Jun 27 '12 at 07:38
  • Jeroen (+1), do you know how how it would behave with Delphi XE2 Starter? That does not have an event log window. If I run in the IDE would output then go to the external Debug Viewer? Or would it go to the IDE which has no window for me to view it? – Mawg says reinstate Monica Jun 28 '12 at 03:00
  • I remember SysInternals DebugView described, that depending on Windows version and executing subsystems, there wer 3 different OutputDebugString routines with its quirks, and loggers had its quirks too. There was recommendation to end each outputting string with `#13#10` (`\r\n`) – Arioch 'The Apr 05 '13 at 13:24
  • 1
    https://www.peganza.com/delphi-and-outputdebugstring.html – Gabriel Aug 02 '21 at 14:26

5 Answers5

29

The DebugView tool works fine; only be sure to launch your application directly (without the Delphi IDE or another debugger attached).

Anyway, the natural way to view the OutputDebugString output for a Delphi application is to use the Delphi IDE and the Event Log Window.

Enter image description here

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 1
    +1 can you confirm that XE2 starter edition doesn't have an event log viewer? http://www.embarcadero.com/products/delphi/delphi-feature-matrix seems to indicate so. Presumably, then, I can't view my output from the IDE, but only when I run it stand-alone? – Mawg says reinstate Monica Jun 27 '12 at 02:24
  • 4
    I can't believe which the `Event Log` Windows isn't part of the Delphi Starter edition :( – RRUZ Jun 27 '12 at 02:46
  • 1
    But it looks that way in the feature matix, right? And I can't find it on the menu ... – Mawg says reinstate Monica Jun 27 '12 at 03:04
  • 1
    Yes, it seems a Delphi Starter limitation. – RRUZ Jun 27 '12 at 03:06
  • 3
    Hmmm, I do understand that Embarcadero had to cut features for the starter (else no one would have bought anything else). But if the IDE doesn't handle it, couldn't they have made it so that outher programs, such as DebugView could handle OutputDebugString() while I am steeping through my my app in the IDE? – Mawg says reinstate Monica Jun 27 '12 at 03:06
  • 1
    I understand your frustration, Are you tried the Ken White suggestion about the GExperts plugin? – RRUZ Jun 27 '12 at 03:10
  • 1
    @RRUZ: I know a cool tool that you can use to fix the color scheme of your IDE. Interested? ;) – Wouter van Nifterick Jun 27 '12 at 21:42
  • 1
    One minor downside of the built-in Event Viewer is that it has no way to properly show a message with built-in line breaks--it folds it into a single line with "funny characters" in place of line breaks. Delphi 7 IDE (yes, I know it's year 2014 but still). – kostix Mar 18 '14 at 18:16
  • @Mawg I highly suspect that Starter-edition IDE actually handles (receives) it, but just don't show it. – Alex Mar 26 '15 at 16:55
  • @Mawg, So does it work or does it not work? Why did you accept the other answer as answer instead of this? – Pacerier Jul 14 '15 at 23:58
  • OutputDebugString output does not seem to appear in Event Log window. – Allan F Aug 01 '22 at 05:39
9

GExperts has a debug viewer and a unit you add to your application that will do this for you, even in XE2 Starter. See the DebugIntf unit and the GExpertsDebugWindow.exe application that it includes. These work under XE2 Starter (and pretty much all other versions of Delphi).

GExperts also includes tons of other IDE and editor enhancements that are great, and of course they've been around forever, so they're pretty solid tools.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • It doesn't run on a PC which has never had Delphi installed (can't find RTL60.BPL). I have posted to the GExperts mailing list, but so mfar had no reply. – Mawg says reinstate Monica Aug 11 '12 at 02:38
  • It's built with runtime packages. If the version number in the BPL you give isn't a typo (I think it should be 160, not 60), you have a very early version installed (60 was around Delphi 6, IIRC). If it is 160, you can find that in your Windows\System32 folder, and put it in the same folder as you put the debug viewer executable. It's one of Delphi's distributable runtime libraries, so you're allowed to ship it with the app. You'll need VCL160 and VCLX160, too. – Ken White Aug 11 '12 at 02:48
  • 1
    Oops, it was indeed 160, not 60. I had already copied that BPL, but did not know to copy the other two. That works nicely. Thank you, as always, for your excellent help. – Mawg says reinstate Monica Aug 11 '12 at 04:31
  • Careful, GExpers is known to crash the IDE: https://quality.embarcadero.com/browse/RSP-34374 – Gabriel Aug 02 '21 at 14:48
5

CnWizards contains a tool, CnDebugViewer.exe, which can capture OutputDebugString (should be run as administrator in Windows 7).

Not like DbgView, CnDebugViewer can create separated tabs for different applications.

CnPack contains an unit, CnDebug.pas. Using this unit, you can trace typed objects, collections, exceptions, memdump, etc. With CnDebug.pas, you also can set whether to autostart CnDebugViewer, whether dump to file while sending debug messages, etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shenloqi
  • 61
  • 1
4

One problem with using OutputDebugString is that other programs may also be using it, cluttering your log: Debugging OutputDebugString calls in Delphi.

You could use CodeSite Express, which we've been very happy with in day-to-day use: http://www.raize.com/devtools/codesite/Default.asp.

Community
  • 1
  • 1
RobertFrank
  • 7,332
  • 11
  • 53
  • 99
  • 1
    That's only a problem if you're using a tool that intentionally collects messages from other programs and then offers no way to filter them. Plain old debuggers won't have that problem because `OutputDebugString` usually only sends messages to the single program debugging it. The SysInternals tool uses a different, non-standard technique for collecting messages without becoming a debugger for all processes. But it also allows filtering. So there's really no problem at all. – Rob Kennedy Jun 27 '12 at 14:50
  • 1
    Code Site does not appear to be free :-( – Mawg says reinstate Monica Aug 11 '12 at 02:36
  • CS Express is included in XE2 & XE3. I've been using the full version for years; outstanding tool, much more useful than it may appear at first glance. – casterle Dec 07 '12 at 15:48
2

In my version of XE5, I had to enable the "Output Messages" option under:

Tools > Options > Debugger Options > Event Log

I don't recall disabling it.

bvj
  • 3,294
  • 31
  • 30