阿杰 发表于 2009-7-26 14:27:58

【开源】delphi调用API来实启动/停止/卸载windows服务

<p>建议参考一下Delphi的ScktSrvr原码, $(DELPHI)\Source\Vcl\ScktSrvr.dpr&nbsp;&nbsp;</p>
<p>--------------------------------------------------------------------------------<br/>windows 系统服务<br/>--------------------------------------------------------------------------------<br/>function StartService(AServName: string): Boolean; //use WinSvc<br/>var<br/>SCManager, hService: SC_HANDLE;<br/>lpServiceArgVectors: PChar;<br/>begin<br/>SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br/>Result := SCManager &lt;&gt; 0;<br/>if Result then<br/>try<br/>&nbsp;&nbsp;&nbsp; hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);<br/>&nbsp;&nbsp;&nbsp; Result := hService &lt;&gt; 0;<br/>&nbsp;&nbsp;&nbsp; if (hService = 0) and (GetLastError = ERROR_SERVICE_DOES_NOT_EXIST) then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exception.Create('The specified service does not exist');<br/>&nbsp;&nbsp;&nbsp; if hService &lt;&gt; 0 then<br/>&nbsp;&nbsp;&nbsp; try<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lpServiceArgVectors := nil;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := WinSvc.StartService(hService, 0, PChar(lpServiceArgVectors));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := True;<br/>&nbsp;&nbsp;&nbsp; finally<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseServiceHandle(hService);<br/>&nbsp;&nbsp;&nbsp; end;<br/>finally<br/>&nbsp;&nbsp;&nbsp; CloseServiceHandle(SCManager);<br/>end;<br/>end;</p>
<p><br/>function StopService(AServName: string): Boolean;<br/>var<br/>SCManager, hService: SC_HANDLE;<br/>SvcStatus: TServiceStatus;<br/>begin<br/>SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br/>Result := SCManager &lt;&gt; 0;<br/>if Result then<br/>try<br/>&nbsp;&nbsp;&nbsp; hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);<br/>&nbsp;&nbsp;&nbsp; Result := hService &lt;&gt; 0;<br/>&nbsp;&nbsp;&nbsp; if Result then<br/>&nbsp;&nbsp;&nbsp; try //停止并卸载服务;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := ControlService(hService, SERVICE_CONTROL_STOP, SvcStatus);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //删除服务,这一句可以不要;<br/>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DeleteService(hService);<br/>&nbsp;&nbsp;&nbsp; finally<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseServiceHandle(hService);<br/>&nbsp;&nbsp;&nbsp; end;<br/>finally<br/>&nbsp;&nbsp;&nbsp; CloseServiceHandle(SCManager);<br/>end;<br/>end;</p>
<p><br/>------------------------------------------------------------------------------------------------------<br/>Others<br/>------------------------------------------------------------------------------------------------------</p>
<p><br/>uses<br/>Winsvc,..,;<br/><br/>function RunServies(svr:String):Boolean;//启动某个服务;<br/>var<br/>schService:SC_HANDLE;<br/>schSCManager:SC_HANDLE;<br/>ssStatus:TServiceStatus;<br/>Argv:PChar;<br/>begin<br/>schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);<br/>schService:=OpenService(schSCManager,Pchar(svr),SERVICE_ALL_ACCESS);<br/>result := True;<br/>try<br/>&nbsp;&nbsp;&nbsp; if StartService(schService,0,Argv) then<br/>&nbsp;&nbsp;&nbsp; begin<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (QueryServiceStatus(schService,ssStatus)) do<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(500);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application.ProcessMessages;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ssStatus.dwCurrentState=SERVICE_START_PENDING then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(500)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Break;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;//while<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ssStatus.dwCurrentState=SERVICE_RUNNING then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := False;<br/>&nbsp;&nbsp;&nbsp; end<br/>&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := False;<br/>finally<br/>&nbsp;&nbsp;&nbsp; CloseServiceHandle(schService);<br/>&nbsp;&nbsp;&nbsp; CloseServiceHandle(schSCManager);<br/>end;<br/>end;<br/>function StopServies(svr:String):Boolean;//停止某个服务;<br/>var<br/>schService:SC_HANDLE;<br/>schSCManager:SC_HANDLE;<br/>ssStatus:TServiceStatus;<br/>begin<br/>schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);<br/>schService:=OpenService(schSCManager,Pchar(svr),SERVICE_ALL_ACCESS);<br/>try<br/>&nbsp;&nbsp;&nbsp; if ControlService(schService,SERVICE_CONTROL_STOP,ssStatus) then<br/>&nbsp;&nbsp;&nbsp; begin<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(1000);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (QueryServiceStatus(schService,ssStatus)) do<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application.ProcessMessages;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ssStatus.dwCurrentState=SERVICE_STOP_PENDING then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(1000)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end; //while<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ssStatus.dwCurrentState=SERVICE_STOPPED then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := False;<br/>end<br/>else<br/>&nbsp;&nbsp;&nbsp; result := False;<br/>finally<br/>&nbsp;&nbsp;&nbsp; CloseServiceHandle(schService);<br/>&nbsp;&nbsp;&nbsp; CloseServiceHandle(schSCManager);<br/>end;<br/>end;</p>
<p>------------------------------------------------------------------------------------------------------<br/>Others<br/>------------------------------------------------------------------------------------------------------</p>
<p></p>
<p>unit Unit1;</p>
<p>interface</p>
<p>uses<br/>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br/>Dialogs, StdCtrls,Winsvc;</p>
<p>type<br/>TForm1 = class(TForm)<br/>Button1: TButton;<br/>Button2: TButton;<br/>procedure FormCreate(Sender: TObject);<br/>procedure FormClose(Sender: TObject; var Action: TCloseAction);<br/>procedure Button1Click(Sender: TObject);<br/>procedure Button2Click(Sender: TObject);<br/>private<br/>schService:SC_HANDLE;<br/>schSCManager:SC_HANDLE;<br/>ssStatus:TServiceStatus;<br/>{ Private declarations }<br/>public<br/>{ Public declarations }<br/>end;</p>
<p>var<br/>Form1: TForm1;</p>
<p>implementation</p>
<p>{$R *.dfm}</p>
<p>procedure TForm1.FormCreate(Sender: TObject);<br/>begin<br/>schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);<br/>schService:=OpenService(schSCManager,'alerter',SERVICE_ALL_ACCESS);<br/>end;</p>
<p>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br/>begin<br/>CloseServiceHandle(schService);<br/>CloseServiceHandle(schSCManager);<br/>end;</p>
<p>procedure TForm1.Button1Click(Sender: TObject);<br/>var<br/>Argv:PChar;<br/>begin<br/>if StartService(schService,0,Argv) then<br/>begin<br/>form1.caption:='Start Service Now ';<br/>Sleep(1000);<br/>while (QueryServiceStatus(schService,ssStatus)) do<br/>begin<br/>Application.ProcessMessages;<br/>if ssStatus.dwCurrentState=SERVICE_START_PENDING then<br/>begin<br/>form1.caption:=form1.caption+('.');<br/>Sleep(1000);<br/>end else break;<br/>end;<br/>if ssStatus.dwCurrentState=SERVICE_RUNNING then<br/>form1.caption:='Service Start Ok'<br/>else form1.caption:='Service Start Fail';<br/>end<br/>else form1.caption:='Service Start Fail';<br/>end;</p>
<p>procedure TForm1.Button2Click(Sender: TObject);<br/>begin<br/>if ControlService(schService,SERVICE_CONTROL_STOP,ssStatus) then<br/>begin<br/>form1.caption:='Stopping Service Now ';<br/>Sleep(1000);<br/>while (QueryServiceStatus(schService,ssStatus)) do<br/>begin<br/>Application.ProcessMessages;<br/>if ssStatus.dwCurrentState=SERVICE_STOP_PENDING then<br/>begin<br/>form1.caption:=form1.caption+('.');<br/>Sleep(1000);<br/>end else break;<br/>end;<br/>if ssStatus.dwCurrentState=SERVICE_STOPPED then<br/>form1.caption:='Service Stop Ok'<br/>else form1.caption:='Service Stop Fail';<br/>end<br/>else form1.caption:='Service Stop Fail';<br/>end;<br/>end.<br/></p>
页: [1]
查看完整版本: 【开源】delphi调用API来实启动/停止/卸载windows服务