EnumServicesStatus
Mark Meyer mark@logs.com
Thu, 9 Nov 2000 13:37:35 -0600
Previous message: Interfaces
Next message: EnumServicesStatus
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--------------------------------------------------------------------------------
> Anyone got this to work ?
>
> I have some problems obtaining the buffer ... propably some
> pointer stuff
gosh - this subject seems to come up every 3 weeks on one of the delphi
related lists..
here is a component based version of this ever popular and ubiquitous code:
hth
mark
===================== code follows ========================================
(*
this code is the property of Mark Meyer and LOGS Financial Service
Inc., Northbrook, Illinois
Copyright October 2000.
this code is contributed to the Delphi developer community. it is
submitted for
public use "as is" and is meant to provide a basis on which others can
learn and build.
i originally found this code out on the Borland Win32 newsgroup written
for D3 and rewrote it
as a component using D5.
if this code and component is of any benefit to you - please "keep the
wheel turning" by
helping others when and where you can in the programming community.
if you have any questions please contact me.
thx
mark meyer
wk: markm.hq@logs.com
hm: geeky2@gte.net
*)
unit NTServiceList;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
WinSvc;
type
PMyServices =^TMyServices;
TMyServices = array[0..255] of TEnumServiceStatus;
type
TNTServiceList = class(TComponent)
private
{ Private declarations }
FServiceList : TStringlist;
FServer : string;
FSCManagerHandle : HWND;
function Display_status(status_code:DWORD):string;
function GetEnumPriv : boolean;
function GetServer : string;
function GetServiceList : tstringlist;
procedure SetServer (value : string);
procedure DoServiceList;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Server : string read GetServer write SetServer;
property ServiceList : tstringlist read GetServiceList;
end;
procedure Register;
const
SERVICE_WIN32_OWN_PROCESS = $00000010;
SERVICE_WIN32_SHARE_PROCESS = $00000020;
SERVICE_WIN32 = (SERVICE_WIN32_OWN_PROCESS or
SERVICE_WIN32_SHARE_PROCESS);
SERVICE_ACTIVE = 1;
SERVICE_INACTIVE = 2;
SERVICE_STATE_ALL = SERVICE_ACTIVE + SERVICE_INACTIVE;
implementation
constructor TNTServiceList.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FServiceList := tstringlist.Create;
// DoServiceList;
end;
destructor TNTServiceList.Destroy;
begin
FServiceList.destroy;
inherited destroy;
end;
function TNTServiceList.GetServiceList : TStringlist;
begin
DoServiceList;
result := FServiceList;
end;
procedure Register;
begin
RegisterComponents('Samples', [TNTServiceList]);
end;
function TNTServiceList.GetServer : string;
begin
result := FServer;
end;
procedure TNTServiceList.SetServer(value : string);
begin
if (value <> FServer) then
FServer := value;
end;
function TNTServiceList.GetEnumPriv : boolean;
var
htoken : thandle;
tkp : ttokenprivileges;
p : ttokenprivileges;
retlen : dword;
reply : dword;
begin
result := false;
if openprocesstoken(getcurrentprocess, TOKEN_ADJUST_PRIVILEGES OR
TOKEN_QUERY, htoken) then begin
if
lookupprivilegevalue(nil,'seshutdownprivilege',tkp.privileges[0].luid) then
begin
tkp.privilegecount := 1;
tkp.Privileges[0].attributes := SE_PRIVILEGE_ENABLED;
adjusttokenprivileges(htoken,false,tkp,sizeof(tkp),p,retlen);
reply := getlasterror;
if reply = error_success then begin
result := true;
end;{if}
end;{if}
end;{if}
end;
procedure TNTServiceList.DoServiceList;
var
ResumeHandle: DWORD;
Buff : Integer;
BytesNeeded : DWORD;
NumberOfServices : DWORD;
x :Integer;
MyPointer: PMyServices;
Re: Boolean;
displayname : string;
currentstate : DWORD;
display_currentstate : string;
servicename : string;
servername : string;
begin
screen.cursor:=crHourglass;
new(MyPointer);
try
FSCManagerHandle :=0;
ResumeHandle:=0;
Buff:=4048;
Bytesneeded:=0;
Numberofservices:=0;
if (getenumpriv) then begin
FSCManagerHandle :=
openscmanager(pchar(FServer),nil,SC_MANAGER_ALL_ACCESS);
end{if}
else begin
exit;
end;
if FSCManagerHandle <> 0 then
re:=EnumServicesStatus(FSCManagerHandle,
SERVICE_WIN32,
SERVICE_STATE_ALL,
MyPointer^[0],
Buff,
Bytesneeded,
NumberOfServices,
ResumeHandle);
FServiceList.clear;
for x := 0 to NumberofServices - 1 do begin
displayname := StrPas(MyPointer^[x].lpDisplayName);
servicename := strpas(mypointer^[x].lpservicename);
currentstate := mypointer^[x].servicestatus.dwcurrentstate;
display_currentstate := display_status(currentstate);
FServiceList.Add(servicename + ' : ' +
display_status(currentstate));
end;{for}
finally{try-finally}
dispose(MyPointer);
end;
screen.cursor:=crDefault;
end;
function TNTServiceList.Display_status(status_code:DWORD):string;
var
temp : string;
begin
case status_code of
SERVICE_STOPPED : temp := 'STOPPED';
SERVICE_START_PENDING : temp :=
'START_PENDING';
SERVICE_STOP_PENDING : temp :=
'STOP_PENDING';
SERVICE_RUNNING : temp := 'RUNNING';
SERVICE_CONTINUE_PENDING : temp :=
'CONTINUE_PENDING';
SERVICE_PAUSE_PENDING : temp :=
'PAUSE_PENDING';
SERVICE_PAUSED : temp := 'PAUSED';
end;{case}
result := temp;
end;
end.
--------------------------------------------------------------------------------
Previous message: Interfaces
Next message: EnumServicesStatus
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]