Récupérer le type de démarrage d'un service
Salut,
J'essaye de récupere le type de démarrage d'un service.
Voila mon code :
WinSvc.pas
Code:
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
|
//
// Query Service Configuration Structure
//
PQueryServiceConfigA = ^TQueryServiceConfigA;
PQueryServiceConfigW = ^TQueryServiceConfigW;
PQueryServiceConfig = PQueryServiceConfigA;
{$EXTERNALSYM _QUERY_SERVICE_CONFIGA}
_QUERY_SERVICE_CONFIGA = record
dwServiceType: DWORD;
dwStartType: DWORD;
dwErrorControl: DWORD;
lpBinaryPathName: PAnsiChar;
lpLoadOrderGroup: PAnsiChar;
dwTagId: DWORD;
lpDependencies: PAnsiChar;
lpServiceStartName: PAnsiChar;
lpDisplayName: PAnsiChar;
end;
{$EXTERNALSYM _QUERY_SERVICE_CONFIGW}
_QUERY_SERVICE_CONFIGW = record
dwServiceType: DWORD;
dwStartType: DWORD;
dwErrorControl: DWORD;
lpBinaryPathName: PWideChar;
lpLoadOrderGroup: PWideChar;
dwTagId: DWORD;
lpDependencies: PWideChar;
lpServiceStartName: PWideChar;
lpDisplayName: PWideChar;
end;
{$EXTERNALSYM _QUERY_SERVICE_CONFIG}
_QUERY_SERVICE_CONFIG = _QUERY_SERVICE_CONFIGA;
{$EXTERNALSYM QUERY_SERVICE_CONFIGA}
QUERY_SERVICE_CONFIGA = _QUERY_SERVICE_CONFIGA;
{$EXTERNALSYM QUERY_SERVICE_CONFIGW}
QUERY_SERVICE_CONFIGW = _QUERY_SERVICE_CONFIGW;
{$EXTERNALSYM QUERY_SERVICE_CONFIG}
QUERY_SERVICE_CONFIG = QUERY_SERVICE_CONFIGA;
TQueryServiceConfigA = _QUERY_SERVICE_CONFIGA;
TQueryServiceConfigW = _QUERY_SERVICE_CONFIGW;
TQueryServiceConfig = TQueryServiceConfigA; |
Ma fonction
Code:
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
|
// retourne le type de démarrage du service
function ServiceGetStartType( sMachineName, sServiceName : string) : DWord;
var
schm : SC_Handle;
schs : SC_Handle;
qsc : TQueryServiceConfig;
dwNeededBytes : DWord;
begin
// initialisation
Result := 0;
schs := 0;
// se connecte au service control manager
schm := OpenSCManager( PChar(sMachineName), Nil, SC_MANAGER_CONNECT);
try
// si la connection a réussie
if schm > 0 then
begin
// ouvre un handle sur le service en question afin de recuperer sa config
schs := OpenService( schm, PChar(sServiceName), SERVICE_QUERY_CONFIG);
// si la connection au service a réussie
if schs > 0 then
begin
// recupere le type de démarrage du service
dwNeededBytes := 0;
if QueryServiceConfig( schs, @qsc, sizeof(qsc), dwNeededBytes) then
begin
Result := qsc.dwStartType;
end
else
begin
// si échec, envoie une exception
case GetLastError of
ERROR_ACCESS_DENIED : Raise Exception.Create('ERROR_ACCESS_DENIED');
ERROR_INSUFFICIENT_BUFFER : Raise Exception.Create('ERROR_INSUFFICIENT_BUFFER : ' + IntToStr(dwNeededBytes));
ERROR_INVALID_HANDLE : Raise Exception.Create('ERROR_INVALID_HANDLE');
else
Raise Exception.Create( Format(rsUnableToGetServiceStartType, [sServiceName]));
end;
end;
end
else
begin
// envoie une exception
Raise Exception.Create( Format(rsUnableToOpenService, [sServiceName]));
end;
end
else
begin
// envoie une exception
if sMachineName = '' then
begin
Raise Exception.Create( Format(rsUnableToConnectToSCM, [rsLocalMachine]));
end
else
begin
Raise Exception.Create( Format(rsUnableToConnectToSCM, [sMachineName]));
end;
end;
finally
if schs > 0 then
begin
// ferme le handle du service
CloseServiceHandle(schs);
end;
if schm > 0 then
begin
// ferme le handle du control manager
CloseServiceHandle(schm);
end;
end;
end; |
Je tombe systématiquement en exception avec l'erreur ERROR_INSUFFICIENT_BUFFER
la valeur de dwNeededBytes est 200.
Est-ce que quelq'un a une idée pour résoudre ce problème?
Merci.