1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
unit utils_services;
interface
uses
system.classes, system.types, system.sysutils, messages, windows,
vcl.forms, vcl.dialogs, winsvc;
type
DisplayService=(Key,Display);
PNTServices = ^TNTServices ;
TNTServices = array[0..4096] of TEnumServiceStatus;
var
Handle_SCM: SC_Handle; // handle du manager de control des services
Handle_Service: SC_Handle; // handle d'un service
TypeDemarrageSrvCourant : DWord;
Function ConnectToServicesManager(Computer_Name : String;Var AHandle_SCM: SC_Handle):Boolean;
function ServiceGetName(AServiceName : string ;ATypeAffichage:DisplayService) : string;
function ServiceGetKeyName(AServiceDisplayName : string ) : string;
function ServiceGetDisplayName(AServiceKeyName : string ) : string;
Procedure Pause_No_Bloc_Application(Millisenconde : Cardinal);
Function etat_service(AHandle_SCM : SC_Handle; Nom_Service: string): integer;
function lance_service(NomService: string): boolean;
implementation
function lance_service(NomService: string): boolean;
var
NomPC : String;
index: integer;
Status_Service : TServiceStatus;
EtatDuService : Word;
ArrayOfArguments : PChar;
lpBuffer: array[0..MAX_COMPUTERNAME_LENGTH] of char;
nSize: dword;
etat_serv: integer;
begin
Result:=false;
NSize := Length(LpBuffer);
if GetComputerName(lpBuffer,NSize)
then
begin
NomPC := LpBuffer;
if ConnectToServicesManager(NomPC,Handle_SCM)
then
begin
etat_serv:=etat_service(Handle_SCM, NomService);
if etat_serv=-1 then
begin
MessageDlg('Le service n''existe pas', mtError, [mbOK], 0);
Exit;
end;
Case etat_serv of
SERVICE_STOPPED,
SERVICE_PAUSED :
Begin
ArrayOfArguments := nil;
Handle_Service := OpenService(Handle_SCM, Pchar(ServiceGetKeyName(NomService)), SERVICE_INTERROGATE );
Handle_Service := OpenService(Handle_SCM, Pchar(ServiceGetKeyName(NomService)), SERVICE_ALL_ACCESS);
if StartService(Handle_Service, 0, ArrayOfArguments) = false then
MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOK], 0)
else
MessageDlg('Service MariaDB démarré.', mtInformation, [mbOK], 0);
Pause_No_Bloc_Application(1000);
end;
SERVICE_RUNNING :
Begin
Result:=true;
exit;
end;
end; // case of
if etat_service(Handle_SCM, NomService)=SERVICE_RUNNING
then
begin
Result:=true;
end;
end
else
showmessage('Service '+nomservice+' non trouvé');
end
else
showmessage('Nom du PC non trouvé');
end;
Function etat_service(AHandle_SCM : SC_Handle; Nom_Service: string): integer;
Var
Mes_Services : PNTServices;
ResumeHandle,
OctetsNecessaires,
Nombre_Srv : DWord;
i, etat_serv : Integer;
begin
Result:=-1;
Mes_Services:=nil;
try
New(Mes_Services);
Nombre_Srv := 0;
OctetsNecessaires:=0;
ResumeHandle := 0;
if EnumServicesStatus(AHandle_SCM,
SERVICE_WIN32, //Uniquement les services, pas les drivers
SERVICE_STATE_ALL, // Peut importe l'état
@Mes_Services[0],
SizeOf(Mes_Services^),
OctetsNecessaires,
Nombre_Srv,
ResumeHandle)
then
For i := 0 to Nombre_Srv-1 do
begin
if Mes_Services^[i].lpDisplayName=Nom_Service then
begin
result:=Mes_Services^[i].ServiceStatus.dwCurrentState;
exit;
end;
end;
finally
Dispose(Mes_Services);
end;
end;
Procedure Pause_No_Bloc_Application(Millisenconde : Cardinal);
Var H : Cardinal;
Begin
H := GetTickCount;
WHile H+Millisenconde >= GetTickCount do
Application.ProcessMessages;
end;
Function ConnectToServicesManager(Computer_Name : String;Var AHandle_SCM: SC_Handle):Boolean;
Begin
AHandle_SCM:= OpenSCManager(PChar(Computer_Name), nil, SC_MANAGER_ENUMERATE_SERVICE);
//AHandle_SCM:= OpenSCManager(PChar(Computer_Name), nil, SC_MANAGER_ALL_ACCESS);
Result :=AHandle_SCM <> 0;
end;
function ServiceGetName(AServiceName : string ;ATypeAffichage:DisplayService) : string;
Const LongueurMaxNomSvr = 255;
var NomService : Array[0..LongueurMaxNomSvr+1] of Char;
TailleBuffer: DWord;
Erreur: Boolean;
begin
FillChar(NomService,SizeOf(NomService),#0);
TailleBuffer:=LongueurMaxNomSvr+1;
Case ATypeAffichage of
Key : Erreur:=GetServiceKeyName(Handle_SCM,PChar(AServiceName),NomService,TailleBuffer)=False;
Display: Erreur:=GetServiceDisplayName(Handle_SCM,PChar(AServiceName),NomService,TailleBuffer)=False;
end;
if Erreur
then MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOK], 0);
Result :=NomService;
end;
function ServiceGetKeyName(AServiceDisplayName : string ) : string;
begin
Result:=ServiceGetName(AServiceDisplayName,Key);
end;
function ServiceGetDisplayName(AServiceKeyName : string ) : string;
begin
Result:=ServiceGetName(AServiceKeyName,Display);
end; |
Partager