Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:27:52+00:00 2026-05-15T02:27:52+00:00

I’m looking for Delphi sample code to develope a Win32 Windows service which can

  • 0

I’m looking for Delphi sample code to develope a Win32 Windows service which can be installed many times (with different Name).
The idea is to have 1 exe and 1 registry key with 1 subkey for every service to be installed.
I use the exe to install/run many service, every service take his parameter from his registry subkey.

Does anyone have a sample code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:27:53+00:00Added an answer on May 15, 2026 at 2:27 am

    We’ve done this by creating a TService descendant and adding an ‘InstanceName’ property. This gets passed on the command line as something like … instance=”MyInstanceName” and gets checked for and set (if it exists) before SvcMgr.Application.Run.

    eg
    Project1.dpr:

    program Project1;
    
    uses
      SvcMgr,
      SysUtils,
      Unit1 in 'Unit1.pas' {Service1: TService};
    
    {$R *.RES}
    
    const
      INSTANCE_SWITCH = '-instance=';
    
    function GetInstanceName: string;
    var
      index: integer;
    begin
      result := '';
      for index := 1 to ParamCount do
      begin
        if SameText(INSTANCE_SWITCH, Copy(ParamStr(index), 1, Length(INSTANCE_SWITCH))) then
        begin
          result := Copy(ParamStr(index), Length(INSTANCE_SWITCH) + 1, MaxInt);
          break;
        end;
      end;
      if (result <> '') and (result[1] = '"') then
        result := AnsiDequotedStr(result, '"');
    end;
    
    var
      inst: string;
    
    begin
      Application.Initialize;
      Application.CreateForm(TService1, Service1);
      // Get the instance name
      inst := GetInstanceName;
      if (inst <> '') then
      begin
        Service1.InstanceName := inst;
      end;
      Application.Run;
    end.
    

    Unit1 (a TService descendant)

    unit Unit1;
    
    interface
    
    uses
      Windows, SysUtils, Classes, SvcMgr, WinSvc;
    
    type
      TService1 = class(TService)
        procedure ServiceAfterInstall(Sender: TService);
      private
        FInstanceName: string;
        procedure SetInstanceName(const Value: string);
        procedure ChangeServiceConfiguration;
      public
        function GetServiceController: TServiceController; override;
        property InstanceName: string read FInstanceName write SetInstanceName;
      end;
    
    var
      Service1: TService1;
    
    implementation
    
    {$R *.DFM}
    
    procedure ServiceController(CtrlCode: DWord); stdcall;
    begin
      Service1.Controller(CtrlCode);
    end;
    
    procedure TService1.ChangeServiceConfiguration;
    var
      mngr: Cardinal;
      svc: Cardinal;
      newpath: string;
    begin
      // Open the service manager
      mngr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
      if (mngr = 0) then
        RaiseLastOSError;
      try
        // Open the service
        svc := OpenService(mngr, PChar(Self.Name), SERVICE_CHANGE_CONFIG);
        if (svc = 0) then
          RaiseLastOSError;
        try
          // Change the service params
          newpath := ParamStr(0) + ' ' + Format('-instance="%s"', [FInstanceName]); // + any other cmd line params you fancy
          ChangeServiceConfig(svc, SERVICE_NO_CHANGE, //  dwServiceType
                                   SERVICE_NO_CHANGE, //  dwStartType
                                   SERVICE_NO_CHANGE, //  dwErrorControl
                                   PChar(newpath),    //  <-- The only one we need to set/change
                                   nil,               //  lpLoadOrderGroup
                                   nil,               //  lpdwTagId
                                   nil,               //  lpDependencies
                                   nil,               //  lpServiceStartName
                                   nil,               //  lpPassword
                                   nil);              //  lpDisplayName
        finally
          CloseServiceHandle(svc);
        end;
      finally
        CloseServiceHandle(mngr);
      end;
    end;
    
    function TService1.GetServiceController: TServiceController;
    begin
      Result := ServiceController;
    end;
    
    procedure TService1.ServiceAfterInstall(Sender: TService);
    begin
      if (FInstanceName <> '') then
      begin
        ChangeServiceConfiguration;
      end;
    end;
    
    procedure TService1.SetInstanceName(const Value: string);
    begin
      if (FInstanceName <> Value) then
      begin
        FInstanceName := Value;
        if (FInstanceName <> '') then
        begin
          Self.Name := 'Service1_' + FInstanceName;
          Self.DisplayName := Format('Service1 (%s)', [FInstanceName]);
        end;
      end;
    end;
    
    end.
    

    Usage:
    Project1.exe /install
    Project1.exe /install -instance=”MyInstanceName”
    Project1.exe /uninstall [-instance=”MyInstanceName]
    It doesn’t actually do anything – it’s up to you to write the start/stop server bits etc.

    The ChangeServiceConfiguration call is used to update the real command line that the service manager calls when it starts up. You could just edit the registry instead but at least this is the ‘proper’ API way.

    This allows any number of instances of the service to be run at the same time and they will appear in the service manager as ‘MyService’, ‘MyService (Inst1)’, ‘MyService (AnotherInstance)’ etc etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes it is a good idea and relatively easy to… May 16, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure of what you're looking for exactly. But… May 16, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer With Chrome 5.0.375.126 on MacOSX 10.5.8 on my Mac laptop,… May 16, 2026 at 4:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.