CSDN博客

img linmaochen

windwos 2000及NT 服务注册的具体实现

发表于2003/4/1 9:03:00  630人阅读

windwos 2000及NT 服务注册的具体实现

{**************************************************************}

{*******************************************************}
{*  程序编写: 陈林茂
{*  编写日期: 2003-01-05
{*  联系作者: linmaochen@sohu.com
{*******************************************************}

interface
uses
   Windows,Classes,SysUtils,Winsvc,nb30,winsock;

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 CreateService():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;

implementation

 

{ TService }

constructor TService.Create;
begin
  try
    ScmHandle :=0;
    SetScmInfo();
    ScmHandle:=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
  Except
  End; 


end;

{建立一服务}
function TService.CreateService(): Boolean;
Var
  i :Integer;
begin
  Try
    OpenScmHandle();
    svc_info.HscManager := ScmHandle;
    Result :=False;
    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(Svc_Info.BinaryPathName),
                    pchar(''),LPDWORD(0),Pchar(Svc_Info.Dependencies),Pchar(Svc_info.ServerStartName),Pchar(Svc_Info.Password));

   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.isSvcExists(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

        Result :=true;
        {关闭句柄}
        CloseServiceHandle(RHandle);
      End;
    End;
  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.Open_Service(ServiceName: String): Boolean;
Var
  SHandle :Integer;
begin
  Try
    Result :=False;
    sHandle := 0;
    SHandle :=OpenScmHandle();
    If SHandle<>0 Then
    Begin
      SvcHandle :=OpenService(SHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
      If SvcHandle <>0 Then Result :=True;
    End;
  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;

//控制WINDOWS 系统内部的服务

function TService.Control_Service(ServiceName: String;controlType : Integer): Boolean;
Var
  RHandle:Integer;
  rc :Boolean;
  Rec_status :TServiceStatus;
  serviceArg :Pchar;
  i  :Integer;

begin
  i :=0;
  serviceArg := Pchar('');
  Try
    Result :=False;
    If ScmHandle <>0 Then
    Begin
      RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
      If RHandle <>0 Then
      Begin
        Case controlType of
          1:  //启动服务器
            Begin
              //ControlService(rHandle,SERVICE_CONTROL_START,rec_status);
              StartService(rHandle,0,servicearg);
              i:=0;
              While i<30 Do
              Begin
                QueryServiceStatus(RHandle,Rec_status);
                if rec_status.dwCurrentState =SERVICE_RUNNING then
                Begin
                  result :=True;
                  CloseServiceHandle(RHandle);
                  Break;
                End;
                sleep(1000);
                i:=i+1;
              End;
            End;
          2:     //暂停服务进程
            Begin
              ControlService(rHandle,SERVICE_CONTROL_PAUSE,rec_status);
              i:=0;
              While i<30 Do
              Begin
                 QueryServiceStatus(RHandle,Rec_status);
                 if rec_status.dwCurrentState =SERVICE_PAUSED then
                 Begin
                   result :=true;
                   CloseServiceHandle(RHandle);
                   Break;
                 End;
                 Sleep(1000);
                 i:=i+1;
              End;
            End;
          3:    //唤醒服务进程
            Begin
              ControlService(rHandle,SERVICE_CONTROL_CONTINUE,rec_status);
              i:=0;
              While i<30 do
              Begin
                QueryServiceStatus(RHandle,Rec_status);
                if rec_status.dwCurrentState =SERVICE_RUNNING then
                Begin
                  result :=True;
                  CloseServiceHandle(RHandle);
                  Break;
                End;
                Sleep(1000);
                i:=i+1;
              End;
            End;
          4:   //停止服务进程
            begin
              ControlService(rHandle,SERVICE_CONTROL_STOP,rec_status);
              i:=0;
              While i<30 Do
              Begin
                QueryServiceStatus(RHandle,Rec_status);
                if rec_status.dwCurrentState =SERVICE_STOPPED then
                Begin
                  result :=True;
                  CloseServiceHandle(RHandle);
                  Break;
                End;
                Sleep(1000);
                i:=i+1;
              End;
            End;
          5:   //关闭服务程序 SERVICE_CONTROL_SHUTDOWN
            Begin
              rec_status.dwCurrentState :=SERVICE_RUNNING;

              ControlService(rHandle,SERVICE_CONTROL_STOP,rec_status);
              i:=0;
              While i<30 Do
              Begin
                QueryServiceStatus(RHandle,Rec_status);
                if rec_status.dwCurrentState =SERVICE_STOPPED then
                Begin
                  result :=True;
                  CloseServiceHandle(RHandle);
                  Break;
                End;
                Sleep(1000);
                i:=i+1;
              End;
            End;
        end;
        Result :=true;
        {关闭句柄}
        if rHandle <> 0  then
          CloseServiceHandle(RHandle);
      End;
    End;
  Except
  End;

end;

function TService.IsServiceStart(ServiceName: String): Boolean;
Var
  RHandle:Integer;
  rc :Boolean;
  Rec_status :TServiceStatus;
begin
  Try
    OpenScmHandle();
    Result :=False;
    If ScmHandle<>0 Then
    Begin
      RHandle := 0;
      RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
      If RHandle <>0 Then
      Begin
        QueryServiceStatus(RHandle,Rec_status);
        if rec_status.dwCurrentState =SERVICE_RUNNING then
           result :=True;

        {关闭句柄}
        CloseServiceHandle(RHandle);
      End;
    End;
  Except
  End;

end;

end.

0 0

相关博文

我的热门文章

img
取 消
img