Solved

How to attach to existing console

Posted on 2001-06-29
8
1,744 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now