下面是我程序的一部分(未作任何改动),希望对你有所帮助,不要指望这个程序COPY到你的源码里就能用。<br>因为这只是程序的一部分。作为一个被调用的函数。里面有Sybase服务的调用方法。<br>====================================<br>{TODO:启动或关闭Sybase服务}<br><br>function tfrm_main.Stop_or_Start_Service(ServiceName: string; Operater:<br> integer): Boolean;<br>var<br> SrvHandle: SC_HANDLE; //这些都在winsvc单元中定义的<br> A: pchar;<br> B: _SERVICE_STATUS;<br>begin<br> sleep(100);<br> if ServiceName = 'SYBBCK' then<br> ServiceName := 'SYBBCK_' + Uppercase(ServerName) + '_BS';<br> if ServiceName = 'SYBSQL' then<br> ServiceName := 'SYBSQL_' + Uppercase(ServerName);<br> if (ServiceName = 'SYBSQL') and (operater = 0) then<br> dm.db_csbt.Connected := false;<br> //取sql server的状态,如果sql server正在运行则返回true ,否则返回 false<br> SrvHandle := OpenSCManager('', SERVICES_ACTIVE_DATABASE,<br> SC_MANAGER_ALL_ACCESS);<br> if Operater = 1 then<br> begin<br> SrvHandle := OpenService(SrvHandle, PChar(ServiceName), SERVICE_START);<br> StartService(SrvHandle, 0, A);<br> end<br> else<br> begin<br> SrvHandle := OpenService(SrvHandle, PChar(ServiceName), SERVICE_STOP);<br> ControlService(SrvHandle, SERVICE_CONTROL_STOP, B);<br> end;<br> Result := true;<br>end;<br>====================================<br>{TODO:检测SQLService服务的状态。}<br>function tfrm_main.GetSqlServerStatus(ServiceName: string; var ServiceStatus:<br> Integer): Boolean;<br>var<br> SrvHandle: SC_HANDLE; //这些都在winsvc单元中定义的<br> Service_Status: _SERVICE_STATUS; //在winsvc单元中有定义<br> SrvStatus: Integer;<br>begin<br> ServerName := trim(edt_svrname.Text);<br> Username := trim(edt_userid.Text);<br> UserPass := trim(edt_userpass.Text);<br> Database_name := trim(edt_dbname.Text);<br><br> if ServiceName = 'SYBBCK' then<br> ServiceName := 'SYBBCK_' + Uppercase(ServerName) + '_BS';<br> if ServiceName = 'SYBSQL' then<br> ServiceName := 'SYBSQL_' + Uppercase(ServerName);<br><br> //取sql server的状态,如果sql server正在运行则返回true ,否则返回 false<br> SrvHandle := OpenSCManager('', SERVICES_ACTIVE_DATABASE,<br> SC_MANAGER_ALL_ACCESS);<br> SrvHandle := OpenService(SrvHandle, PChar(ServiceName), SERVICE_QUERY_STATUS<br> or<br> SERVICE_START);<br> case SrvHandle of<br> ERROR_ACCESS_DENIED:<br> mm_output.Lines.Add('The specified service control manager database handle does not have access to the service.');<br> ERROR_INVALID_HANDLE:<br> mm_output.Lines.Add('The specified handle is invalid.');<br> ERROR_INVALID_NAME:<br> mm_output.Lines.Add('The specified service name is invalid.');<br> ERROR_SERVICE_DOES_NOT_EXIST:<br> mm_output.Lines.Add('The specified service does not exist.');<br> end;<br><br> if QueryServiceStatus(SrvHandle, Service_Status) then<br> begin<br> //判断Sql Server服务的状态<br> SrvStatus := Service_Status.dwCurrentState;<br> case SrvStatus of<br> SERVICE_STOPPED: ServiceStatus := 0; //The service is not running.<br> SERVICE_START_PENDING: ServiceStatus := 3; //The service is starting.<br> SERVICE_STOP_PENDING: ServiceStatus := 0; //The service is stopping.<br> SERVICE_RUNNING: ServiceStatus := 1; //The service is running.<br> SERVICE_CONTINUE_PENDING: ServiceStatus := 0;<br> //The service continue is pending.<br> SERVICE_PAUSE_PENDING: ServiceStatus := 0; // The service pause is pending.<br> SERVICE_PAUSED: ServiceStatus := 0; // The service is paused<br> end;<br> //Result := (SrvStatus = SERVICE_RUNNING);<br> result := true;<br> end<br> else<br> Result := true;<br> ;<br>end;<br><br>