How to debug a Delphi application with redirected output
-
27-09-2019 - |
Question
I've got a console application that crashes with an I/O error 6 when the output is redirected to a file. It probably has something to do with the fact that the console application changes the text color, which doesn't make much sense in a file.
- This works:
c:\dir\app.exe
- This crashes:
c:\dir\app.exe >out.txt
When I supply >out.txt
as a parameter in the IDE (run\parameters\parameters\
), I just get >out.txt
as a parameter.
How can I debug the application with the stdout redirected to a file instead of the console?
Solution
Redirection is made by the command line interpreter, in windows it is cmd.exe
To debug the application, just launch a cmd.exe with propers arguments to launch your application and redirect the output, for example:
cmd.exe /c "yourapplication.exe >redirect.txt"
To make this happen from inside IDE in order to debug, configure cmd.exe as the host application (Run/Parameters):
Put a breakpoint where you want to stop, and launch a new cmd.exe (Project/Load process) with "Run to first source" after load action:
And you're done... the debugger must stop the application at your breakpoint.
OTHER TIPS
You could try remote debugging:
at the beginning of the application, add a
ReadLn;
which gives you time to attach to the process from within Delphistart the application from a command line (specifying the >out.txt parameter)
in Delphi, connect with the app process (
Run | Attach to Process...
), set a breakpoint and then switch to the application to enter a key
Hint: a debugger breakpoint can also be set in code:
asm
int 3
end;
Make sure it isn't failing due to directory permissions. Fully-qualify the output file path to somewhere that you're guaranteed to be able to write. Otherwise, Windows7 may be playing games with the directory. (VirtualStore stuff).