2

My company has a large Windows application with a document-object model: open application, open a file, make some changes, save, close. I'm attempting to cut the GUI off of the top and create a console application that accepts some parameters, opens a file, does something useful, saves, closes the file, and terminates. Because there is a large amount of legacy code, I'm forced to use VCL forms application and launch it from the command line (or batch script). I really need to be able to print to stdout so I can write out status messages, respond to options like "--version" and "-?". I've spent all morning doing Google searches on this topic and I haven't found anything that's helpful.

The application is written in CodeGear C++ Builder 2007 using the VCL.

3 Answers 3

2

If you just want to show a console window you can call AllocConsole and FreeConsole Then you can just call WriteLn('xxx') like you normally would with a Console application. However, if you run this application from the command line it will still create a new console and the standard output will go to the new console rather than the calling console.

AllocConsole and FreeConsole are prototyped in the Windows unit.

1

You can write to STDOUT in a GUI program, there just usually won't be any output since there is no Console, unless it is launched from an actual Console. Alternatively, look at the GetStdHandle() and WriteConsole() functions in the Win32 API. If GetStdHandle() returns a valid handle, then you can write to it. This is particlarly useful if your GUI app is launched by another app that wants to intercept your STDOUT output for its own purposes.

1
  • Hmm, I ended up not using any console output for now, but I'll try this when I come back to it. Thanks. Sep 24, 2009 at 21:43
0

Use {$APPTYPE CONSOLE} in your project file. This will allocate a console (even though your app is still based on forms).

Alternatively, you can do branching based on command-line parameters in the project (.DPR) file (Delphi code follows - up to you to convert to C++ Builder equivalent). You'll still need the APPTYPE definition, or you'll need to use Win32 API console functions to create your own console (see MSDN Console Functions for more info):

begin
  if ParamCount() > 0 then
    DoWhateverTheConsoleAppWouldDoHere()
  else
  begin
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end;
2
  • 1
    APPTYPE applies to Delphi, not to C++Builder. There is no equivilent to C++. Jonathan needs to create an actual "Console" project, instead of a GUI project. Sep 23, 2009 at 22:34
  • Ah. Thanks, Remy, for the correction. I guess the actual way to do it is with the console functions I referred Jonathan to in my answer.
    – Ken White
    Sep 24, 2009 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.