Re: Console and Form together in one application based on EXE parameters



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
.



Relevant Pages

  • Re: working set and console applications
    ... I created a windows application and then I created a child console process ... that I started from the parent process using CreateProcess. ... I minimize the child console window it also causes the working set of the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Console applications window size
    ... AllocConsole() or AttachConsole. ... You already have a console, ... SMALL_RECT srWindowRect; // hold the new window size ... >exe from a cmd prompt, the same cmd prompt gets bigger properly. ...
    (microsoft.public.vc.language)
  • Re: How to minimize command mode window in console program?
    ... to set some property to minimize the console window. ...     ByVal hWnd As IntPtr, ...     End Sub ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Prevent Domain Users From Browsing Around in Active Directory?
    ... View, Customize, uncheck everything except the top one that says Console ... But they will have the rt-click feature. ... Rt-click on that OU, new window from here. ...
    (microsoft.public.windows.server.active_directory)
  • Re: vb6 run from and output to command window
    ... VB doesn't really create true console applications (because the linker ... allocate the console window, and read/write from/to it. ... Private Declare Function AllocConsole Lib "kernel32" As Long ... Dim lResult As Long ...
    (microsoft.public.vb.general.discussion)