工作中可能需要多次启动、停止某项服务(程序),若每次都(计算机管理--->服务和应用程序--->服务)手动操作,未免有点繁琐...       
 
{**************************************************************************************}
{ 单元作者:Simon.Hu/ADelphiCoder                                                       }
{ 创建日期:2009/10/12                                                                          }
{ 参考说明:经修改部分网上源码而成                                                      }
{ 测试环境:Delphi 7 SP1 + WinXP SP3                                                  }
{ 注意事项:程序中所指的"服务名"是指某项服务的"服务名称",非其"描述" }
{**************************************************************************************}
unit unt_SvcCtrl;
   
interface
uses
  SysUtils, Windows, Messages, Winsvc, Dialogs;
   
function MyStartService(const SvcName: string): Boolean;
function MyStopService(const SvcName: string): Boolean;
function MyQueryServiceState(const SvcName: string): string;
function MyCreateService(const SvcName, FilePath: String): Boolean;
function MyDeleteService(const SvcName: string): Boolean;
implementation
{ 开启服务 }
function MyStartService(const SvcName: string): Boolean;
var
  SH1, SH2 : SC_HANDLE;
  PC       : PChar;
begin
  Result := False;
  SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SH1 <= 0 then
    Exit;
  SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
  if SH2 <= 0 then
    Exit;
  try
    Result := StartService(SH2, 0, PC);
   
    CloseServiceHandle(SH2);
    CloseServiceHandle(SH1);
  except
    CloseServiceHandle(SH2);
    CloseServiceHandle(SH1);
   
    Exit;
  end;
end;
{ 停止服务 }
function MyStopService(const SvcName: string): Boolean;
var
  SH1, SH2 : SC_HANDLE;
  SvcState : TServiceStatus;
begin
  Result := False;
   
  SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SH1 <= 0 then
    Exit;
  SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
  if SH2 <= 0 then
    Exit;
  try
    Result := ControlService(SH2, SERVICE_CONTROL_STOP, SvcState);
   
    CloseServiceHandle(SH1);
    CloseServiceHandle(SH2);
  except
    CloseServiceHandle(SH1);
    CloseServiceHandle(SH2);
   
    Exit;
  end;
end;
{ 查询当前服务的状态 }
function MyQueryServiceState(const SvcName: string): string;
var
  SH1, SH2 : SC_HANDLE;
  SvcState : TServiceStatus;
begin  
  Result := '未安装';
   
  SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SH1 <= 0 then
    Exit;
  SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
  if SH2 <= 0 then
    Exit;
  try
    QueryServiceStatus(SH2, SvcState);
   
    if SvcState.dwCurrentState = SERVICE_RUNNING then
      Result := '启动'       //Run
    else if SvcState.dwCurrentState = SERVICE_RUNNING then
      Result := 'Wait'       //Runing
    else if SvcState.dwCurrentState = SERVICE_START_PENDING then
      Result := 'Wait'       //Pause
    else if SvcState.dwCurrentState = SERVICE_STOP_PENDING then
      Result := '停止'       //Pause
    else if SvcState.dwCurrentState = SERVICE_PAUSED then
      Result := '暂停'       //Pause
    else if SvcState.dwCurrentState = SERVICE_STOPPED then
      Result := '停止'       //Stop
    else if SvcState.dwCurrentState = SERVICE_CONTINUE_PENDING then
      Result := 'Wait'       //Pause
    else if SvcState.dwCurrentState = SERVICE_PAUSE_PENDING then
      Result := 'Wait';      //Pause
    CloseServiceHandle(SH1);
    CloseServiceHandle(SH2);
  except
    CloseServiceHandle(SH1);
    CloseServiceHandle(SH2);
   
    Exit;
  end;
end;
{ 建立服务 }
function MyCreateService(const SvcName, FilePath: string): Boolean;
var  
  SH1, SH2 : SC_HANDLE;
begin
  Result := False;
  if FilePath = '' then
    Exit;
  SH1 := OpenSCManager(nil, nil, SC_MANAGER_CREATE_SERVICE);
  if SH1 <= 0 then
    Exit;
  try
    SH2 := CreateService(
                         SH1,
                         PChar(SvcName),
                         PChar(SvcName),
                         SERVICE_ALL_ACCESS,
                         SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS,
                         SERVICE_AUTO_START,
                         SERVICE_ERROR_NORMAL,
                         PChar(FilePath),
                         nil, nil, nil, nil, nil
                        );
    if SH2 <= 0 then
    begin
      ShowMessage(SysErrorMessage(GetlastError));
      Exit;
    end;
   
    CloseServiceHandle(SH1);
    CloseServiceHandle(SH2);
    Result := True;
  except
    CloseServiceHandle(SH1);
    CloseServiceHandle(SH2);
   
    Exit;
  end;
end;
{ 卸载服务 }
function MyDeleteService(const SvcName: string): Boolean;
var
  SH1, SH2 : SC_HANDLE;
begin
  Result := False;
  SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SH1 <= 0 then
    Exit;
  SH2 := OpenService(SH1, PChar(SvcName), STANDARD_RIGHTS_REQUIRED);
  if SH2 <= 0 then
    Exit;
  try
    Result := DeleteService(SH2);
    if not Result then
      ShowMessage(SysErrorMessage(GetlastError));
    CloseServiceHandle(SH2);
    CloseServiceHandle(SH1);
  except
    CloseServiceHandle(SH2);
    CloseServiceHandle(SH1);
   
    Exit;
  end;
end;
end.