How can I disable a service via Delphi?
I use a routine that can st开发者_运维知识库art and stop services via Delphi but I also need to be able to disable them, is it possible?
Open the service with OpenService
, and then disable it by passing Service_Disabled
as the dwStartType
parameter for ChangeServiceConfig
. Specify a null pointer or Service_No_Change
for the rest of the parameters since you're not interested in changing them.
You can use file JclSvcCtrl.pas from JEDI Components Library (JCL). I have written a pseudo example that you could use. However, be aware that I didn't test it. But in this way it should work (error checks omitted):
M := TJclSCManager.Create;
M.Refresh(true); //Not sure if true is needed or not (refresh all services)
For i := 0 to M.ServiceCount -1 do
begin
S := M.Services[i]; //TJclNtService
if CompareText(S.ServiceName, 'bla') then
begin
S.Stop;
S.StartType := sstDisabled;
S.Commit;
break;
end;
end;
Besides using the previous methods, if you need more control you can use WMI.
With Win32_Service class have access to all information of the services installed on the machine and you can has access to methods: Start, Stop, Pause, Resume, Interrogate, Create, Delete, Change, ChangeStartMode...
Here (Web / SourceForge)you can find a set of components to work with WMI (GLibWMI components Library); There are one called CServiceInfo thah give you all information and some methods of this class.
In addition with the package tere are some demos; One is called (ServiceControl) and implement all methods.
All the package are source included. See the code it can be usefull for you.
Regards.
ShellExecute(0, nil, 'cmd.exe', 'sc config "the service name" start=disabled', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', 'sc config "the service name" start=auto', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', 'sc config "the service name" start=demand', nil, SW_HIDE);
This is what i use
It's just a little wrapper around some Windows API Functions we found useful to handle NT-Services. It allows you to query, start, stop, pause and enable/disable NT-Services on the local or a remote system.
http://blog.marcduerst.com/post/How-to-use-TServiceManager-to-manage-Windows-services.aspx
Which lets you write 'nice' delphi code ;)
procedure DisableService(ServiceName: PChar);
var SM: TServiceManager;
begin
SM:=TServiceManager.Create;
try
SM.Connect;
SM.OpenServiceConnection(ServiceName);
//not working with TServiceManager as is
//but its easy to fix, see below
SM.DisableService;
finally
SM.Free;
end;
end;
the DisableService section hasnt been written, but all that is needed is
procedure TServiceManager.DisableService;
begin
ChangeServiceConfig(ServiceHandle, SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_NO_CHANGE, nil, nil, nil, nil, nil, nil, nil);
end;
An other option is to use the DelphiVault ServiceManager unit (see also this answer):
var
SM: TServiceManager;
Svc: TServiceInfo;
begin
SM := TServiceManager.Create;
try
SM.Active := True;
Svc := SM.ServiceByName['MyServiceName'];
Svc.SetStartType(ssDisabled);
finally
SM.Free;
end;
end;
精彩评论