VCL forms a request to write to stdout

My company has a large Windows application with a document-object model: open the application, open the file, make some changes, save, close. I am trying to disable the GUI from the top and create a console application that takes some parameters, opens a file, does something useful, saves, closes the file, and exits. Since there is a lot of legacy code out there, I am forced to use a VCL forms application and run it from the command line (or script batch). I really need to be able to print to stdout so that I can write status messages, respond to options like "--version" and "-?". I've spent all morning googling about this topic and I haven't found anything useful.

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

+2


source to share


3 answers


You can write to STDOUT in a GUI program, there usually won't be any output as there is no console unless started from a real console. Also, take a look at the GetStdHandle () and WriteConsole () functions in the Win32 API. If GetStdHandle () returns a valid handle, you can write to it. This is especially useful if your GUI application is being launched by another application that wants to intercept the STDOUT output for its own purposes.



+1


source


If you just want to show the console window, you can call AllocConsole and FreeConsole. Then you can simply call WriteLn ('xxx') as usual with your console application. However, if you run this application from the command line, it will still create a new console and the standard output will be sent to the new console, not to the calling console.



AllocConsole and FreeConsole are prototyped in the Windows block.

+2


source


Use {$ APPTYPE CONSOLE} in your project file. This will highlight the console (even if your application is still based on forms).

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

begin
  if ParamCount() > 0 then
    DoWhateverTheConsoleAppWouldDoHere()
  else
  begin
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end;

      

0


source







All Articles