1

I have written a number of services in the past for Delphi using the framework. I would now like to extend a service with some console like features.

The easiest example I can provide is that I'd like to run the service executable with something like the following from a Command prompt.

> myservice.exe /version

MyService Version 1.0

In the project file, I'd handle the parameter and exit prior to the service initializing and be done.

If ParamStr(1) = '/version' then
begin
   writeln ('MyService Version 1.0');
   exit;
end;

// Other standard service launch code is after this for proper initialization
// when run as a service, i.e.
if not Application.DelayInitialize or Application Installing then 
...

However to get a writeln statement to work, typically I would need the directive {$APPTYPE CONSOLE} in the project file which then breaks the service app Destroy event.

Is there another way to wire up standard output to a console without using the {$APPTYPE CONSOLE} directive for a Delphi Windows Service App?

2
  • 1
    You need a console app. Find a way to fix the service running as a console app, or use two executables. Commented Nov 28, 2012 at 22:05
  • In which way does APPTYPE CONSOLE break the service Destroy event? Commented Nov 29, 2012 at 8:04

1 Answer 1

1

New own console

begin

  if paramstr(1)='/?' then
    begin
      if Windows.AllocConsole then
      try
        WriteLn;
        // irgendeine sinnvolle Information, z.B.:
        WriteLn('Your Info');
        readln;
      finally
        FreeConsole;
      end;
    end

  else
     begin
     //Your Appcode

or attach to console, without creating own console

begin

  if paramstr(1) = '/?' then
  begin

    if AttachConsole($FFFFFFFF) then
    begin
      WriteLn('Your Info');
      Readln;
      FreeConsole; 
    end;
  end

  else
  begin
  // Your Appcode
Sign up to request clarification or add additional context in comments.

5 Comments

When I use the Windows.AllocConsole technique, it does create a console session, but opens a new console window which then close when the application exits. Ideally, I'd rather have the output appear in the current console window from where I run the application. Thanks for the idea though.
added a version without new console
Attaching to the console is also wrong. When the command interpreter starts a non-console program, it does not wait for the program to finish running before it proceeds. In this case, the command interpreter will print another prompt, and that may or may not get printed before the non-console program prints its own text to the same console. If you want to use your parent process's console, you must be a console program from the very start.
Rob's right. Try as you might, if you want a well-behaved console app, you must target the console subsystem.
For each service I create two executables, the console and the service. The console loads the service if not loaded and talks to the service. BTW, I do the same when I want a dual CLI-GUI program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.