delphi 서비스 조작
unit Service;
interface
uses Windows,Messages,SysUtils,Winsvc,Dialogs;
function StartServices(Const SvrName:String):Boolean;
function StopServices(Const SvrName:String):Boolean;
function QueryServiceStatu(Const SvrName: String):String;
function CreateServices(Const SvrName,FilePath:String):Boolean;
function DeleteServices(Const SvrName: String):Boolean;
implementation
//
function StartServices(Const SvrName: String): Boolean;
var
a,b:SC_HANDLE;
c:PChar;
begin
Result:=False;
a:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if a <=0 then Exit;
b:=OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);
if b <=0 then Exit;
try
Result:=StartService(b,0,c);
CloseServiceHandle(b);
CloseServiceHandle(a);
except
CloseServiceHandle(b);
CloseServiceHandle(a);
Exit;
end;
end;
//
function StopServices(Const SvrName: String): Boolean;
var
a,b: SC_HANDLE;
d: TServiceStatus;
begin
Result := False;
a :=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if a <=0 then Exit;
b:=OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);
if b <=0 then Exit;
try
Result:=ControlService(b,SERVICE_CONTROL_STOP,d);
CloseServiceHandle(a);
CloseServiceHandle(b);
except
CloseServiceHandle(a);
CloseServiceHandle(b);
Exit;
end;
end;
//
function QueryServiceStatu(Const SvrName: String): String;
var
a,b: SC_HANDLE;
d: TServiceStatus;
begin
Result := ' ';
a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if a <=0 then Exit;
b := OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);
if b <= 0 then Exit;
try
QueryServiceStatus(b,d);
if d.dwCurrentState = SERVICE_RUNNING then
Result := ' ' //Run
else if d.dwCurrentState = SERVICE_RUNNING then
Result := 'Wait' //Runing
else if d.dwCurrentState = SERVICE_START_PENDING then
Result := 'Wait' //Pause
else if d.dwCurrentState = SERVICE_STOP_PENDING then
Result := ' ' //Pause
else if d.dwCurrentState = SERVICE_PAUSED then
Result := ' ' //Pause
else if d.dwCurrentState = SERVICE_STOPPED then
Result := ' ' //Stop
else if d.dwCurrentState = SERVICE_CONTINUE_PENDING then
Result := 'Wait' //Pause
else if d.dwCurrentState = SERVICE_PAUSE_PENDING then
Result := 'Wait'; //Pause
CloseServiceHandle(a);
CloseServiceHandle(b);
except
CloseServiceHandle(a);
CloseServiceHandle(b);
Exit;
end;
end;
{ }
function CreateServices(Const SvrName,FilePath: String): Boolean;
var
a,b:SC_HANDLE;
begin
Result:=False;
if FilePath ='' then Exit;
a := OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);
if a <= 0 then Exit;
try
b := CreateService(a,PChar(SvrName),
PChar(SvrName),
SERVICE_ALL_ACCESS,
SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
PChar(FilePath),nil,nil,nil,nil,nil);
if b <= 0 then begin
ShowMessage( SysErrorMessage( GetlastError ));
Exit;
end;
CloseServiceHandle(a);
CloseServiceHandle(b);
Result := True;
except
CloseServiceHandle(a);
CloseServiceHandle(b);
Exit;
end;
end;
{ }
function DeleteServices(Const SvrName: String): Boolean;
var
a,b:SC_HANDLE;
begin
Result:=False;
a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if a <= 0 then Exit;
b :=OpenService(a,PChar(SvrName),STANDARD_RIGHTS_REQUIRED);
if b <= 0 then Exit;
try
Result := DeleteService(b);
if not Result then
ShowMessage(SysErrorMessage(GetlastError));
CloseServiceHandle(b);
CloseServiceHandle(a);
except
CloseServiceHandle(b);
CloseServiceHandle(a);
Exit;
end;
end;
end.
:
{ }
StartServices( );
{ }
StopServices( );
{ }
CreateServices( ,exe );
{ }
DeleteServices( );
{ }
string:=QueryServiceStatu( );
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
스마트 포인터(Smart Pointer)의 구현{****************************************************** * * Delphi Smart Pointer class * AutoPtr * Version 0.2 beta * Ya...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.