Wednesday, March 2, 2011

How can I disable a service via Delphi?


You really only need to set the appropriate registry key for the service. The start type is determined by the "Start" keyword in service configuration section of the registry:
HKLM\System\CurrentControlSet\services\YOUR_SVC_NAME\
You'll notice a REG_DWORD called Start. To set this service's startup type to Disabled, change the value of the Start key to 0x4.
Other valid options for the Start key are:
     Boot        0x0
     System      0x1
     Automatic   0X2
     Manual      0x3
     Disabled    0x4
Here's a function to get you started. Note that I have not tested this function, but it should work as-is. You will need to include the Registry unit in your uses clause like this:
uses
   Registry;
// [.........]
function SetSvcStartup(iStartType: integer): Boolean;
var
  Reg: TRegistry;
  Path: string;
begin

  //    Boot        0x0
  //    System      0x1
  //    Automatic   0x2
  //    Manual      0x3
  //    Disabled    0x4

  Reg := TRegistry.Create;
  try
    with Reg do
    begin
      RootKey := HKEY_LOCAL_MACHINE;
      Path := 'System\CurrentControlSet\services\YOUR_SVC_NAME';

      if KeyExists(Path) then
      begin
        OpenKey(Path, True);
        WriteInteger('Start', iStartType);
      end;
    end;
  finally
    Reg.CloseKey;
    Reg.Free;
  end;
end;
------------------------------------------------------------------------------------------------------
To set a service's startup type to "disabled", you call the function like this:
SetSvcStartup(4);

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum