杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2407|回复: 1

检查、安装、启动、停止以及删除基于 NT 的服务的脚本

[复制链接]
发表于 2017-3-6 16:17:49 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. ;  
  2. ;Inno Setup Extensions Knowledge Base
  3. ;Article 31 - Functions to Start, Stop, Install, Remove a NT Service
  4. ;http://www13.brinkster.com/vincenzog/isxart.asp?idart=31
  5. ;Author: Silvio Iaccarino
  6. ;Compiler: ISX 3.0.4
  7. ;

  8. [Setup]
  9. AppName=test
  10. AppVerName=test

  11. CreateAppDir=false
  12. UsePreviousAppDir=false
  13. UsePreviousGroup=false
  14. AlwaysShowComponentsList=false
  15. ShowComponentSizes=false
  16. FlatComponentsList=false
  17. UsePreviousSetupType=false
  18. UsePreviousTasks=false
  19. UsePreviousUserInfo=false
  20. DisableStartupPrompt=true

  21. [_ISTool]
  22. EnableISX=true

  23. [Code]
  24. type
  25.      SERVICE_STATUS = record
  26.          dwServiceType    : cardinal;
  27.          dwCurrentState    : cardinal;
  28.          dwControlsAccepted       : cardinal;
  29.          dwWin32ExitCode    : cardinal;
  30.          dwServiceSpecificExitCode      : cardinal;
  31.          dwCheckPoint    : cardinal;
  32.          dwWaitHint         : cardinal;
  33.      end;
  34.      HANDLE = cardinal;

  35. const
  36.      SERVICE_QUERY_CONFIG  = $1;
  37.      SERVICE_CHANGE_CONFIG  = $2;
  38.      SERVICE_QUERY_STATUS  = $4;
  39.      SERVICE_START    = $10;
  40.      SERVICE_STOP    = $20;
  41.      SERVICE_ALL_ACCESS       = $f01ff;
  42.      SC_MANAGER_ALL_ACCESS  = $f003f;
  43.      SERVICE_WIN32_OWN_PROCESS      = $10;
  44.      SERVICE_WIN32_SHARE_PROCESS      = $20;
  45.      SERVICE_WIN32    = $30;
  46.      SERVICE_INTERACTIVE_PROCESS = $100;
  47.      SERVICE_BOOT_START          = $0;
  48.      SERVICE_SYSTEM_START        = $1;
  49.      SERVICE_AUTO_START          = $2;
  50.      SERVICE_DEMAND_START        = $3;
  51.      SERVICE_DISABLED            = $4;
  52.      SERVICE_DELETE              = $10000;
  53.      SERVICE_CONTROL_STOP  = $1;
  54.      SERVICE_CONTROL_PAUSE  = $2;
  55.      SERVICE_CONTROL_CONTINUE      = $3;
  56.      SERVICE_CONTROL_INTERROGATE = $4;
  57.      SERVICE_STOPPED    = $1;
  58.      SERVICE_START_PENDING      = $2;
  59.      SERVICE_STOP_PENDING        = $3;
  60.      SERVICE_RUNNING            = $4;
  61.      SERVICE_CONTINUE_PENDING    = $5;
  62.      SERVICE_PAUSE_PENDING      = $6;
  63.      SERVICE_PAUSED              = $7;

  64. // #######################################################################################
  65. // nt based service utilities
  66. // #######################################################################################
  67. function OpenSCManager(lpMachineName, lpDatabaseName: string; dwDesiredAccess :cardinal): HANDLE;
  68. external 'OpenSCManagerA@advapi32.dll stdcall';

  69. function OpenService(hSCManager :HANDLE;lpServiceName: string; dwDesiredAccess :cardinal): HANDLE;
  70. external 'OpenServiceA@advapi32.dll stdcall';

  71. function CloseServiceHandle(hSCObject :HANDLE): boolean;
  72. external 'CloseServiceHandle@advapi32.dll stdcall';

  73. function CreateService(hSCManager :HANDLE;lpServiceName, lpDisplayName: string;dwDesiredAccess,dwServiceType,dwStartType,dwErrorControl: cardinal;lpBinaryPathName,lpLoadOrderGroup: String; lpdwTagId : cardinal;lpDependencies,lpServiceStartName,lpPassword :string): cardinal;
  74. external 'CreateServiceA@advapi32.dll stdcall';

  75. function DeleteService(hService :HANDLE): boolean;
  76. external 'DeleteService@advapi32.dll stdcall';

  77. function StartNTService(hService :HANDLE;dwNumServiceArgs : cardinal;lpServiceArgVectors : cardinal) : boolean;
  78. external 'StartServiceA@advapi32.dll stdcall';

  79. function ControlService(hService :HANDLE; dwControl :cardinal;var ServiceStatus :SERVICE_STATUS) : boolean;
  80. external 'ControlService@advapi32.dll stdcall';

  81. function QueryServiceStatus(hService :HANDLE;var ServiceStatus :SERVICE_STATUS) : boolean;
  82. external 'QueryServiceStatus@advapi32.dll stdcall';

  83. function QueryServiceStatusEx(hService :HANDLE;ServiceStatus :SERVICE_STATUS) : boolean;
  84. external 'QueryServiceStatus@advapi32.dll stdcall';

  85. function OpenServiceManager() : HANDLE;
  86. begin
  87.      if UsingWinNT() = true then begin
  88.   Result := OpenSCManager('','ServicesActive',SC_MANAGER_ALL_ACCESS);
  89.   if Result = 0 then
  90.        MsgBox('the servicemanager is not available', mbError, MB_OK)
  91.      end
  92.      else begin
  93.        MsgBox('only nt based systems support services', mbError, MB_OK)
  94.        Result := 0;
  95.      end
  96. end;

  97. function IsServiceInstalled(ServiceName: string) : boolean;
  98. var
  99.      hSCM      : HANDLE;
  100.      hService: HANDLE;
  101. begin
  102.      hSCM := OpenServiceManager();
  103.      Result := false;
  104.      if hSCM <> 0 then begin
  105.   hService := OpenService(hSCM,ServiceName,SERVICE_QUERY_CONFIG);
  106.         if hService <> 0 then begin
  107.             Result := true;
  108.             CloseServiceHandle(hService)
  109.   end;
  110.         CloseServiceHandle(hSCM)
  111.      end
  112. end;

  113. function InstallService(FileName, ServiceName, DisplayName, Description : string;ServiceType,StartType :cardinal) : boolean;
  114. var
  115.      hSCM      : HANDLE;
  116.      hService: HANDLE;
  117. begin
  118.      hSCM := OpenServiceManager();
  119.      Result := false;
  120.      if hSCM <> 0 then begin
  121.   hService := CreateService(hSCM,ServiceName,DisplayName,SERVICE_ALL_ACCESS,ServiceType,StartType,0,FileName,'',0,'','','');
  122.   if hService <> 0 then begin
  123.        Result := true;
  124.        // Win2K & WinXP supports aditional description text for services
  125.        if Description<> '' then
  126.     RegWriteStringValue(HKLM,'System\CurrentControlSet\Services\' + ServiceName,'Description',Description);
  127.        CloseServiceHandle(hService)
  128.   end;
  129.         CloseServiceHandle(hSCM)
  130.      end
  131. end;

  132. function RemoveService(ServiceName: string) : boolean;
  133. var
  134.      hSCM      : HANDLE;
  135.      hService: HANDLE;
  136. begin
  137.      hSCM := OpenServiceManager();
  138.      Result := false;
  139.      if hSCM <> 0 then begin
  140.   hService := OpenService(hSCM,ServiceName,SERVICE_DELETE);
  141.         if hService <> 0 then begin
  142.             Result := DeleteService(hService);
  143.             CloseServiceHandle(hService)
  144.   end;
  145.         CloseServiceHandle(hSCM)
  146.      end
  147. end;

  148. function StartService(ServiceName: string) : boolean;
  149. var
  150.      hSCM      : HANDLE;
  151.      hService: HANDLE;
  152. begin
  153.      hSCM := OpenServiceManager();
  154.      Result := false;
  155.      if hSCM <> 0 then begin
  156.   hService := OpenService(hSCM,ServiceName,SERVICE_START);
  157.         if hService <> 0 then begin
  158.              Result := StartNTService(hService,0,0);
  159.             CloseServiceHandle(hService)
  160.   end;
  161.         CloseServiceHandle(hSCM)
  162.      end;
  163. end;

  164. function StopService(ServiceName: string) : boolean;
  165. var
  166.      hSCM      : HANDLE;
  167.      hService: HANDLE;
  168.      Status      : SERVICE_STATUS;
  169. begin
  170.      hSCM := OpenServiceManager();
  171.      Result := false;
  172.      if hSCM <> 0 then begin
  173.   hService := OpenService(hSCM,ServiceName,SERVICE_STOP);
  174.         if hService <> 0 then begin
  175.              Result := ControlService(hService,SERVICE_CONTROL_STOP,Status);
  176.             CloseServiceHandle(hService)
  177.   end;
  178.         CloseServiceHandle(hSCM)
  179.      end;
  180. end;

  181. function IsServiceRunning(ServiceName: string) : boolean;
  182. var
  183.      hSCM      : HANDLE;
  184.      hService: HANDLE;
  185.      Status      : SERVICE_STATUS;
  186. begin
  187.      hSCM := OpenServiceManager();
  188.      Result := false;
  189.      if hSCM <> 0 then begin
  190.   hService := OpenService(hSCM,ServiceName,SERVICE_QUERY_STATUS);
  191.          if hService <> 0 then begin
  192.        if QueryServiceStatus(hService,Status) then begin
  193.     Result :=(Status.dwCurrentState = SERVICE_RUNNING)
  194.              end;
  195.             CloseServiceHandle(hService)
  196.       end;
  197.         CloseServiceHandle(hSCM)
  198.      end
  199. end;

  200. // #######################################################################################
  201. // create an entry in the services file
  202. // #######################################################################################
  203. function SetupService(service, port, comment: string) : boolean;
  204. var
  205.      filename      : string;
  206.      s       : string;
  207.      lines  : TArrayOfString;
  208.      n       : longint;
  209.      i       : longint;
  210.      errcode  : integer;
  211.      servnamlen      : integer;
  212.      error  : boolean;
  213. begin
  214.      if UsingWinNT() = true then
  215.   filename := ExpandConstant('{sys}\drivers\etc\services')
  216.      else
  217.   filename := ExpandConstant('{win}\services');

  218.      if LoadStringsFromFile(filename,lines) = true then begin
  219.   Result  := true;
  220.   n       := GetArrayLength(lines) - 1;
  221.   servnamlen      := Length(service);
  222.   error  := false;

  223.   for i:=0 to n do begin
  224.        if Copy(lines,1,1) <> '#' then begin
  225.     s := Copy(lines,1,servnamlen);
  226.     if CompareText(s,service) = 0 then
  227.          exit; // found service-entry

  228.     if Pos(port,lines) > 0 then begin
  229.          error := true;
  230.          lines := '#' + lines + '  # disabled because collision with  ' + service + ' service';
  231.     end;
  232.        end
  233.        else if CompareText(Copy(lines,2,servnamlen),service) = 0 then begin
  234.     // service-entry was disabled
  235.     Delete(lines,1,1);
  236.     Result := SaveStringsToFile(filename,lines,false);
  237.     exit;
  238.        end;
  239.   end;

  240.   if error = true then begin
  241.        // save disabled entries
  242.        if SaveStringsToFile(filename,lines,false) = false then begin
  243.     Result := false;
  244.     exit;
  245.        end;
  246.   end;

  247.   // create new service entry
  248.   s := service + '      ' + port + '          # ' + comment + #13#10;
  249.   if SaveStringToFile(filename,s,true) = false then begin
  250.        Result := false;
  251.        exit;
  252.   end;

  253.   if error = true then begin
  254.        MsgBox('the ' + service + ' port was already used. The old service is disabled now. You should check the services file manually now.',mbInformation,MB_OK);
  255.        InstExec('notepad.exe',filename,GetCurrentDir(),true,false,SW_SHOWNORMAL,errcode);
  256.   end;
  257.      end
  258.      else
  259.   Result := false;
  260. end;

  261. // #######################################################################################
  262. // version functions
  263. // #######################################################################################
  264. function CheckVersion(Filename : string;hh,hl,lh,ll : integer) : boolean;
  265. var
  266.      VersionMS      : cardinal;
  267.      VersionLS      : cardinal;
  268.      CheckMS  : cardinal;
  269.      CheckLS  : cardinal;
  270. begin
  271.      if GetVersionNumbers(Filename,VersionMS,VersionLS) = false then
  272.   Result := false
  273.      else begin
  274.   CheckMS := (hh shl $10) or hl;
  275.   CheckLS := (lh shl $10) or ll;
  276.   Result := (VersionMS > CheckMS) or ((VersionMS = CheckMS) and (VersionLS >= CheckLS));
  277.      end;
  278. end;

  279. // Some examples for version checking
  280. function NeedShellFolderUpdate() : boolean;
  281. begin
  282.      Result := CheckVersion('ShFolder.dll',5,50,4027,300) = false;
  283. end;

  284. function NeedVCRedistUpdate() : boolean;
  285. begin
  286.      Result := (CheckVersion('mfc42.dll',6,0,8665,0) = false)
  287.   or (CheckVersion('msvcrt.dll',6,0,8797,0) = false)
  288.   or (CheckVersion('comctl32.dll',5,80,2614,3600) = false);
  289. end;

  290. function NeedHTMLHelpUpdate() : boolean;
  291. begin
  292.      Result := CheckVersion('hh.exe',4,72,0,0) = false;
  293. end;

  294. function NeedWinsockUpdate() : boolean;
  295. begin
  296.      Result := (UsingWinNT() = false) and (CheckVersion('mswsock.dll',4,10,0,1656) = false);
  297. end;

  298. function NeedDCOMUpdate() : boolean;
  299. begin
  300.      Result := (UsingWinNT() = false) and (CheckVersion('oleaut32.dll',2,30,0,0) = false);
  301. end;

  302. // function IsServiceInstalled(ServiceName: string) : boolean;
  303. // function IsServiceRunning(ServiceName: string) : boolean;
  304. // function InstallService(FileName, ServiceName, DisplayName, Description : string;ServiceType,StartType :cardinal) : boolean;
  305. // function RemoveService(ServiceName: string) : boolean;
  306. // function StartService(ServiceName: string) : boolean;
  307. // function StopService(ServiceName: string) : boolean;

  308. // function SetupService(service, port, comment: string) : boolean;

  309. // function CheckVersion(Filename : string;hh,hl,lh,ll : integer) : boolean;


  310. function InitializeSetup(): boolean;
  311. begin
  312.      if IsServiceInstalled('myservice') = false then begin
  313.   if InstallService('c:\winnt\system32\myservice.exe','myservice','my service','my service is doing usefull things',SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START) = true then begin
  314.        StartService('myservice');
  315.        StopService('myservice');
  316.        // after stopping a service you should wait some seconds before removing
  317.        RemoveService('myservice');
  318.        // otherwise removing can fail
  319.   end
  320.      end
  321.      else if IsServiceRunning('myservice') then
  322.   MsgBox('myservice is running',mbInformation, MB_OK);

  323.      Result := false
  324. end;
复制代码


回复

使用道具 举报

 楼主| 发表于 2017-3-6 16:19:16 | 显示全部楼层
如果只是套用,那么只要关心myservice部分就行了


  1. function InitializeSetup(): boolean;
  2. begin
  3. if IsServiceInstalled('myservice') = false then begin
  4. if InstallService('c:\winnt\system32\myservice.exe','myservice','my service','my service is doing usefull things',SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START) = true then begin
  5. StartService('myservice');
  6. StopService('myservice');
  7. // after stopping a service you should wait some seconds before removing
  8. RemoveService('myservice');
  9. // otherwise removing can fail
  10. end
  11. end
  12. else if IsServiceRunning('myservice') then
  13. MsgBox('myservice is running',mbInformation, MB_OK);

  14. Result := false
  15. end;
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-18 15:48 , Processed in 0.111127 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表