如何在程序中控制某个服务程序的启动?

解决方案 »

  1.   

    启动和停止服务  (转自猛料)  
        
    uses WinSvc,SvcMgr !Here's example I made. Hope it helps. "ServerServiceName" is the Name ofservice to control.procedure StartServiceExecute(Sender: TObject);VarSCH: SC_HANDLE;SvcSCH: SC_HANDLE;arg: PChar;beginSCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);If SCH=0 thenBeginMessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);Exit;End;Screen.Cursor:= crHourGlass;trySvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);If SvcSCH=0 thenBeginMessageDlg('Error: OpenService', mtError, [mbOK], 0);Exit;End;tryarg:= nil;If Not StartService(SvcSCH, 0, arg) thenBeginCase GetLastError ofERROR_ACCESS_DENIED :MessageDlg('The specified handlewas not opened with SERVICE_START access.', mtError, [mbOK], 0);ERROR_INVALID_HANDLE :MessageDlg('The specified handleis invalid.', mtError, [mbOK], 0);ERROR_PATH_NOT_FOUND :MessageDlg('The service binaryfile could not be found.', mtError, [mbOK], 0);ERROR_SERVICE_ALREADY_RUNNING :MessageDlg('An instance of theservice is already running.', mtError, [mbOK], 0);ERROR_SERVICE_DATABASE_LOCKED :MessageDlg('The database islocked.', mtError, [mbOK], 0);ERROR_SERVICE_DEPENDENCY_DELETED :MessageDlg('The service dependson a service that does not exist or has been ed for deletion.', mtError,[mbOK], 0);ERROR_SERVICE_DEPENDENCY_FAIL :MessageDlg('The service dependson another service that has failed to start.', mtError, [mbOK], 0);ERROR_SERVICE_DISABLED :MessageDlg('The service has beendisabled.', mtError, [mbOK], 0);ERROR_SERVICE_LOGON_FAILED :MessageDlg('The service couldnot be logged on. This error occurs if the service was started from anaccount that does not have the "Log on as a service" right.', mtError,[mbOK], 0);ERROR_SERVICE_MARKED_FOR_DELETE :MessageDlg('The service has beened for deletion.', mtError, [mbOK], 0);ERROR_SERVICE_NO_THREAD :MessageDlg('A thread could notbe created for the service.', mtError, [mbOK], 0);ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for theservice was started, but it did not call StartServiceCtrlDispatcher, or thethread that called StartServiceCtrlDispatcher may be blocked in a controlhandler function.', mtError, [mbOK], 0);Else MessageDlg('Error:StartService', mtError, [mbOK], 0);End;{CASE}End;CheckServiceState;finallyCloseServiceHandle(SvcSCH);end;finallyScreen.Cursor:= crDefault;CloseServiceHandle(SCH);end;end;procedure StopServiceExecute(Sender: TObject);VarSCH: SC_HANDLE;SvcSCH: SC_HANDLE;ss: TServiceStatus;beginSCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);If SCH=0 thenBeginMessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);Exit;End;Screen.Cursor:= crHourGlass;trySvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);If SvcSCH=0 thenBeginMessageDlg('Error: OpenService', mtError, [mbOK], 0);Exit;End;tryIf Not ControlService(SvcSCH, SERVICE_CONTROL_STOP, ss) thenBeginCase GetLastError ofERROR_ACCESS_DENIED :MessageDlg('The specifiedhandle was not opened with the necessary access.', mtError, [mbOK], 0);ERROR_DEPENDENT_SERVICES_RUNNING :MessageDlg('The service cannotbe stopped because other running services are dependent on it.', mtError,[mbOK], 0);ERROR_INVALID_HANDLE :MessageDlg('The specifiedhandle was not obtained using CreateService or OpenService, or the handle isno longer valid.', mtError, [mbOK], 0);ERROR_INVALID_PARAMETER :MessageDlg('The requestedcontrol code is undefined.', mtError, [mbOK], 0);ERROR_INVALID_SERVICE_CONTROL :MessageDlg('The requestedcontrol code is not valid, or it is unacceptable to the service.', mtError,[mbOK], 0);ERROR_SERVICE_CANNOT_ACCEPT_CTRL :MessageDlg('The requestedcontrol code cannot be sent to the service because the state of the serviceis SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.',mtError, [mbOK], 0);ERROR_SERVICE_NOT_ACTIVE :MessageDlg('The service has notbeen started.', mtError, [mbOK], 0);ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for theservice was started, but it did not call StartServiceCtrlDispatcher, or thethread that called StartServiceCtrlDispatcher may be blocked in a controlhandler function.', mtError, [mbOK], 0);ERROR_SHUTDOWN_IN_PROGRESS :MessageDlg('The system isshutting down.', mtError, [mbOK], 0);Else MessageDlg('Error:ControlService', mtError, [mbOK], 0);End;End;CheckServiceState;finallyCloseServiceHandle(SvcSCH);end;finallyScreen.Cursor:= crDefault;CloseServiceHandle(SCH);end;end;如果出现编译错误,请删除CheckServiceState则一行代码,这个对你没有什么用!可以删除的。
     
       
      

  2.   

    简单的:WinExec('net start mysql',1);
    WinExec('net stop mysql',1);
      

  3.   


    WinExec('net start mysql',1);
    WinExec('net stop mysql',1);
    呵呵。这两就行了。不用写那么那么长:)