W
wanwqing
Unregistered / Unconfirmed
GUEST, unregistred user!
我用如下代码实现了获取现有服务,可是如何同时直接得到它的状态呢?我的程序是通过我自己的函数实现的.我用过 ESS.ServiceStatus 可是不知道具体怎么改,老提示我类型不对!求助!
procedure TServiceForm1.GetServices(ServiceType, ServiceState: Cardinal);
var // ; Services: TStrings
SvcHandle: SC_HANDLE;
ListItem :TListItem;
ErrStr: String;
ESS: Array of TEnumServiceStatus;
nBytesNeeded,nServices,nResumeHandle: Cardinal;
i: Integer;
s:string;
begin
// if not Assigned(Services) then
// Exit;
SvcHandle:=OpenSCManager(Nil,Nil,SC_MANAGER_ENUMERATE_SERVICE);
if SvcHandle=0 then
begin
if GetLastError = ERROR_ACCESS_DENIED then
ErrStr:='The requested access was denied'
else if GetLastError = ERROR_DATABASE_DOES_NOT_EXIST then
ErrStr:='The specified database does not exist'
else if GetLastError = ERROR_INVALID_PARAMETER then
ErrStr:='A parameter that was specified is invalid'
else
ErrStr:='OpenSCManager failed';
Raise Exception.Create(ErrStr);
Exit;
end;
nResumeHandle:=0;
try
EnumServicesStatus(SvcHandle,ServiceType,ServiceState,ESS[0],0,
nBytesNeeded,nServices,nResumeHandle);
SetLength(ESS,(nBytesNeeded div SizeOf(TEnumServiceStatus)+1));
if not EnumServicesStatus(SvcHandle,ServiceType,ServiceState,ESS[0],nBytesNeeded,
nBytesNeeded,nServices,nResumeHandle) then
Exit;
for i:=0 to nServices-1 do
begin
// Services.Add(ESS.lpServiceName);
ListItem:=ListV.items.add;
ListItem.caption:=ESS.lpDisplayName;
s:=ListItem.caption;
if GetSqlServerStatus(s) then
//ESS.lpServiceName;
ListItem.subitems.add('启动');
end;
//Services.Add(ESS.lpDisplayName);
finally
CloseServiceHandle(SvcHandle);
end;
end;
procedure TServiceForm1.GetServices(ServiceType, ServiceState: Cardinal);
var // ; Services: TStrings
SvcHandle: SC_HANDLE;
ListItem :TListItem;
ErrStr: String;
ESS: Array of TEnumServiceStatus;
nBytesNeeded,nServices,nResumeHandle: Cardinal;
i: Integer;
s:string;
begin
// if not Assigned(Services) then
// Exit;
SvcHandle:=OpenSCManager(Nil,Nil,SC_MANAGER_ENUMERATE_SERVICE);
if SvcHandle=0 then
begin
if GetLastError = ERROR_ACCESS_DENIED then
ErrStr:='The requested access was denied'
else if GetLastError = ERROR_DATABASE_DOES_NOT_EXIST then
ErrStr:='The specified database does not exist'
else if GetLastError = ERROR_INVALID_PARAMETER then
ErrStr:='A parameter that was specified is invalid'
else
ErrStr:='OpenSCManager failed';
Raise Exception.Create(ErrStr);
Exit;
end;
nResumeHandle:=0;
try
EnumServicesStatus(SvcHandle,ServiceType,ServiceState,ESS[0],0,
nBytesNeeded,nServices,nResumeHandle);
SetLength(ESS,(nBytesNeeded div SizeOf(TEnumServiceStatus)+1));
if not EnumServicesStatus(SvcHandle,ServiceType,ServiceState,ESS[0],nBytesNeeded,
nBytesNeeded,nServices,nResumeHandle) then
Exit;
for i:=0 to nServices-1 do
begin
// Services.Add(ESS.lpServiceName);
ListItem:=ListV.items.add;
ListItem.caption:=ESS.lpDisplayName;
s:=ListItem.caption;
if GetSqlServerStatus(s) then
//ESS.lpServiceName;
ListItem.subitems.add('启动');
end;
//Services.Add(ESS.lpDisplayName);
finally
CloseServiceHandle(SvcHandle);
end;
end;