怎么控制windows服务???不用cmd用delphi如何实现启动停止windows服务(30分)

  • 主题发起人 主题发起人 jgame
  • 开始时间 开始时间
J

jgame

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么控制windows服务??<br><br><br>不用cmd用delphi如何实现启动停止windows服务
 
windows 系统服务
 
function StartService(AServName: string): Boolean; //use WinSvc<br>var<br> &nbsp;SCManager, hService: SC_HANDLE;<br> &nbsp;lpServiceArgVectors: PChar;<br>begin<br> &nbsp;SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br> &nbsp;Result := SCManager &lt;&gt; 0;<br> &nbsp;if Result then<br> &nbsp;try<br> &nbsp; &nbsp;hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);<br> &nbsp; &nbsp;Result := hService &lt;&gt; 0;<br> &nbsp; &nbsp;if (hService = 0) and (GetLastError = ERROR_SERVICE_DOES_NOT_EXIST) then<br> &nbsp; &nbsp; &nbsp;Exception.Create('The specified service does not exist');<br> &nbsp; &nbsp;if hService &lt;&gt; 0 then<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;lpServiceArgVectors := nil;<br> &nbsp; &nbsp; &nbsp;Result := WinSvc.StartService(hService, 0, PChar(lpServiceArgVectors));<br> &nbsp; &nbsp; &nbsp;if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;CloseServiceHandle(hService);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;CloseServiceHandle(SCManager);<br> &nbsp;end;<br>end;<br><br><br>function StopService(AServName: string): Boolean;<br>var<br> &nbsp;SCManager, hService: SC_HANDLE;<br> &nbsp;SvcStatus: TServiceStatus;<br>begin<br> &nbsp;SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br> &nbsp;Result := SCManager &lt;&gt; 0;<br> &nbsp;if Result then<br> &nbsp;try<br> &nbsp; &nbsp;hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);<br> &nbsp; &nbsp;Result := hService &lt;&gt; 0;<br> &nbsp; &nbsp;if Result then<br> &nbsp; &nbsp;try &nbsp;//停止并卸载服务;<br> &nbsp; &nbsp; &nbsp;Result := ControlService(hService, SERVICE_CONTROL_STOP, SvcStatus);<br> &nbsp; &nbsp; &nbsp;//删除服务,这一句可以不要;<br>// &nbsp; &nbsp; &nbsp;DeleteService(hService);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;CloseServiceHandle(hService);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;CloseServiceHandle(SCManager);<br> &nbsp;end;<br>end;
 
后退
顶部