14

I recently had to do some changes in some Delphi code. Therefore, I have some basics questions:

  1. Generally, how do I output to the console?
  2. How do I output to the console with fx that is a string variable?

I started using OutputDebugString, but I couldn't get it working with a variable.

Sae1962
  • 1,122
  • 15
  • 31
  • When you say "output to console" are you actually developing a console app or do you want debug output printed? – Tony Feb 02 '11 at 10:44
  • Its an application with a GUI. I just want to write out in the Event log for debugging variables like i'm used to in fx. Java. –  Feb 02 '11 at 10:46
  • 4
    See also: http://stackoverflow.com/questions/4421042/how-can-i-write-to-console-window-for-debugging – Sertac Akyuz Feb 02 '11 at 10:47
  • 1
    `{$IFDEF DEBUG}OutputDebugString(PChar(Format('Result = %d', [Result])));{$ENDIF}` (* save that as code template *) – Free Consulting Feb 02 '11 at 11:10
  • 1
    OutputDebugString does not write to the "console" or "event log". Delphi calls it "event log" but it is not the Windows Event Log. It's a "debugger log". OutputDebugString sends string to a registered debugger. SysInternals calls its debug string utility Debug View correctly. –  Feb 02 '11 at 17:28

3 Answers3

29

You can write a wrapper function to take care of the variables passed to OutputDebugString as it expects a PChar.

Something like:

procedure DebugMsg(const Msg: String);
begin
    OutputDebugString(PChar(Msg))
end;

There is a useful reference for debugging techniques here.

And if your Delphi is a bit rusty there's the ever useful Delphi Basics site. I use it a lot :)

Tony
  • 9,672
  • 3
  • 47
  • 75
4

In addition to the 2 answers you got about OutputDebugString() and WriteLn(), for debugging there is a better solution: CodeSite from Raize Software (see http://www.raize.com/DevTools/CodeSite/Default.asp ).

If you have Delphi XE, that should already come with an somewhat reduced functionality version of CodeSite.

Thorsten Engler
  • 2,333
  • 12
  • 13
0

If you have a console application, just use write() and writeln() global functions. If you have a GUI application and want to create a separate console windows, things go tricky (this article will guide you through the process, though it's in C++).

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121