我在用Delphi开发一个系统服务控制软件,如何获得系统服务的描述信息和启动类型?

解决方案 »

  1.   

    是要这个吗?
      function GetWinVersion: String;
      var
        VersionInfo : TOSVersionInfo;
        OSName      : String;
      begin
        VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );    if Windows.GetVersionEx( VersionInfo ) then begin
          with VersionInfo do begin
            case dwPlatformId of
              VER_PLATFORM_WIN32s   : OSName := 'Win32s';
              VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';
              VER_PLATFORM_WIN32_NT      : OSName := 'Windows NT';
            end;
            Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) +
                  #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';
          end;
        end 
        else
          Result := '';
      end;
      

  2.   

    兄弟这个是获得系统类型
    我是要获得系统服务的描述信息
    系统服务就是以SYSTEM帐户运行的服务进程明白不?
      

  3.   

    function QueryServiceConfig2(hService: SC_HANDLE; dwInfoLevel: DWORD;
      lpBuffer: PChar; cbBufSize: DWORD; var pcbBytesNeeded: DWORD): BOOL;
      stdcall; external advapi32 name 'QueryServiceConfig2A';查看SvcMgr和WinSVC单元里面的代码吧。里面有很多函数可以用的。
    function ServiceDescription(const ServiceName: string; const Computer: PChar = nil): 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
      if IsLocalComputer(Computer) then
        SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS)
      else
        SCM := OpenSCManager(Computer, 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;function ServiceDisplayName(const ServiceName: string; const Computer: PChar = nil): string;
    {
      返回指定服务的显示名称
    }
    var
      SCM, SCH: SC_Handle;
      L: DWORD;
    begin
      Result := '';
      if IsLocalComputer(Computer) then
        SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS)
      else
        SCM := OpenSCManager(Computer, 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;procedure ServiceNames(Names: TStrings; DisplayNames: TStrings = nil;
      const Service_Type: integer = $30; const Computer: PChar = nil);
    {
      返回系统服务列表,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;
      if IsLocalComputer(Computer) then
        SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS)
      else
        SCM := OpenSCManager(Computer, 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, Service_Type, 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;其余的自己看着办吧。