unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Winsvc,nb30,winsock, StdCtrls, ExtCtrls;  Type {服务句柄信息}
 TScmInfo=Record
   HostName :String;
   DataBaseName:String;
   DesireAccess :DWORD;
 End; {服务信息}
 TSvcInfo=Record
   HscManager :Integer;
   ServerName :String; //服务名
   DisplayName :String; //服务的显示名
   DesireAccess :DWORD; //
   ServiceType :Dword; //服务的类别
   StartType :Dword; //服务的启动方式
   ErrorControl :Dword; //服务错误控制方式
   BinaryPathName :String; //服务执行文件的具体路径
   LoadOrderGroup :String;
   TagId :Dword;
   Dependencies :String;  //依赖:有多个依赖的服务时,中间以空格隔开}
   ServerStartName :String;
   Password :String;
 End;
 //需要注册的服务信息数组 SvcArray=Array[1..20] of TSvcInfo; Type  {服务控制类}
  TService=Class(TObject)
  Private
    Scm_Info  :TScmInfo;
    Svc_Info  :TSvcInfo;
    ScmHandle :Integer;    {服务句柄}
    SvcHandle :Integer;
  Public
    Constructor Create();
    Destructor Destroy();Override;
    Function SetScmInfo():Boolean;
    Function OpenScmHandle():Integer;
    Function SetServiceInfo(TmpSvc :TsvcInfo):Boolean;
    Function Create_Service():Boolean;    {删除一服务}
    Function Delete_AService(ServiceName :String):Boolean;    {建立一服务}
    Function Open_Service(ServiceName :String):Boolean;{/////////////////////////////////////////////////////////////////////}
{//  函数名称: IsSvcExists()
{//  函数功能: 判断系统中相关的服务是否存在
{//  参数信息: 需要判断的服务名
{//  返回值:   TRUE : WINDOWS 中的服务已存在,FALSE :WINDOWS 中的服务不存在
{//
{/////////////////////////////////////////////////////////////////////}
    Function isSvcExists(ServiceName :string):Boolean;{/////////////////////////////////////////////////////////////////////////}
{//  函数名称: Start_Service()
{//  函数功能: 启动相关的服务进程
{//  参数信息: 需要启动的服务名
{//  返回值:   TRUE :服务已经启动;FALSE:服务启动失败;
{/////////////////////////////////////////////////////////////////////////}
    Function Control_Service(ServiceName : String; controlType :integer):Boolean;{/////////////////////////////////////////////////////////////////////////}
{//  函数名称: IsServiceStart()
{//  函数功能: 判断服务是否启动
{//  参数信息:  需要判断的服务名
{//  返回值:   TRUE :服务已经启动; FALSE :服务已停止
{/////////////////////////////////////////////////////////////////////////}
    Function IsServiceStart(ServiceName:String):Boolean;  End;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    RadioGroup1: TRadioGroup;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}constructor TService.Create;
begin
 try
   ScmHandle :=0;
   SetScmInfo();
   ScmHandle:=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
 Except
 End;
end;{建立一服务}
function TService.Create_Service(): Boolean;
Var
 i :Integer;
begin
 Try
   OpenScmHandle();
   svc_info.HscManager := ScmHandle;
   Result :=False;   Svc_Info.ServerName :='Test';//服务名
   Svc_Info.DisplayName:='Jwxx';//显示名字
   Svc_info.DesireAccess:=SERVICE_ALL_ACCESS;//访问权限
   svc_INfo.ServiceType:=1;//1:自动启动,2:手动启动;3:禁用
   Svc_Info.ErrorControl:=SERVICE_ERROR_IGNORE;   //下面这个地方老出错误,到底应该怎么写啊
   Svc_Info.BinaryPathName:='d\\jwxx.exe';
   Svc_Info.Dependencies:='NULL';
   Svc_info.ServerStartName:='DY\administrator';
   
   Svc_Info.Password:='NULL';
  SvcHandle :=CreateService(svc_Info.HscManager ,Pchar(svc_Info.ServerName),
  Pchar(Svc_Info.DisplayName),Svc_info.DesireAccess ,
  svc_INfo.ServiceType ,Svc_Info.StartType ,Svc_Info.ErrorControl,
  pchar('d:\jwxx.exe'),
  pchar(''),LPDWORD(0),Pchar(Svc_Info.Dependencies),Pchar (Svc_info.ServerStartName),
  Pchar(Svc_Info.Password)); SvcHandle :=0;
  If SvcHandle <> 0 Then
   Begin
     Result :=True;
     CloseServiceHandle(SvcHandle);
   End ;
   I:=i+1;
 Except
 End;end;
{注销一服务}
function TService.Delete_AService(ServiceName: String): Boolean;
Var
 RHandle:Integer;
 rc :Boolean;
begin
 Try
   OpenScmHandle();
   Result :=False;
   If ScmHandle<>0 Then
   Begin
     RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
     If RHandle <>0 Then
     Begin       Rc :=DeleteService(Rhandle);
       Result :=Rc;
       {关闭句柄}
       CloseServiceHandle(RHandle);
     End Else Result :=True;
   End;
 Except
 End;
end;destructor TService.Destroy;
begin
 try
   CloseServiceHandle(ScmHandle);
 Except
 End;end;
function TService.OpenScmHandle: Integer;
begin
 try
   SetScmInfo();
   if  ScmHandle <>0 Then CloseServiceHandle(ScmHandle);
   ScmHandle := 0;
   ScmHandle :=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
   if ScmHandle = 0 then
     ScmHandle :=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
 Except
 End;
end;
function TService.SetScmInfo(): Boolean;
Var
 hostName : array [0..255] of char;
 wsdata : TWSAData;
begin
 try
   WSAStartup ($0101, wsdata);
   Result :=False;
   gethostname (hostName,sizeof(hostName));   If HostName ='' Then
     Exit;   scm_Info.HostName :=HostName;
   Scm_Info.DesireAccess :=SC_MANAGER_ALL_ACCESS;
 Except
 End;end;//设置需要注册的WINDOWS 服务的具体信息function TService.SetServiceInfo(TmpSvc: TsvcInfo): Boolean;
begin
 try
   Result :=False;
   If ScmHandle >0 Then
   Begin
     Svc_Info :=TmpSvc;
     Svc_Info.HscManager :=ScmHandle;
     Result :=True;
   End;
 Except
 End;end;procedure TForm1.Button4Click(Sender: TObject);
Var
tt:tservice;
begin    tt.Create;
    tt.Create_Service();
    tt.Free;end;end.
end.