首页  编辑  

服务器管理的相关函数

Tags: /超级猛料/Core.驱动,VxD,服务/   Date Created:
//////////////////////////////////////////////////////////////////////////////
//       Service function & procedure
//////////////////////////////////////////////////////////////////////////////
function ServiceStart(const ServiceName: string): Boolean;
{   启动服务 }
var  SCM, SCH: SC_Handle;
  P: PChar;
begin
  Result := False;
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS); 
  if SCM <> 0 then
  begin
    SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if SCH <> 0 then
    begin
      Result := StartService(SCH, 0, P);
      CloseServiceHandle(SCH);    
    end;
    CloseServiceHandle(SCM);
  end;
end;

function ServiceStop(const ServiceName: string): Boolean;
{   停止服务 }
var
  SCM, SCH: SC_Handle;
  ServiceStatus: TServiceStatus;
begin
  Result := False;
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCM <> 0 then
  begin
    SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if SCH <> 0 then
    begin
      Result := ControlService(SCH, SERVICE_CONTROL_STOP, ServiceStatus);
      CloseServiceHandle(SCH);
    end;
    CloseServiceHandle(SCM);
  end;
end;

function ServiceShutdown(const ServiceName: string): Boolean;
{   关闭服务 }
var
  SCM, SCH: SC_Handle;
  ServiceStatus: TServiceStatus;
begin
  Result := False;
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCM <> 0 then  
  begin    
    SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);    
    if SCH <> 0 then 
    begin    
      Result := ControlService(SCH, SERVICE_CONTROL_SHUTDOWN, ServiceStatus); 
      CloseServiceHandle(SCH);    
    end;    
    
    CloseServiceHandle(SCM);  
  end;
end;

function ServiceStatus(const ServiceName: string): integer;
{ 返回指定服务的状态 }
var  
  SCM, SCH: SC_Handle;  
  ServiceStatus: TServiceStatus;
begin  
  Result := 0;  
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCM <> 0 then  
  begin    
    SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);    
    if SCH <> 0 then    
    begin      
      QueryServiceStatus(SCH, ServiceStatus);      
      Result := ServiceStatus.dwCurrentState;      
      CloseServiceHandle(SCH);    
    end;    
    CloseServiceHandle(SCM);
  end;
end;

function ServiceRunning(const ServiceName: string): Boolean;
{ 判断指定服务是否正在运行 }
begin  
  Result := ServiceStatus(ServiceName) = SERVICE_RUNNING;
end;

function ServiceStopped(const ServiceName: string): Boolean;
{ 判断指定服务是否停止运行 }
begin  
  Result := ServiceStatus(ServiceName) = SERVICE_STOPPED;
end;

function ServiceDisplayName(const ServiceName: string): string;
{ 返回指定服务的显示名称 }
var  
  SCM, SCH: SC_Handle;  
  L: DWORD;
begin  
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);  
  if SCM <> 0 then  
  begin    
    SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);    
    if SCH <> 0 then    
    begin      
      GetServiceDisplayName(SCM, PChar(ServiceName), nil, L);      
      SetLength(Result, L);      
      GetServiceDisplayName(SCM, PChar(ServiceName), PChar(Result), L);      
      CloseServiceHandle(SCH);    
    end;    
    CloseServiceHandle(SCM);
  end;
end;

function ServiceFileName(const Name: string): string;
{ 返回指定服务对应的可执行文件名称 }
var  
  SCM, SCH: SC_Handle;  
  ServiceConfig: PQueryServiceConfig;  
  R: DWORD;
begin  
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);  
  if SCM <> 0 then  
  begin    
    SCH := OpenService(SCM, PChar(Name), SERVICE_ALL_ACCESS);    
    if SCH <> 0 then    
    begin      
      QueryServiceConfig(SCH, nil, 0, R); // Get Buffer Length 
      GetMem(ServiceConfig, R + 1);      
      QueryServiceConfig(SCH, ServiceConfig, R + 1, R);      
      Result := ServiceConfig.lpBinaryPathName;      
      FreeMem(ServiceConfig);      
      CloseServiceHandle(SCH);    
    end;    
    CloseServiceHandle(SCM);
  end;
end;

function QueryServiceConfig2(hService: SC_HANDLE; dwInfoLevel: DWORD;  
  lpBuffer: PChar; cbBufSize: DWORD; var pcbBytesNeeded: DWORD): BOOL;  
  stdcall; external advapi32 name 'QueryServiceConfig2A';

function ServiceDescription(const ServiceName: string): string;
{ 返回指定服务的描述信息 }
const  
  SERVICE_CONFIG_DESCRIPTION = $00000001;
type  
  TServiceDescription = packed record    
    lpDescription: LPSTR;
  end;  
  PServiceDescription = ^TServiceDescription;
var  
  SCM, SCH: SC_Handle;  
  P: PServiceDescription;  
  R: DWORD;
begin  
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);  
  if SCM <> 0 then  
  begin    
    SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);    
    if SCH <> 0 then    
    begin      
      QueryServiceConfig2(SCH, SERVICE_CONFIG_DESCRIPTION, nil, 0, R); // Get Buffer Length
      GetMem(P, R + 1);      
      QueryServiceConfig2(SCH, SERVICE_CONFIG_DESCRIPTION, PChar(P), R + 1, R);      
      Result := P.lpDescription;      
      FreeMem(P);      
      CloseServiceHandle(SCH);    
    end;    
    CloseServiceHandle(SCM);
  end;
end;

procedure ServiceNames(Names: TStrings; DisplayNames: TStrings = nil; const uType: integer = SERVICE_WIN32);
{ 返回系统服务列表, Names :用于接收返回的服务名称列表,  
  DisplayName :用于接收返回的对应显示名称列表,  
  uType :需要返回那些类型服务名称列表,可以为 SERVICE_DRIVER,SERVICE_WIN32,SERVICE_ALL}
type  
  TEnumServices = array[0..0] of TEnumServiceStatus;  
  PEnumServices = ^TEnumServices;
var  
  SCM: SC_Handle;  
  Services: PEnumServices;  
  Len: Cardinal;  
  ServiceCount, ResumeHandle, i: Cardinal;
begin  
  ResumeHandle := 0;  
  SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);  
  Len := 0;  
  ServiceCount := 0;  
  Services := nil;  
  try    
    Names.BeginUpdate;    
    if Assigned(DisplayNames) then DisplayNames.BeginUpdate;    
    if SCM <> 0 then    
    begin      
      EnumServicesStatus(SCM, uType, SERVICE_STATE_ALL,        
        Services[0], 0, Len, ServiceCount, ResumeHandle);      
      GetMem(Services, Len);      
      EnumServicesStatus(SCM, SERVICE_DRIVER or SERVICE_WIN32, SERVICE_STATE_ALL,        
        Services[0], Len, Len, ServiceCount, ResumeHandle);      
      for i := 0 to ServiceCount - 1 do      
      begin        
        Names.Add(Services[i].lpServiceName);        
        if Assigned(DisplayNames) then DisplayNames.Add(Services[i].lpDisplayName);      
      end;      
      FreeMem(Services);    
    end;  
  finally    
    Names.EndUpdate;    
    if Assigned(DisplayNames) then DisplayNames.EndUpdate;    
    CloseServiceHandle(SCM);  
  end;
end;