怎样用程序停止Win2000中正在启动的服务 ( 积分: 100 )

  • 主题发起人 主题发起人 dataking
  • 开始时间 开始时间
D

dataking

Unregistered / Unconfirmed
GUEST, unregistred user!
我想停止win2000中的某一个正在启动的服务,用Delphi程序怎样来实现??哪个大哥能指导一下小弟呀,谢了!
 
我想停止win2000中的某一个正在启动的服务,用Delphi程序怎样来实现??哪个大哥能指导一下小弟呀,谢了!
 
WinExec(PAnsiChar('net stop "服务名称"'),SW_HIDE)
 
<br>能不能给出一个例子呀?
 
uses WinSvc,SvcMgr !<br>Here's example I made. Hope it helps. &quot;ServerServiceName&quot; is the Name of<br>service to control.<br>procedure StartServiceExecute(Sender: TObject);<br>Var<br>SCH: SC_HANDLE;<br>SvcSCH: SC_HANDLE;<br>arg: PChar;<br>begin<br>SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br>If SCH=0 then<br>Begin<br>MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);<br>Exit;<br>End;<br>Screen.Cursor:= crHourGlass;<br>try<br>SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);<br>If SvcSCH=0 then<br>Begin<br>MessageDlg('Error: OpenService', mtError, [mbOK], 0);<br>Exit;<br>End;<br>try<br>arg:= nil;<br>If Not StartService(SvcSCH, 0, arg) then<br>Begin<br>Case GetLastError of<br>ERROR_ACCESS_DENIED :MessageDlg('The specified handle<br>was not opened with SERVICE_START access.', mtError, [mbOK], 0);<br>ERROR_INVALID_HANDLE :MessageDlg('The specified handle<br>is invalid.', mtError, [mbOK], 0);<br>ERROR_PATH_NOT_FOUND :MessageDlg('The service binary<br>file could not be found.', mtError, [mbOK], 0);<br>ERROR_SERVICE_ALREADY_RUNNING :MessageDlg('An instance of the<br>service is already running.', mtError, [mbOK], 0);<br>ERROR_SERVICE_DATABASE_LOCKED :MessageDlg('The database is<br>locked.', mtError, [mbOK], 0);<br>ERROR_SERVICE_DEPENDENCY_DELETED :MessageDlg('The service depends<br>on a service that does not exist or has been marked for deletion.', mtError,<br>[mbOK], 0);<br>ERROR_SERVICE_DEPENDENCY_FAIL :MessageDlg('The service depends<br>on another service that has failed to start.', mtError, [mbOK], 0);<br>ERROR_SERVICE_DISABLED :MessageDlg('The service has been<br>disabled.', mtError, [mbOK], 0);<br>ERROR_SERVICE_LOGON_FAILED :MessageDlg('The service could<br>not be logged on. This error occurs if the service was started from an<br>account that does not have the &quot;Log on as a service&quot; right.', mtError,<br>[mbOK], 0);<br>ERROR_SERVICE_MARKED_FOR_DELETE :MessageDlg('The service has been<br>marked for deletion.', mtError, [mbOK], 0);<br>ERROR_SERVICE_NO_THREAD :MessageDlg('A thread could not<br>be created for the service.', mtError, [mbOK], 0);<br>ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for the<br>service was started, but it did not call StartServiceCtrlDispatcher, or the<br>thread that called StartServiceCtrlDispatcher may be blocked in a control<br>handler function.', mtError, [mbOK], 0);<br>Else MessageDlg('Error:StartService', mtError, [mbOK], 0);<br>End;{CASE}<br>End;<br>CheckServiceState;<br>finally<br>CloseServiceHandle(SvcSCH);<br>end;<br>finally<br>Screen.Cursor:= crDefault;<br>CloseServiceHandle(SCH);<br>end;<br>end;<br>procedure StopServiceExecute(Sender: TObject);<br>Var<br>SCH: SC_HANDLE;<br>SvcSCH: SC_HANDLE;<br>ss: TServiceStatus;<br>begin<br>SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br>If SCH=0 then<br>Begin<br>MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);<br>Exit;<br>End;<br>Screen.Cursor:= crHourGlass;<br>try<br>SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);<br>If SvcSCH=0 then<br>Begin<br>MessageDlg('Error: OpenService', mtError, [mbOK], 0);<br>Exit;<br>End;<br>try<br>If Not ControlService(SvcSCH, SERVICE_CONTROL_STOP, ss) then<br>Begin<br>Case GetLastError of<br>ERROR_ACCESS_DENIED :MessageDlg('The specified<br>handle was not opened with the necessary access.', mtError, [mbOK], 0);<br>ERROR_DEPENDENT_SERVICES_RUNNING :MessageDlg('The service cannot be stopped because other running services are dependent on it.', mtError,[mbOK], 0);<br>ERROR_INVALID_HANDLE :MessageDlg('The specified handle was not obtained using CreateService or OpenService, or the handle is no longer valid.', mtError, [mbOK], 0);<br>ERROR_INVALID_PARAMETER :MessageDlg('The requested<br>control code is undefined.', mtError, [mbOK], 0);<br>ERROR_INVALID_SERVICE_CONTROL :MessageDlg('The requested<br>control code is not valid, or it is unacceptable to the service.', mtError,<br>[mbOK], 0);<br>ERROR_SERVICE_CANNOT_ACCEPT_CTRL :MessageDlg('The requested<br>control code cannot be sent to the service because the state of the service<br>is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.',<br>mtError, [mbOK], 0);<br>ERROR_SERVICE_NOT_ACTIVE :MessageDlg('The service has not<br>been started.', mtError, [mbOK], 0);<br>ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for the<br>service was started, but it did not call StartServiceCtrlDispatcher, or the<br>thread that called StartServiceCtrlDispatcher may be blocked in a control<br>handler function.', mtError, [mbOK], 0);<br>ERROR_SHUTDOWN_IN_PROGRESS :MessageDlg('The system is<br>shutting down.', mtError, [mbOK], 0);<br>Else MessageDlg('Error:<br>ControlService', mtError, [mbOK], 0);<br>End;<br>End;<br>CheckServiceState;<br>finally<br>CloseServiceHandle(SvcSCH);<br>end;<br>finally<br>Screen.Cursor:= crDefault;<br>CloseServiceHandle(SCH);<br>end;<br>end;<br><br>如果出现编译错误,请删除CheckServiceState则一行代码,这个对你没有什么用!可以删除的。<br>
 
参考:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2114375
 
多人接受答案了。
 
后退
顶部