[delphi] Starting and stopping Windows services
http://www.delphifaq.com/faq/delphi_windows_API/f521_0.htm
Note:
Thanks to Henk Mulder who mailed me about a correction with the assignment to dwCurrentState. I think this code was written originally with Delphi 3 and probably Borland or Microsoft changed something from a signed integer to an unsigned word.
{ Thanks to Andrea Canu for pointing out a problem in this code (fixed now). }
program test;
uses
WinSvc;
{sc-----------------------------------------------------------------------
ServiceStart
sMachine:
machine name, ie: \\SERVER
empty = local machine
Purpose:
start service
return TRUE if successful
-----------------------------------------------------------------------sc}
function ServiceStart(sMachine, sService: String) : Boolean;
var
schm,
schs: SC_Handle;
ss: TServiceStatus;
psTemp: PChar;
dwChkP: DWord;
begin
ss.dwCurrentState := 1; // originally -1, corrected by Henk Mulder
schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
if (schm>0) then
begin
schs := OpenService(schm, PChar(sService), SERVICE_START or
SERVICE_QUERY_STATUS);
if (schs>0) then
begin
psTemp := nil;
if (StartService(schs, 0, psTemp)) then
if (QueryServiceStatus(schs, ss)) then
while (SERVICE_RUNNING<>ss.dwCurrentState) do
begin
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
if (not QueryServiceStatus(schs, ss)) then
Break;
if (ss.dwCheckPoint < dwChkP) then
Break;
end;
CloseServiceHandle(schs);
end;
CloseServiceHandle(schm);
end;
Result := SERVICE_RUNNING=ss.dwCurrentState;
end;
{sc-----------------------------------------------------------------------
ServiceStop
Purpose:
stop a service, parameters as in ServiceStart
-----------------------------------------------------------------------sc}
function ServiceStop(sMachine, sService: String) : Boolean;
var
schm,
schs: SC_Handle;
ss: TServiceStatus;
dwChkP: DWord;
begin
schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
if (schm>0) then
begin
schs := OpenService(schm, PChar(sService), SERVICE_STOP or
SERVICE_QUERY_STATUS);
if (schs>0) then
begin
if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then
if (QueryServiceStatus(schs, ss)) then
while (SERVICE_STOPPED<>ss.dwCurrentState) do
begin
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
if (not QueryServiceStatus(schs, ss)) then
Break;
if (ss.dwCheckPoint < dwChkP) then
Break;
end;
CloseServiceHandle(schs);
end;
CloseServiceHandle(schm);
end;
Result := SERVICE_STOPPED=ss.dwCurrentState;
end;
begin
if (ServiceStart('\\ComputerName', 'alerter')) then
begin
// ..
end;
if (ServiceStop('', 'alerter')) then
begin
// ..
end;
end.
반응형
'Delphi, RadStudio' 카테고리의 다른 글
[개발/delphi] 자기 자신을 지우는 프로그램 만들기(delphi) (0) | 2011.12.12 |
---|---|
[개발/delphi] how to register an OCX (0) | 2011.12.12 |
[개발/delphi] how to have an exe file delete itself (0) | 2011.12.12 |
[개발/delphi] 외부 프로그램 실행하고 기다리기 Execute and wait (0) | 2011.12.12 |
[delphi] Tracking DLL loading, attaching, detaching and unloading (0) | 2011.12.12 |
댓글