Solved

How to attach to existing console

Posted on 2001-06-29
8
1,772 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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
Printing problem 2 102
can't find the executable in Simulator 1 101
Tidtcpserver listening on multiports? 1 35
FMX TEdit KeyUp handler detecting  "enter" key 4 14
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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