Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to attach to existing console

Posted on 2001-06-29
8
Medium Priority
?
1,829 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

715 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