zoukankan      html  css  js  c++  java
  • Delphi 服务操作

    [delphi] view plaincopyprint?
     
    1. unit Service;  
    2.       
    3. interface     
    4.   uses Windows,Messages,SysUtils,Winsvc,Dialogs;  
    5.       
    6.   function  StartServices(Const  SvrName:String):Boolean;  
    7.   function  StopServices(Const  SvrName:String):Boolean;  
    8.   function  QueryServiceStatu(Const SvrName:   String):String;  
    9.   function  CreateServices(Const SvrName,FilePath:String):Boolean;     
    10.   function  DeleteServices(Const SvrName: String):Boolean;     
    11.       
    12.   implementation    
    13.       
    14.   //开启服务     
    15.   function StartServices(Const   SvrName:   String):   Boolean;  
    16.   var     
    17.       a,b:SC_HANDLE;     
    18.         c:PChar;     
    19.   begin     
    20.         Result:=False;     
    21.       
    22.         a:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);     
    23.       
    24.         if a <=then  Exit;     
    25.       
    26.         b:=OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);     
    27.       
    28.         if b <=0  then  Exit;     
    29.             try     
    30.               Result:=StartService(b,0,c);     
    31.               CloseServiceHandle(b);     
    32.               CloseServiceHandle(a);     
    33.         except     
    34.               CloseServiceHandle(b);     
    35.               CloseServiceHandle(a);     
    36.               Exit;     
    37.         end;     
    38.   end;     
    39.       
    40.       
    41.   //停止服务     
    42.   function   StopServices(Const   SvrName:   String):   Boolean;  
    43.   var  
    44.         a,b:   SC_HANDLE;     
    45.         d:   TServiceStatus;     
    46.   begin     
    47.         Result := False;     
    48.       
    49.         a :=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);     
    50.       
    51.         if a <=then Exit;     
    52.       
    53.         b:=OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);     
    54.       
    55.         if b <=0  then  Exit;     
    56.             try     
    57.               Result:=ControlService(b,SERVICE_CONTROL_STOP,d);     
    58.                CloseServiceHandle(a);     
    59.               CloseServiceHandle(b);     
    60.         except     
    61.               CloseServiceHandle(a);     
    62.               CloseServiceHandle(b);     
    63.               Exit;     
    64.         end;     
    65.   end;     
    66.       
    67.       
    68.   //查询当前服务的状态     
    69.   function  QueryServiceStatu(Const   SvrName:   String):   String;     
    70.   var     
    71.         a,b:   SC_HANDLE;     
    72.         d:   TServiceStatus;     
    73.   begin     
    74.         Result := '未安装';     
    75.       
    76.         a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);     
    77.       
    78.         if a <=then  Exit;     
    79.       
    80.         b := OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);     
    81.       
    82.         if  b  <= 0  then  Exit;     
    83.             try     
    84.               QueryServiceStatus(b,d);     
    85.                      if   d.dwCurrentState     =   SERVICE_RUNNING   then               
    86.                     Result   :=   '启动'       //Run     
    87.               else   if   d.dwCurrentState     =   SERVICE_RUNNING   then     
    88.                     Result   :=   'Wait'       //Runing     
    89.               else   if   d.dwCurrentState     =   SERVICE_START_PENDING then     
    90.                     Result   :=   'Wait'       //Pause     
    91.               else   if   d.dwCurrentState     =   SERVICE_STOP_PENDING     then     
    92.                     Result   :=   '停止'       //Pause     
    93.               else   if   d.dwCurrentState     =   SERVICE_PAUSED   then     
    94.                     Result   :=   '暂停'       //Pause     
    95.               else   if   d.dwCurrentState     =   SERVICE_STOPPED   then     
    96.                     Result   :=   '停止'     //Stop     
    97.               else   if   d.dwCurrentState     =   SERVICE_CONTINUE_PENDING     then     
    98.                     Result   :=   'Wait'       //Pause     
    99.               else   if   d.dwCurrentState     =   SERVICE_PAUSE_PENDING   then     
    100.                     Result   :=   'Wait';       //Pause     
    101.       
    102.               CloseServiceHandle(a);     
    103.               CloseServiceHandle(b);     
    104.         except     
    105.               CloseServiceHandle(a);     
    106.               CloseServiceHandle(b);     
    107.               Exit;     
    108.         end;     
    109.   end;     
    110.       
    111.       
    112.   {建立服务}     
    113.   function  CreateServices(Const SvrName,FilePath:   String):   Boolean;     
    114.   var     
    115.         a,b:SC_HANDLE;     
    116.   begin     
    117.         Result:=False;     
    118.             if  FilePath   =''   then   Exit;     
    119.       
    120.         a   :=   OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);     
    121.       
    122.         if   a   <=   0   then   Exit;     
    123.         try     
    124.               b   :=   CreateService(a,PChar(SvrName),     
    125.                PChar(SvrName),     
    126.                SERVICE_ALL_ACCESS,     
    127.                SERVICE_INTERACTIVE_PROCESS   or   SERVICE_WIN32_OWN_PROCESS,     
    128.                SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,     
    129.                PChar(FilePath),nil,nil,nil,nil,nil);     
    130.                   if   b   <=   0   then   begin     
    131.                     ShowMessage(   SysErrorMessage(   GetlastError   ));     
    132.                     Exit;     
    133.               end;     
    134.                   CloseServiceHandle(a);     
    135.               CloseServiceHandle(b);     
    136.                   
    137.               Result   :=   True;     
    138.         except     
    139.               CloseServiceHandle(a);     
    140.               CloseServiceHandle(b);     
    141.               Exit;     
    142.         end;     
    143.   end;     
    144.       
    145.       
    146.   {卸载服务}     
    147.   function   DeleteServices(Const   SvrName:   String):   Boolean;     
    148.   var     
    149.         a,b:SC_HANDLE;     
    150.   begin     
    151.         Result:=False;     
    152.             a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);     
    153.             if a <= then  Exit;     
    154.             b :=OpenService(a,PChar(SvrName),STANDARD_RIGHTS_REQUIRED);     
    155.             if b <= then Exit;     
    156.             try     
    157.               Result := DeleteService(b);     
    158.       
    159.               if not Result then     
    160.                     ShowMessage(SysErrorMessage(GetlastError));     
    161.                CloseServiceHandle(b);     
    162.               CloseServiceHandle(a);     
    163.             except     
    164.               CloseServiceHandle(b);     
    165.               CloseServiceHandle(a);     
    166.               Exit;     
    167.         end;     
    168.   end;     
    169.   end.  
    170. 调用方法:  
    171.    {启动服务}  
    172.        StartServices(服务名);  
    173.    {停止服务}  
    174.        StopServices(服务名);  
    175.    {新建服务}  
    176.        CreateServices(服务名,exe文件路径);  
    177.    {删除服务}  
    178.          DeleteServices(服务名);  
    179.    {获取服务状态}  
    180.          string:=QueryServiceStatu(服务名);  

    http://blog.csdn.net/yanjiaye520/article/details/7840911

  • 相关阅读:
    [原创]测试计划与测试方案区别
    [原创]什么是构建验证测试(BVT)
    [原创]什么是测试驱动开发?
    [原创]用TestDirector的测试管理的流程
    [原创]测试用例设计之"正面测试与和负面测试"
    [原创]测试用例设计之“因果图”法
    [原创]测试用例设计之“状态迁移图”法
    [转贴]测试工具自动化的最佳实践
    [原创]软件测试管理之“测试角色和职能”概述
    软件测试试题
  • 原文地址:https://www.cnblogs.com/findumars/p/4999279.html
Copyright © 2011-2022 走看看