Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Console Mode without console compilation... 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
How can console mode programs be done without {$APPTYPE CONSOLE} or compiling the programs for console mode?

Wanting to do something involving the console, but in the current state the window flashes up as a non-coded initialization, even if nothing is done in the program.
 
Look into the Windows Console commands. I believe you can open a console window and manipulate it using these. However be warned that using this method will prevent capturing your output in other applications, including on the command line.
 
That's what I've been doing. It works fine with AllocConsole and FreeConsole, but I don't want the second console window to come up if I'm in a command-line console calling the program.
 
glen, do you just want to have a "windowless" application?

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
no, what I've been trying to do is have a mixture...like..

if parmcount > 0 then
check parms & write stuff to console
else
bring up GUI form to ask for parms
Do stuff and output to GUI form.

Basically what happens behind the scenes with apptype console is what I'm wanting to know.

(and yes you could do this with apptype console, but it still flashes up the console window, which is an unattractive thing)

 
why not make a standard windowed vcl app?

project source looks like this

Code:
program Test;

uses
  Forms,
  u_frm_main in 'u_frm_main.pas' {Frm_main};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TFrm_main, Frm_main);
  Application.Run;
end.

now adapt the dpr to meet your demand:

Code:
program Test;

uses
  Forms,
  u_frm_main in 'u_frm_main.pas' {Frm_main};

{$R *.res}

begin
if ParamCount > 0 then                
  begin
   // see if program is called from console, if so,  write to it
   // I'm searching at the moment how to implement this part...
 else
 begin
  // start normal GUI
  Application.Initialize;
  Application.CreateForm(TFrm_main, Frm_main);
  Application.Run;
 end;
end.

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
That's the basic idea I had. The problem lies where you have the comments:
Code:
  // see if program is called from console, if so,  write to it
   // I'm searching at the moment how to implement this part...

Like was said it can be done with {$APPTYPE CONSOLE}, but the problem is the console gets placed onto screen as part of the initialization codes for the program before the "begin" part of the dpr ever gets hit.
 
forget the $APPTYPE console thing,

the real problem is how to detect from what your program is called (explorer.exe or cmd.exe). I suppose you only want to give feedback when run from a command window, right?

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Hi,

found out how to do it!!

Code:
program Test;

uses
  Windows,
  Forms,
  u_frm_main in 'u_frm_main.pas' {Frm_main};

{$R *.res}
function AttachConsole(dwProcessId : Longint): BOOL; stdcall; external kernel32 name 'AttachConsole';


begin
if ParamCount > 0 then                
  begin
   // check if parent process is a console, if so write to it
  if AttachConsole(-1) then
   begin
     writeln('not enoug parameters!');
     FreeConsole; // detach from parent console
   end;
  end
 else
 begin
  // start normal GUI
  Application.Initialize;
  Application.CreateForm(TFrm_main, Frm_main);
  Application.Run;
 end;
end.

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks for the push in the right general direction. It's by no means the perfect solution as posted (just noting that for someone else that might happen by that a lot more code needs written to support it), but a very good start.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top