关于Windows系统服务的问题

lxdu120 2008-08-19 05:05:55
我在用Delphi开发一个系统服务控制软件,如何获得系统服务的描述信息和启动类型?
...全文
100 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
一如当初 2008-09-13
  • 打赏
  • 举报
回复
在注册表里读取
Kingron 2008-09-12
  • 打赏
  • 举报
回复
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;

其余的自己看着办吧。
lxdu120 2008-08-21
  • 打赏
  • 举报
回复
兄弟这个是获得系统类型。。。
我是要获得系统服务的描述信息。。。
系统服务就是以SYSTEM帐户运行的服务进程。。。明白不?
bright 2008-08-19
  • 打赏
  • 举报
回复
是要这个吗?


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;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧