Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Windows Services

Discussion in 'Delphi Programming' started by AdminDF, Feb 14, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member DelphiFan Administrator Staff Member DF Staff

    Code:
    unit ServiceManager;
    
    interface
    
    uses
    SysUtils, Windows, WinSvc;
    
    type
    
    TServiceManager = class
    private
    { Private declarations }
    ServiceControlManager: SC_Handle;
    ServiceHandle: SC_Handle;
    protected
    function DoStartService(NumberOfArgument: DWORD; ServiceArgVectors: PChar): Boolean;
    public
    { Public declarations }
    function Connect(MachineName: PChar = nil; DatabaseName: PChar = nil;
    Access: DWORD = SC_MANAGER_ALL_ACCESS): Boolean; // Access may be SC_MANAGER_ALL_ACCESS
    function OpenServiceConnection(ServiceName: PChar): Boolean;
    function StartService: Boolean; overload; // Simple start
    function StartService(NumberOfArgument: DWORD; ServiceArgVectors: PChar): Boolean;
    overload; // More complex start
    function StopService: Boolean;
    procedure PauseService;
    procedure ContinueService;
    procedure ShutdownService;
    procedure DisableService;
    function GetStatus: DWORD;
    function ServiceRunning: Boolean;
    function ServiceStopped: Boolean;
    end;
    
    implementation
    
    { TServiceManager }
    
    function TServiceManager.Connect(MachineName, DatabaseName: PChar;
    Access: DWORD): Boolean;
    begin
    Result := False;
    { open a connection to the windows service manager }
    ServiceControlManager := OpenSCManager(MachineName, DatabaseName, Access);
    Result := (ServiceControlManager <> 0);
    end;
    
    
    function TServiceManager.OpenServiceConnection(ServiceName: PChar): Boolean;
    begin
    Result := False;
    { open a connetcion to a specific service }
    ServiceHandle := OpenService(ServiceControlManager, ServiceName, SERVICE_ALL_ACCESS);
    Result := (ServiceHandle <> 0);
    end;
    
    procedure TServiceManager.PauseService;
    var
    ServiceStatus: TServiceStatus;
    begin
    { Pause the service: attention not supported by all services }
    ControlService(ServiceHandle, SERVICE_CONTROL_PAUSE, ServiceStatus);
    end;
    
    function TServiceManager.StopService: Boolean;
    var
    ServiceStatus: TServiceStatus;
    begin
    { Stop the service }
    Result := ControlService(ServiceHandle, SERVICE_CONTROL_STOP, ServiceStatus);
    end;
    
    procedure TServiceManager.ContinueService;
    var
    ServiceStatus: TServiceStatus;
    begin
    { Continue the service after a pause: attention not supported by all services }
    ControlService(ServiceHandle, SERVICE_CONTROL_CONTINUE, ServiceStatus);
    end;
    
    procedure TServiceManager.ShutdownService;
    var
    ServiceStatus: TServiceStatus;
    begin
    { Shut service down: attention not supported by all services }
    ControlService(ServiceHandle, SERVICE_CONTROL_SHUTDOWN, ServiceStatus);
    end;
    
    function TServiceManager.StartService: Boolean;
    begin
    Result := DoStartService(0, '');
    end;
    
    function TServiceManager.StartService(NumberOfArgument: DWORD;
    ServiceArgVectors: PChar): Boolean;
    begin
    Result := DoStartService(NumberOfArgument, ServiceArgVectors);
    end;
    
    function TServiceManager.GetStatus: DWORD;
    var
    ServiceStatus: TServiceStatus;
    begin
    { Returns the status of the service. Maybe you want to check this
    more than once, so just call this function again.
    Results may be: SERVICE_STOPPED
    SERVICE_START_PENDING
    SERVICE_STOP_PENDING
    SERVICE_RUNNING
    SERVICE_CONTINUE_PENDING
    SERVICE_PAUSE_PENDING
    SERVICE_PAUSED }
    Result := 0;
    QueryServiceStatus(ServiceHandle, ServiceStatus);
    Result := ServiceStatus.dwCurrentState;
    end;
    
    procedure TServiceManager.DisableService;
    begin
    { Implementation is following... }
    end;
    
    function TServiceManager.ServiceRunning: Boolean;
    begin
    Result := (GetStatus = SERVICE_RUNNING);
    end;
    
    function TServiceManager.ServiceStopped: Boolean;
    begin
    Result := (GetStatus = SERVICE_STOPPED);
    end;
    
    function TServiceManager.DoStartService(NumberOfArgument: DWORD;
    ServiceArgVectors: PChar): Boolean;
    var
    err: integer;
    begin
    Result := WinSvc.StartService(ServiceHandle, NumberOfArgument, ServiceArgVectors);
    end;
    
    end.
     
  2. wozengcong
    Offline

    wozengcong Guest

    RE:

    Thank you very much.
     
  3. Sha
    Offline

    Sha DF Member

    RE:

    Great job, thx :)
     
  4. maymay_x
    Offline

    maymay_x Guest

    RE:

    thanks
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block