Unit scmDrivers;

{
 Prace s ovladaci pomoci rutin Spravce sluzeb (Service Control Manager)
}

Interface

Function SCMInstalujOvladac (InterniJmeno:WideString; Jmenosouboru:WideString):Boolean;
Function SCMNactiOvladac (InterniJmeno:WideString):Boolean;
Function SCMUvolniOvladac (InterniJmeno:WideString):Boolean;
Function SCMOdinstalujOvladac (InterniJmeno:WideString):Boolean;

Implementation

Uses Windows, WinSvc, SysUtils;

Function SCMInstalujOvladac (InterniJmeno:WideString; Jmenosouboru:WideString):Boolean;
{
 Nainstaluje sluzbu popisujici ovladac. Vse obstara volani CreateService, ktere provede
 potrebne zapisy do registru.
 InterniJmeno - interni jmeno nove vytvarene sluzby.
 JmenoSouboru - cele jmeno souboru ovladace.
}
Var hScm : SC_HANDLE;
    hService : SC_HANDLE;
begin
hScm:=OpenSCManagerW(Nil,Nil,SC_MANAGER_CREATE_SERVICE);
Result:=hScm > 0;
If Result Then
  begin
  hService:= CreateServiceW(hScm,PWideChar(InterniJmeno),PWideChar(InterniJmeno),GENERIC_READ,SERVICE_KERNEL_DRIVER,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,PWideChar(JmenoSouboru),Nil,Nil,Nil,Nil,Nil);
  Result:=hService > 0;
  If Result Then
    CloseServiceHandle(hService);
  CloseServiceHandle (hScm);
  end;
end;

Function SCMNactiOvladac (InterniJmeno:WideString):Boolean;
{
 Nacte ovladac do pameti jadra. Provadi se stejne jako spousteni obycejne sluzby - pomoci
 volani StartService.
 InterjniJmeno - interni jmeno sluzby popisujici ovladac.
}
Var
  hScm : SC_HANDLE;
  hService : SC_HANDLE;
  Dummy : PWideChar;
begin
hScm:=OpenSCManagerW(Nil,Nil,SC_MANAGER_CONNECT);
Result:=hScm > 0;
If Result Then
  begin
  hService:=OpenServiceW(hScm,PWideChar(InterniJmeno),SERVICE_START);
  Result:=hService > 0;
  If Result Then
    begin
    Result:=StartServiceW(hService,0,Dummy);
    CloseServiceHandle(hService);
    end;
  CloseServiceHandle(hScm);
  end;
end;

Function SCMUvolniOvladac (InterniJmeno:WideString):Boolean;
{
 Pokusi se uvolnit ovladac z jadra stejne jako se zastavuje beh obycejne sluzby. K tomu se
 pouziva volani StopService.
 InterniJmeno - interni jmeno sluzby ovladace.
}
Var
  hScm : SC_HANDLE;
  hService : SC_HANDLE;
  ServiceStatus : SERVICE_STATUS;
begin
hScm:=OpenSCManagerW(Nil,Nil,SC_MANAGER_CONNECT);
Result:=hScm > 0;
If Result Then
  begin
  hService:=OpenServiceW(hScm,PWideChar(InterniJmeno),SERVICE_STOP);
  Result:=hService > 0;
  If Result Then
    begin
    Result:=ControlService(hService,SERVICE_CONTROL_STOP,ServiceStatus);
    CloseServiceHandle(hService);
    end;
  CloseServiceHandle(hScm);
  end;
end;

Function SCMOdinstalujOvladac (InterniJmeno:WideString):Boolean;
{
 Odstrani zaznam o sluzbe ovladace z registru pomoci rutiny DeleteService. Ovladace, ktere
 nemaji zapis v registru, nepreziji restart pocitace.
 InterniJmeno - interni jmeno sluzby, ktera se ma odstranit.
}
Var
  hScm : SC_HANDLE;
  hService : SC_HANDLE;
begin
hScm:=OpenSCManagerW(Nil,Nil,SC_MANAGER_CONNECT);
Result:=hScm > 0;
If Result Then
  begin
  hService:=OpenServiceW(hScm,PWideChar(InterniJmeno),SERVICE_ALL_ACCESS);
  Result:=hService > 0;
  If Result Then
    begin
    Result:=DeleteService(hService);
    CloseServiceHandle(hService);
    end;
  CloseServiceHandle(hScm);
  end;
end;

End.

