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
| function GetFileVersion(const NameExe: string; out Version: string): boolean;
var
dwVerSize : Integer; // Taille de la structure des données de version de ce fichier
hMem : HGLOBAL; // Memoire Alloué
LpData : LPSTR; // Buffer sécurisé
dwVerHandle : LongWord; // C'est une valeur interne à GetFileVersionInfo...
nStrLong : LongWord; // Longueur des Données
ChaineCC : string; // Chaine temporaire de Format de la fonction VerQueryValue
Lp : Pointer; // Pointeur sur Buffer si les données sont complètes
cStartBuf : array[0..4] of char;
cResBuf : array[0..255] of char; // Chaine de Format de la fonction VerQueryValue
FileInfo : PVSFixedFileInfo;
function GetStringVersion(): Boolean;
begin
// Traduit LpData dans FileInfo
Result := VerQueryValue(LpData, '\', Pointer(FileInfo), nStrLong);
if Result then begin
// Pour comprendre ce Formatage, il fau comprendre la structure Packed PVSFixedFileInfo (Windows.pas)
Version := Format('%d.%d.%d.%d', [ FileInfo.dwFileVersionMS shr 16,
FileInfo.dwFileVersionMS and $FFFF,
FileInfo.dwFileVersionLS shr 16,
FileInfo.dwFileVersionLS and $FFFF]);
end;
end;
begin
Result := False;
// Récupère la Taille de la structure des données de version de ce fichier
dwVerSize := GetFileVersionInfoSize(PChar(NameExe), dwVerHandle);
Version := 'Non Disponible';
if dwVerSize = 0 then
Exit;
// Allocation de la mémoire avec un petit surplus
hMem:= GlobalAlloc(GMEM_ZEROINIT, dwVerSize + 100);
// Fourni un pointeur "non relatif" de la mémoire allouée qui ne peut pas être déplacé par les optimisations de Windows
LpData:= GlobalLock(hMem);
try
// Retourne dans le buffer LpData les données de version de NameExe
GetFileVersionInfo(PChar(NameExe), dwVerHandle, dwVerSize, LpData);
// Détermine les paramètres régionaux sont spécifiques
if not VerQueryValue(LpData,'\VarFileInfo\Translation', Lp, nStrLong) then begin
if GetStringVersion() then
Exit;
end;
// Le format de la version est standard Delphi
try
// Copie une chaîne Pascal dans une chaîne terminée par le caractère Null.
StrPcopy(cStartBuf, PChar(Lp));
// Pour comprendre ce Formatage, il fau comprendre la structure Packed PVSFixedFileInfo (Windows.pas)
ChaineCC := Format('\StringFileInfo\%.2x%.2x%.2x%.2x\', [byte(cStartBuf[1]),
byte(cStartBuf[0]),
byte(cStartBuf[3]),
byte(cStartBuf[2])]);
except
Exit;
end;
// Traduit Lp dans FileInfo selon la Chaine de Format de la fonction VerQueryValue cResBuf
StrCopy(cResBuf, Pchar(ChaineCC + 'FileVersion'));
if VerQueryValue(LpData, cResBuf, Lp, nStrLong) then begin
StrPcopy(cResBuf,PChar(Lp));
// C'est pour le cas si on veut le Copyright StrPas(cResBuf);
Version := StrPas(cResBuf);
Result := True;
end;
// Il n'est pas possible d'utiliser le format régional donc je reprends la méthode normale
if not result then begin
Result := GetStringVersion();
end;
finally
GlobalUnLock(hMem);
GlobalFree(hmem);
end;
end; |
Partager