再win2k下怎么打开,关闭,列举服务?(100分)

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

DreamLand

Unregistered / Unconfirmed
GUEST, unregistred user!
应该使用那些api呢?
 
下面是我程序的一部分(未作任何改动),希望对你有所帮助,不要指望这个程序COPY到你的源码里就能用。<br>因为这只是程序的一部分。作为一个被调用的函数。里面有Sybase服务的调用方法。<br>====================================<br>{TODO:启动或关闭Sybase服务}<br><br>function tfrm_main.Stop_or_Start_Service(ServiceName: string; Operater:<br>&nbsp; integer): Boolean;<br>var<br>&nbsp; SrvHandle: SC_HANDLE; //这些都在winsvc单元中定义的<br>&nbsp; A: pchar;<br>&nbsp; B: _SERVICE_STATUS;<br>begin<br>&nbsp; sleep(100);<br>&nbsp; if ServiceName = 'SYBBCK' then<br>&nbsp; &nbsp; ServiceName := 'SYBBCK_' + Uppercase(ServerName) + '_BS';<br>&nbsp; if ServiceName = 'SYBSQL' then<br>&nbsp; &nbsp; ServiceName := 'SYBSQL_' + Uppercase(ServerName);<br>&nbsp; if (ServiceName = 'SYBSQL') and (operater = 0) then<br>&nbsp; &nbsp; dm.db_csbt.Connected := false;<br>&nbsp; //取sql server的状态,如果sql server正在运行则返回true ,否则返回 false<br>&nbsp; SrvHandle := OpenSCManager('', SERVICES_ACTIVE_DATABASE,<br>&nbsp; &nbsp; SC_MANAGER_ALL_ACCESS);<br>&nbsp; if Operater = 1 then<br>&nbsp; begin<br>&nbsp; &nbsp; SrvHandle := OpenService(SrvHandle, PChar(ServiceName), SERVICE_START);<br>&nbsp; &nbsp; StartService(SrvHandle, 0, A);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; SrvHandle := OpenService(SrvHandle, PChar(ServiceName), SERVICE_STOP);<br>&nbsp; &nbsp; ControlService(SrvHandle, SERVICE_CONTROL_STOP, B);<br>&nbsp; end;<br>&nbsp; Result := true;<br>end;<br>====================================<br>{TODO:检测SQLService服务的状态。}<br>function tfrm_main.GetSqlServerStatus(ServiceName: string; var ServiceStatus:<br>&nbsp; Integer): Boolean;<br>var<br>&nbsp; SrvHandle: SC_HANDLE; //这些都在winsvc单元中定义的<br>&nbsp; Service_Status: _SERVICE_STATUS; //在winsvc单元中有定义<br>&nbsp; SrvStatus: Integer;<br>begin<br>&nbsp; ServerName := trim(edt_svrname.Text);<br>&nbsp; Username := trim(edt_userid.Text);<br>&nbsp; UserPass := trim(edt_userpass.Text);<br>&nbsp; Database_name := trim(edt_dbname.Text);<br><br>&nbsp; if ServiceName = 'SYBBCK' then<br>&nbsp; &nbsp; ServiceName := 'SYBBCK_' + Uppercase(ServerName) + '_BS';<br>&nbsp; if ServiceName = 'SYBSQL' then<br>&nbsp; &nbsp; ServiceName := 'SYBSQL_' + Uppercase(ServerName);<br><br>&nbsp; //取sql server的状态,如果sql server正在运行则返回true ,否则返回 false<br>&nbsp; SrvHandle := OpenSCManager('', SERVICES_ACTIVE_DATABASE,<br>&nbsp; &nbsp; SC_MANAGER_ALL_ACCESS);<br>&nbsp; SrvHandle := OpenService(SrvHandle, PChar(ServiceName), SERVICE_QUERY_STATUS<br>&nbsp; &nbsp; or<br>&nbsp; &nbsp; SERVICE_START);<br>&nbsp; case SrvHandle of<br>&nbsp; &nbsp; ERROR_ACCESS_DENIED:<br>&nbsp; &nbsp; &nbsp; mm_output.Lines.Add('The specified service control manager database handle does not have access to the service.');<br>&nbsp; &nbsp; ERROR_INVALID_HANDLE:<br>&nbsp; &nbsp; &nbsp; mm_output.Lines.Add('The specified handle is invalid.');<br>&nbsp; &nbsp; ERROR_INVALID_NAME:<br>&nbsp; &nbsp; &nbsp; mm_output.Lines.Add('The specified service name is invalid.');<br>&nbsp; &nbsp; ERROR_SERVICE_DOES_NOT_EXIST:<br>&nbsp; &nbsp; &nbsp; mm_output.Lines.Add('The specified service does not exist.');<br>&nbsp; end;<br><br>&nbsp; if QueryServiceStatus(SrvHandle, Service_Status) then<br>&nbsp; begin<br>&nbsp; &nbsp; //判断Sql Server服务的状态<br>&nbsp; &nbsp; SrvStatus := Service_Status.dwCurrentState;<br>&nbsp; &nbsp; case SrvStatus of<br>&nbsp; &nbsp; &nbsp; SERVICE_STOPPED: ServiceStatus := 0; //The service is not running.<br>&nbsp; &nbsp; &nbsp; SERVICE_START_PENDING: ServiceStatus := 3; //The service is starting.<br>&nbsp; &nbsp; &nbsp; SERVICE_STOP_PENDING: ServiceStatus := 0; //The service is stopping.<br>&nbsp; &nbsp; &nbsp; SERVICE_RUNNING: ServiceStatus := 1; //The service is running.<br>&nbsp; &nbsp; &nbsp; SERVICE_CONTINUE_PENDING: ServiceStatus := 0;<br>&nbsp; &nbsp; &nbsp; //The service continue is pending.<br>&nbsp; &nbsp; &nbsp; SERVICE_PAUSE_PENDING: ServiceStatus := 0; // The service pause is pending.<br>&nbsp; &nbsp; &nbsp; SERVICE_PAUSED: ServiceStatus := 0; // &nbsp; The service is paused<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; //Result := (SrvStatus = SERVICE_RUNNING);<br>&nbsp; &nbsp; result := true;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; Result := true;<br>&nbsp; ;<br>end;<br><br>
 
services api:<br>ChangeServiceConfig<br>CloseServiceHandle<br>ControlService<br>CreateService<br>DeleteService<br>EnumDependentServices<br>EnumServicesStatus<br>GetServiceDisplayName<br>GetServiceKeyName<br>Handler<br>LockServiceDatabase<br>NotifyBootConfigStatus<br>OpenSCManager<br>OpenService<br>QueryServiceConfig<br>QueryServiceLockStatus<br>QueryServiceObjectSecurity<br>QueryServiceStatus<br>RegisterServiceCtrlHandler<br>ServiceMain<br>SetServiceBits<br>SetServiceObjectSecurity<br>SetServiceStatus<br>StartService<br>StartServiceCtrlDispatcher<br>UnlockServiceDatabase
 
同意使用LittleIsland写的这些函数,具体使用蛮麻烦的,最好看看参考书
 
在WINSDK中搜索 SERVICE FUNTIONS 主题就可以找到所有的服务操作函数,自己去看看最好,<br>在DELPHI 中做服务程序简单得不得了,
 
权限不够恐怕什么也做不了。
 
NowCan:<br><br>在DELPHI中做服务程序,很简单了,因为IDE已经给你把框架搭起来了,,,
 
HeyTommy说的对,你可以自己写服务的而且开关随你设定!
 
后退
顶部