打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
delphi编程启动服务停止服务新建服务的方法

本文主要讲解了如何使用delphi新建服务,停止系统服务,以及获取服务状态和新建系统服务器的方法,以下是关键代码

unit Servicescontrol;
   
interface  
  uses Windows,Messages,SysUtils,Winsvc,Dialogs;
   
  function  StartServices(Const  SvrName:String):Boolean;
  function  StopServices(Const  SvrName:String):Boolean;
  function  QueryServiceStatu(Const SvrName:   String):String;
  function  CreateServices(Const SvrName,FilePath:String):Boolean;  
  function  DeleteServices(Const SvrName: String):Boolean;  
   
  implementation  

   
  //开启服务  
  function StartServices(Const   SvrName:   String):   Boolean;
  var  
      a,b:SC_HANDLE;  
        c:PChar;  
  begin  
        Result:=False;  
   
        a:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);  
   
        if a <=0 then  Exit;  
   
        b:=OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);  
   
        if b <=0  then  Exit;  
            try  
              Result:=StartService(b,0,c);  
              CloseServiceHandle(b);  
              CloseServiceHandle(a);  
        except  
              CloseServiceHandle(b);  
              CloseServiceHandle(a);  
              Exit;  
        end;  
  end;  
   
   
  //停止服务  
  function   StopServices(Const   SvrName:   String):   Boolean;
  var
        a,b:   SC_HANDLE;  
        d:   TServiceStatus;  
  begin  
        Result := False;  
   
        a :=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);  
   
        if a <=0 then Exit;  
   
        b:=OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);  
   
        if b <=0  then  Exit;  
            try  
              Result:=ControlService(b,SERVICE_CONTROL_STOP,d);  
               CloseServiceHandle(a);  
              CloseServiceHandle(b);  
        except  
              CloseServiceHandle(a);  
              CloseServiceHandle(b);  
              Exit;  
        end;  
  end;  
   
   
  //查询当前服务的状态  
  function  QueryServiceStatu(Const   SvrName:   String):   String;  
  var  
        a,b:   SC_HANDLE;  
        d:   TServiceStatus;  
  begin  
        Result := '未安装';  
   
        a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);  
   
        if a <=0 then  Exit;  
   
        b := OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);  
   
        if  b  <= 0  then  Exit;  
            try  
              QueryServiceStatus(b,d);  
                     if   d.dwCurrentState     =   SERVICE_RUNNING   then            
                    Result   :=   '启动'       //Run  
              else   if   d.dwCurrentState     =   SERVICE_RUNNING   then  
                    Result   :=   'Wait'       //Runing  
              else   if   d.dwCurrentState     =   SERVICE_START_PENDING then  
                    Result   :=   'Wait'       //Pause  
              else   if   d.dwCurrentState     =   SERVICE_STOP_PENDING     then  
                    Result   :=   '停止'       //Pause  
              else   if   d.dwCurrentState     =   SERVICE_PAUSED   then  
                    Result   :=   '暂停'       //Pause  
              else   if   d.dwCurrentState     =   SERVICE_STOPPED   then  
                    Result   :=   '停止'     //Stop  
              else   if   d.dwCurrentState     =   SERVICE_CONTINUE_PENDING     then  
                    Result   :=   'Wait'       //Pause  
              else   if   d.dwCurrentState     =   SERVICE_PAUSE_PENDING   then  
                    Result   :=   'Wait';       //Pause  
   
              CloseServiceHandle(a);  
              CloseServiceHandle(b);  
        except  
              CloseServiceHandle(a);  
              CloseServiceHandle(b);  
              Exit;  
        end;  
  end;  
   
   
  {建立服务}  
  function  CreateServices(Const SvrName,FilePath:   String):   Boolean;  
  var  
        a,b:SC_HANDLE;  
  begin  
        Result:=False;  
            if  FilePath   =''   then   Exit;  
   
        a   :=   OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);  
   
        if   a   <=   0   then   Exit;  
        try  
              b   :=   CreateService(a,PChar(SvrName),  
               PChar(SvrName),  
               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   b   <=   0   then   begin  
                    ShowMessage(   SysErrorMessage(   GetlastError   ));  
                    Exit;  
              end;  
                  CloseServiceHandle(a);  
              CloseServiceHandle(b);  
               
              Result   :=   True;  
        except  
              CloseServiceHandle(a);  
              CloseServiceHandle(b);  
              Exit;  
        end;  
  end;  
   
   
  {卸载服务}  
  function   DeleteServices(Const   SvrName:   String):   Boolean;  
  var  
        a,b:SC_HANDLE;  
  begin  
        Result:=False;  
            a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);  
            if a <= 0 then  Exit;  
            b :=OpenService(a,PChar(SvrName),STANDARD_RIGHTS_REQUIRED);  
            if b <= 0 then Exit;  
            try  
              Result := DeleteService(b);  
   
              if not Result then  
                    ShowMessage(SysErrorMessage(GetlastError));  
               CloseServiceHandle(b);  
              CloseServiceHandle(a);  
            except  
              CloseServiceHandle(b);  
              CloseServiceHandle(a);  
              Exit;  
        end;  
  end;  
  end.

调用方法:

   {启动服务}
       StartServices(服务名);
   {停止服务}
       StopServices(服务名);
   {新建服务}
       CreateServices(服务名,exe文件路径);
   {删除服务}
         DeleteServices(服务名);
   {获取服务状态}
         string:=QueryServiceStatu(服务名);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在Delphi程序中操作注册表
Delphi6函数大全
Delphi新语法和ifthen的扩展联想. | 武稀松(wr960204)的博客
《Mastering Delphi 6》学习笔记之六
Delphi获取文件夹路径的三种方式
大富翁论坛-富翁笔记-Delphi中的属性编辑器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服