windows服务

2021-03-01 06:25

阅读:672

标签:except   star   use   process   word   返回   char   end   overload   

windows服务

unit untserverinfo;

interface

uses
  Windows, Messages, SysUtils, Classes, Forms, WinSvc, SvcMgr,
  System.Win.Registry;

function ServiceGetStatus(sMachine, sService: string): DWord; // 取服务状态
function ServiceUninstalled(sMachine, sService: string): boolean; // 反安装服务
function ServiceRunning(sMachine, sService: string): boolean; // 判断服务是否运行
function ServiceStopped(sMachine, sService: string): boolean; // 停止服务
function StartService(ServiceName: string; NumberOfArgument: DWord;
  ServiceArgVectors: PChar): boolean; overload; // 服务启动  StartService(‘‘,0,nil)
function StartServicea(AServName: string): boolean; // 开始服务
function StopService(ServiceName: string): boolean; // 暂停服务
function ShutdownService(ServiceName: string): boolean; // 御载服务
function IsServiceInstalled(ServiceName: string): boolean; // 服务是否安装
function InstallService(ServiceName, DisplayName, Filename: string;
  ServiceDescription: string = ‘‘): boolean; // 安装服务
procedure ServiceUpdateDescription(const ServiceName, Description: string);
// 添加服务描述
function DoControlService(ServiceName: string; ControlFalg: Cardinal): boolean;
// 反安装服务
function UnInstallService(ServiceName: string): boolean;

implementation

function UnInstallService(ServiceName: string): boolean;
var
  SCManager, ServiceHandle: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = ‘‘) then
    Exit;
  SCManager := OpenSCManager(nil, nil, GENERIC_WRITE);
  if SCManager = 0 then
    Exit;
  try
    ServiceHandle := OpenService(SCManager, PChar(ServiceName), _DELETE);
    Result := DeleteService(ServiceHandle);
    CloseServiceHandle(ServiceHandle);
  finally
    CloseServiceHandle(SCManager);
  end;
end;

function DoControlService(ServiceName: string; ControlFalg: Cardinal): boolean;
var
  ServiceStatus: TServiceStatus;
  SCManager, hService: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = ‘‘) then
    Exit;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCManager  0 then
  begin
    hService := OpenService(SCManager, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if hService  0 then
    begin
      Result := ControlService(hService, ControlFalg, ServiceStatus);
      CloseServiceHandle(hService);
    end;
    CloseServiceHandle(SCManager);
  end;
end;

procedure ServiceUpdateDescription(const ServiceName, Description: string);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    with reg do
    begin
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey(‘SYSTEM\CurrentControlSet\Services\‘ + ServiceName, False) then
      begin
        WriteString(‘Description‘, Description);
      end;
      CloseKey;
    end;
  finally
    reg.Free;
  end;
end;

function InstallService(ServiceName, DisplayName, Filename: string;
  ServiceDescription: string = ‘‘): boolean;
var
  SCManager, ServiceHandle: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = ‘‘) and not FileExists(Filename) then
    Exit;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCManager = 0 then
    Exit;
  try
    ServiceHandle := CreateService(SCManager, PChar(ServiceName),
      PChar(DisplayName), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS or
      SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
      PChar(Filename), nil, nil, nil, nil, nil);
    if IsServiceInstalled(ServiceName) and (ServiceDescription  ‘‘) then
      ServiceUpdateDescription(ServiceName, ServiceDescription);
    CloseServiceHandle(ServiceHandle);
    Result := ServiceHandle  0;
  finally
    CloseServiceHandle(SCManager);
  end;
end;

function IsServiceInstalled(ServiceName: string): boolean;
var
  Mgr, Svc: Integer;
begin
  Result := False;
  if (Trim(ServiceName) = ‘‘) then
    Exit;
  Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if Mgr  0 then
  begin
    Svc := OpenService(Mgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    Result := Svc  0;
    if Result then
      CloseServiceHandle(Svc);
    CloseServiceHandle(Mgr);
  end;
end;

function ShutdownService(ServiceName: string): boolean;
begin
  Result := DoControlService(ServiceName, SERVICE_CONTROL_SHUTDOWN);
end;

function StopService(ServiceName: string): boolean;
begin
  Result := DoControlService(ServiceName, SERVICE_CONTROL_STOP);
end;

function ServiceGetStatus(sMachine, sService: string): DWord;
var
  schm, schs: THandle;
  ss: TServiceStatus;
  dwStat: DWord;
begin
  dwStat := 0;
  schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
  if (schm > 0) then
  begin
    schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS);
    if (schs > 0) then
    begin
      if (QueryServiceStatus(schs, ss)) then
        dwStat := ss.dwCurrentState;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := dwStat;
end;

{ 判断某服务是否安装,未安装返回true,已安装返回false }

function ServiceUninstalled(sMachine, sService: string): boolean;
begin
  Result := 0 = ServiceGetStatus(sMachine, sService);
end;

{ 判断某服务是否启动,启动返回true,未启动返回false }

function ServiceRunning(sMachine, sService: string): boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService);
end;

{ 判断某服务是否停止,停止返回true,未停止返回false }

function ServiceStopped(sMachine, sService: string): boolean;
begin
  Result := SERVICE_STOPPED = ServiceGetStatus(sMachine, sService);
end;

function StartService(ServiceName: string; NumberOfArgument: DWord;
  ServiceArgVectors: PChar): boolean; overload; // More complex start
var
  SCManager, hService: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = ‘‘) then
    Exit;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  Result := SCManager  0;
  if Result then
    try
      hService := OpenService(SCManager, PChar(ServiceName),
        SERVICE_ALL_ACCESS);
      Result := hService  0;
      if (hService  0) then
        try
          Result := WinSvc.StartService(hService, NumberOfArgument,
            PChar(ServiceArgVectors));
          if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then
            Result := True;
        finally
          CloseServiceHandle(hService);
        end;
    finally
      CloseServiceHandle(SCManager);
    end;
end;

function StartServicea(AServName: string): boolean; // use WinSvc
var
  SCManager, hService: SC_HANDLE;
  lpServiceArgVectors: PChar;
begin
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  Result := SCManager  0;
  if Result then
    try
      hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);
      Result := hService  0;
      if (hService = 0) and (GetLastError = ERROR_SERVICE_DOES_NOT_EXIST) then
        Exception.Create(‘指定的服务不存在!‘);
      if hService  0 then
        try
          lpServiceArgVectors := nil;
          Result := WinSvc.StartService(hService, 0,
            PChar(lpServiceArgVectors));
          if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then
            Result := True;
        finally
          CloseServiceHandle(hService);
        end;
    finally
      CloseServiceHandle(SCManager);
    end;
end;

end.

  

windows服务

标签:except   star   use   process   word   返回   char   end   overload   

原文地址:https://www.cnblogs.com/hnxxcxg/p/14414437.html


评论


亲,登录后才可以留言!