Solved

How to attach to existing console

Posted on 2001-06-29
8
1,805 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

628 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