Re: Console and Form together in one application based on EXE parameters
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Sat, 18 Feb 2006 15:09:29 -0600
ajo@xxxxxxxxxxxx wrote:
b) I've tried not to set the {$APPTYPE CONSOLE} and open the console
using AllocConsole;, but this has again not the exact result as I would
like to get. This has the disadvantage when starting from "cmd" - the
application opens a second console window, which is unaceptable. I need
to write the output in to the standard window...
In one of my programs, I have code like this:
var
CreatedConsole: Boolean;
begin
CreatedConsole := AttachConsole(Attach_Parent_Process) or AllocConsole;
try
// ...
finally
if CreatedConsole then FreeConsole;
end;
end;
If the program was started from a command window, then AttachConsole will find that window and use it. Otherwise, AttachConsole will return False and AllocConsole will create a new window.
I realized, though, that I'd never actually tested this until just now. (This is a service application, so there's not much need for it to run in console mode anyway, except when I'm debugging it, in which case it's executed from the IDE, not from a command window.)
The code works as I've described, but there's a problem. The program is able to write to the parent's console window, but since the command prompt executed the program asynchronously, it still prompts for additional commands. Normally, when you run a console program, the command interpreter waits until the program completes before displaying another command prompt. I wonder whether there's a way to address that without using $APPTYPE CONSOLE.
Another solution is to have two programs. One will be the main program, and it won't be marked as a console application. The other will be a console application, and it will do nothing except invoke the main program in console mode, and then wait for it to terminate. Use the code above to have it attach to the console program's window.
When you invoke the console program from the command prompt, the command prompt will pause waiting for it to terminate, and so the main program will be free to use the same console window without interference.
There is a precedent for using separate console and non-console programs. The console version of the Java run-time is java.exe; the non-console version is javaw.exe. Likewise, the Windows Script Host uses cscript.exe and wscript.exe.
--
Rob
.
- Follow-Ups:
- Re: Console and Form together in one application based on EXE parameters
- From: Hans-Peter Diettrich
- Re: Console and Form together in one application based on EXE parameters
- References:
- Prev by Date: Re: Console and Form together in one application based on EXE parameters
- Next by Date: Re: How to get name of video card even if drivers not installed
- Previous by thread: Re: Console and Form together in one application based on EXE parameters
- Next by thread: Re: Console and Form together in one application based on EXE parameters
- Index(es):
Relevant Pages
|