System Delphi

uses ShellApi, WinSvc;
// ServiceName isimli servisi sistemden kaldırır.
function RemoveService(const ServiceName: string): Boolean;
var
SCM: SC_HANDLE;
ServiceHandle: SC_HANDLE;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM = 0 then
raise Exception.Create('Servis Kontrol Yöneticisi açılamadı.');
ServiceHandle := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if ServiceHandle = 0 then
begin
CloseServiceHandle(SCM);
raise Exception.Create(ServiceName + ' isimli servis açılamadı. Hata: ' + SysErrorMessage(getLastError));
end;
if not DeleteService(ServiceHandle) then
raise Exception.Create(ServiceName + ' isimli servis silinemedi. Hata: ' + SysErrorMessage(getLastError));
CloseServiceHandle(SCM);
end;
// Sisteme yeni servis yükler
function InstallService(const ServiceName, DisplayName, BinaryFile: string; dwStartType: DWord): Boolean;
var
SCM: SC_HANDLE;
ServiceHandle: SC_HANDLE;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM = 0 then
raise Exception.Create('Servis Kontrol Yöneticisi açılamadı.');
ServiceHandle := CreateService(SCM, PChar(ServiceName), PChar(DisplayName), SERVICE_CHANGE_CONFIG,
SERVICE_WIN32_OWN_PROCESS, dwStartType, SERVICE_ERROR_NORMAL, PChar(BinaryFile), nil, nil, nil, nil, nil);
if ServiceHandle = 0 then
begin
CloseServiceHandle(SCM);
raise Exception.Create(ServiceName + ' isimli servis yüklemesi başarısız. Sistem hatası: ' + SysErrorMessage(GetLastError));
end;
CloseServiceHandle(SCM);
CloseServiceHandle(ServiceHandle);
Result := True;
end;
// Servisi başlatır
function StartNTService(const ServiceName: string): Boolean;
var
SCM: SC_HANDLE;
ServiceHandle: SC_HANDLE;
Res: Boolean;
Temp: PChar;
Status: SERVICE_STATUS;
Err: Integer;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM = 0 then
raise Exception.Create('Servis Kontrol Yöneticisi açılamadı. Hata: ' + SysErrorMessage(getLastError));
ServiceHandle := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if ServiceHandle = 0 then
begin
CloseServiceHandle(SCM);
raise Exception.Create(ServiceName + ' isimli servis açılamadı. Hata: ' + SysErrorMessage(getLastError));
end;
Res := StartService(ServiceHandle, 0, Temp);
if Res then
begin
Res := QueryServiceStatus(ServiceHandle, Status);
while (Res) do
begin
if Status.dwCurrentState = SERVICE_RUNNING then
begin
Result := True;
Break;
end else if Status.dwCurrentState = SERVICE_STOPPED then
begin
Result := False;
Break;
end;
QueryServiceStatus(ServiceHandle, Status);
end;
end else begin
Err := GetLastError;
if Err <> ERROR_SERVICE_ALREADY_RUNNING then
raise Exception.Create(ServiceName + ' isimli servis başlatılamadı. Hata:' + SysErrorMessage(Err));
end;
CloseServiceHandle(SCM);
if not Result then
raise Exception.Create(ServiceName + ' isimli servis başlatılamadı');
end;
// Servisi durdurur
function StopNTService(const ServiceName: string): Boolean;
var
SCM: SC_HANDLE;
ServiceHandle: SC_HANDLE;
Res: Boolean;
Status: SERVICE_STATUS;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM = 0 then
raise Exception.Create('Servis Kontrol Yöneticisi açılamadı');
ServiceHandle := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if ServiceHandle = 0 then
begin
CloseServiceHandle(SCM);
raise Exception.Create(ServiceName + ' isimli servis açılamadı. Hata: ' + SysErrorMessage(getLastError));
end;
Res := ControlService(ServiceHandle, SERVICE_CONTROL_STOP, Status);
if not Res then
begin
if GetLastError <> ERROR_SERVICE_NOT_ACTIVE then
raise Exception.Create('Servis durdurulamadı');
end else Result := True;
end;
// Servis durumu hakkında bilgi verir. y.n. çalışıyor, durdurulmuş vb.
function GetServiceStatus(
const ServiceName: string): SERVICE_STATUS;
var
SCM: SC_HANDLE;
ServiceHandle: SC_HANDLE;
begin
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM = 0 then
raise Exception.Create('HATA');
ServiceHandle := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if ServiceHandle = 0 then
begin
CloseServiceHandle(SCM);
case GetLastError of
ERROR_ACCESS_DENIED: raise Exception.Create('Yetersiz ayrıcalıklar');
ERROR_INVALID_HANDLE: raise Exception.Create('Geçersiz Handle');
ERROR_INVALID_NAME:raise Exception.Create('Servis Bulunamadı');
ERROR_SERVICE_DOES_NOT_EXIST: raise Exception.Create('Servis bulunamadı');
end;
end;
if not QueryServiceStatus(ServiceHandle, Result) then
raise Exception.Create('Durum Alınamadı');
end;