Управление службами в Windows XP

16:51
dmdel (10.04.08 16:01) [0]
Здраствуйте.
Как можно с помощью Дельфи 6 отключить какую либо службу, а потом ее включить?
------------------------------------------
Palladin © (10.04.08 16:15) [1]

uses
WinSvc;
 
function vtServiceStart(const p_strComp, p_strServiceName: string): Boolean;
var
hManager, hSvc: SC_Handle;
nSvcStatus: TServiceStatus;
Temp: PChar;
dwCheckPoint: DWord;
begin
ZeroMemory(@nSvcStatus, SizeOf(nSvcStatus));
nSvcStatus.dwCurrentState := 1;
hManager := OpenSCManager(PChar(p_strComp), nil, SC_MANAGER_CONNECT);
if hManager > 0 then
begin
hSvc := OpenService(hManager, PChar(p_strServiceName), SERVICE_START or SERVICE_QUERY_STATUS);
if hSvc > 0 then
begin
Temp := nil;
if StartService(hSvc, 0, temp) then
if QueryServiceStatus(hSvc, nSvcStatus) then
while SERVICE_RUNNING <> nSvcStatus.dwCurrentState do
begin
dwCheckPoint := nSvcStatus.dwCheckPoint;
Sleep(nSvcStatus.dwWaitHint);
if not QueryServiceStatus(hSvc, nSvcStatus) then Break;
if (nSvcStatus.dwCheckPoint < dwCheckPoint) then Break;
end;
CloseServiceHandle(hSvc);
end;
CloseServiceHandle(hManager);
end;
Result := SERVICE_RUNNING = nSvcStatus.dwCurrentState;
end;
 
function vtServiceStop(const p_strMachine, p_strServiceName: string): Boolean;
var
hManager, hSvc: SC_Handle;
nSvcStatus: TServiceStatus;
dwCheckPoint: DWord;
begin
hManager := OpenSCManager(PChar(p_strMachine), nil, SC_MANAGER_CONNECT);
if hManager > 0 then
begin
hSvc := OpenService(hManager, PChar(p_strServiceName), SERVICE_STOP or SERVICE_QUERY_STATUS);
if hSvc > 0 then
begin
if ControlService(hSvc, SERVICE_CONTROL_STOP, nSvcStatus) then
if QueryServiceStatus(hSvc, nSvcStatus) then
while SERVICE_STOPPED <> nSvcStatus.dwCurrentState do
begin
dwCheckPoint := nSvcStatus.dwCheckPoint;
Sleep(nSvcStatus.dwWaitHint);
if not QueryServiceStatus(hSvc, nSvcStatus) then Break;
if nSvcStatus.dwCheckPoint < dwCheckPoint then Break;
end;
CloseServiceHandle(hSvc);
end;
CloseServiceHandle(hManager);
end;
Result := SERVICE_STOPPED = nSvcStatus.dwCurrentState;
end;
 
function vtServiceGetStatus(const p_strMachine, p_strService: string): DWord;
var
hManager, hSvc: SC_Handle;
nSvcStatus: TServiceStatus;
hStat: DWord;
begin
hStat := 1;
hManager := OpenSCManager(PChar(p_strMachine), nil, SC_MANAGER_CONNECT);
if hManager > 0 then
begin
hSvc := OpenService(hManager, PChar(p_strService), SERVICE_QUERY_STATUS);
if hSvc > 0 then
begin
if QueryServiceStatus(hSvc, nSvcStatus) then hStat := nSvcStatus.dwCurrentState;
CloseServiceHandle(hSvc);
end;
CloseServiceHandle(hManager);
end;
Result := hStat;
end;

-------------------------------------------------------
dmdel (10.04.08 16:23) [2]
Спаисбо
-------------------------------------------------------
Palladin © (10.04.08 16:28) [3]
Пожаулста
-------------------------------------------------------
© http://www.delphimaster.ru/cgi-bin/forum.pl?id=1207828862&n=18