Solved

How to attach to existing console

Posted on 2001-06-29
8
1,786 Views
Last Modified: 2007-12-19
Have a Delphi GUI application. Application start in GUI mode if a paramcount=0 and in Console mode else.

program GUI_and_Console

begin
 If ParamCount>0 Then
  Begin
   AllocConsole;
   WriteLn('Console mode!!!');
   FreeConsole;
   Halt(0);
  End;
 Application.Initialize;
 ...
 Application.Run;
end.

If application start wth some command line parameter from batch file or from Far or from DOS command prompt it's open a new console. How check that the parent process already have console and attach to them for outputing text?
0
Comment
Question by:lmikle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6237356
You could try:

if GetStdHandle(STD_OUTPUT_HANDLE) <= 0 then
  // invalid console handle
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6237436
You don't need to call AllocConsole/FreeConsole, you can use {$APPTYPE CONSOLE} directive to generate the code for you.
Then you can use the standard Input, Output variables for stdin, stdout respectively.

Write, Writeln implicitly use Output by default if you don't specify a file variable, and Read, Readln use Input.

For example:

program Project1;

uses
  Forms, Windows, SysUtils,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}
{$APPTYPE CONSOLE}

var
  C: Char;

begin
  if (ParamCount > 0) and (UpperCase(ParamStr(1)) = '/C') then
  begin
    try
      while True do
      begin
        Writeln('Memory error: Neuron not found. (A)bort, (R)etry, (H)ave another beer?');
        Readln(C);
        case UpCase(C) of
          'A':
            Break;
          'H':
            Writeln('Cheers! <repeat until shutdown>');
          else
            ;
        end;
      end;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end
  else
  begin
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end.

HTH
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6237462
TOndrej, I think lmikle only wants a console when necessary...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:TOndrej
ID: 6237501
But if you don't specify {$APPTYPE CONSOLE} then you get a GUI application (a flag in the PE header).
When you run it from command line it will open a new console window.
If you want the app to behave as a true console app then I think you have to use {$APPTYPE CONSOLE}. That will give you "real" console mode and no problems in GUI mode (apart from console being allocated unnecessarily but I don't think it's a big deal).
0
 
LVL 3

Author Comment

by:lmikle
ID: 6237612
2 TOndrei

Ii use a APPTYPE CONSOLE then start in GUI mode the console window are in a back of GUI window. But i want to open console window only if user start app in console mode and program can't find already allocated console window.
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6237807
> the console window are in a back of GUI window

You're right this happens when you run the app from explorer not from command line... I haven't noticed this, thanks for the info.
Sorry, I'm out of ideas. I guess a windows executable can only be compiled as either console or GUI, not both.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8700637
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

To be PAQ/Refund

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 8816272
PAQ'ed and points not refunded ,

PashaMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
QRReport  TQrmemo vertical stretching 1 67
can't find the executable in Simulator 1 129
Delphi and Access based Enumeration 9 90
Get weeknumber and year from date 4 56
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question