How to delete (unregister) services?
This page contains the two examples of how to unregister (uninstall) Windows services. They both produce the same result. The first example is based on TWmiQuery and TWmiConnection components from WmiSet component collection. The second example utilizes TNTServiceManager component from NTSet component collection. There are minor differences in the examples:
  • WmiSet components better handle the connection to the remote host. They use the WMI connection capabilities. NTSet components use "null session" method, which may fail if the current user already has a connection to destination host with different credentials.
  • NTSet components use pure API and work little bit faster.
The source code below creates and destroys the components on the fly, so these functions may be used in console type applications. With minor changes the functions may be adapted for UI-type apps.

Please note that service applications created with Delphi or C++Builder can unregister themselves. Simply run them with "/uninstall" parameter. This utility can unregisters any service.

How to unregister (uninstall) services with WmiSet?  Download

// this procedure unregisters the Windows Service specified by name.
// It may be used for local or remote host. When used for local host 
// TargetHost, UserName, Password parameters must be empty. 
// For remote computer the parameters must be: 
// TargetHost: name or IP address of the remote host, like '10.8.36.54' 
// UserName: name of the user, may include domain, like 'MOON\Administrator'; 
// Password: the user's password. 
// ServiceName: name of the service to unregister (delete). 
// The function returns zero on success, error code on error. 
function UnregisterServices(TargetHost, UserName, Password, 
                            ServiceName: string): integer;
const
  WQL = 'select * from Win32_Service where name = ''%s''';
var
  Connection: TWmiConnection;
  Query: TWmiQuery;
  Method: TWmiMethod;
  s: string;
begin
  Result := -1;
  Method := TWmiMethod.Create(nil);
  Query := TWmiQuery.Create(nil);
  Connection := TWmiConnection.Create(nil);
  try
    Query.Connection := Connection;
    Query.WQL.Text := Format(WQL, [ServiceName]);
    Method.WmiObjectSource := Query;
    Connection.MachineName := TargetHost;
    Connection.Credentials.UserName := UserName;
    Connection.Credentials.Password := Password;
    try
      // exception may happen on Win9x, WinNT if WMI core is not installed;
      // The provided credentials may also be invalid. 
      Connection.Connected := true;
    except
      writeln('Cannot connect to the destination host');
      Exit;
    end;

    Query.Open;
    if Query.EOF then
    begin
      writeln('Service not found: '+ServiceName);
      Exit;
    end;

    Method.WmiMethodName := 'Delete';
    Result := Method.Execute;
    if Result <> 0 then
    begin
      s := Method.LastWmiErrorDescription;
      if s = '' then s := 'Error '+ IntToStr(Method.LastWmiError);
      writeln(s);
    end;
  finally
    Method.Free;
    Query.Free;
    Connection.Free;
  end;
end;
How to unregister (uninstall) services with NTSet?  Download

// this procedure unregisters the Windows Service specified by name.
// It may be used for local or remote host. When used for local host 
// TargetHost, UserName, Password parameters must be empty. 
// For remote computer the parameters must be: 
// TargetHost: name or IP address of the remote host, like '10.8.36.54' 
// UserName: name of the user, may include domain, like 'MOON\Administrator'; 
// Password: the user's password. 
// ServiceName: name of the service to unregister (delete). 
// The function returns zero on success, error code on error. 
function UnregisterServices(TargetHost, UserName, Password, 
                            ServiceName: string): integer;
var
  ServiceMan: TNTServiceManager;
begin
  ServiceMan := TNTServiceManager.Create(nil);
  try
    ServiceMan.LogonAs.UserName := UserName;
    ServiceMan.LogonAs.Password := Password;
    if TargetHost <> '' then ServiceMan.MachineName := '\\'+ TargetHost;

    if (ServiceMan.MachineName <> '') and (ServiceMan.ConnectIPC <> 0) then
      writeln('Warning: could not use the provided credentials.');

    ServiceMan.ServiceName := ServiceName;
    try
      ServiceMan.ActiveManager := true;
      ServiceMan.ActiveService := true;
      ServiceMan.DeleteService;
      Result := 0
    except
      On e: ENTException do
      begin
        Result := e.ErrorCode;
        writeln(e.message)
      end;
    end;
  finally
    ServiceMan.Free;
  end;
end;