在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
获取指定文件的版本号 方式一: function GetFileVersion(FileName: string): string;
type
PVerInfo = ^TVS_FIXEDFILEINFO;
TVS_FIXEDFILEINFO = record
dwSignature: longint;
dwStrucVersion: longint;
dwFileVersionMS: longint;
dwFileVersionLS: longint;
dwFileFlagsMask: longint;
dwFileFlags: longint;
dwFileOS: longint;
dwFileType: longint;
dwFileSubtype: longint;
dwFileDateMS: longint;
dwFileDateLS: longint;
end;
var
ExeNames: array[0..255] of char;
zKeyPath: array[0..255] of Char;
VerInfo: PVerInfo;
Buf: pointer;
Sz: word;
L, Len: Cardinal;
begin
StrPCopy(ExeNames, FileName);
Sz := GetFileVersionInfoSize(ExeNames, L);
if Sz=0 then
begin
Result:='';
Exit;
end;
try
GetMem(Buf, Sz);
try
GetFileVersionInfo(ExeNames, 0, Sz, Buf);
if VerQueryValue(Buf, '\', Pointer(VerInfo), Len) then
begin
Result := IntToStr(HIWORD(VerInfo.dwFileVersionMS)) + '.' +
IntToStr(LOWORD(VerInfo.dwFileVersionMS)) + '.' +
IntToStr(HIWORD(VerInfo.dwFileVersionLS)) + '.' +
IntToStr(LOWORD(VerInfo.dwFileVersionLS));
end;
finally
FreeMem(Buf);
end;
except
Result := '-1';
end;
end;
方式二: function GetFileVersion(AFileName: string): string;
var
n, Len: DWORD;
Buf : PChar;
Value: Pointer;
szName: array [0..255] of Char;
Transstring: string;
begin
Len := GetFileVersionInfoSize(PChar(AFileName), n);
if Len > 0 then
begin
Buf := AllocMem(Len);
if GetFileVersionInfo(Pchar(AFileName), n, Len, Buf) then
begin
Value := nil;
VerQueryValue(Buf, '\VarFileInfo\Translation', Value, Len);
if Value <> nil then
Transstring := IntToHex(MakeLong(HiWord(LongInt(Value^)),
LoWord(LongInt(Value^))),8);
StrPCopy(szName, '\stringFileInfo\' + Transstring + '\FileVersion');
if VerQueryValue(Buf, szName, Value, Len) then
Result := StrPas(Pchar(Value));
end;
FreeMem(Buf, n);
end;
end;
按钮 代码示例: procedure TForm1.Button1Click(Sender: TObject);
var
dwVerInfoSize, dwVerHnd: DWORD;
hMem: THandle;
lpvMem, lszVer: Pointer;
szGetName: array[0..255] of Char;
cchVer: UINT;
fRet: Boolean;
vsffi: VS_FIXEDFILEINFO;
begin
dwVerInfoSize := GetFileVersionInfoSize('Shell32.dll', dwVerHnd);
if Boolean(dwVerInfoSize) then
begin
hMem := GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem := GlobalLock(hMem);
GetFileVersionInfo('Shell32.dll', dwVerHnd, dwVerInfoSize, lpvMem);
StrCopy(szGetName, '\');
lszVer := nil;
fRet := VerQueryValue(lpvMem, szGetName, lszVer, cchVer);
if fRet and Boolean(cchVer) and Boolean(lszVer) then
begin
vsffi := VS_FIXEDFILEINFO(lszVer^);
ShowMessage('文件版本: ' +
IntToStr(HIWORD(vsffi.dwFileVersionMS)) + '.' +
IntToStr(LOWORD(vsffi.dwFileVersionMS)) + '.' +
IntToStr(HIWORD(vsffi.dwFileVersionLS)) + '.' +
IntToStr(LOWORD(vsffi.dwFileVersionLS)));
end;
GlobalUnlock(hMem);
GlobalFree(hMem);
end;
end;
方式3: function GetFileVersion(fn: string; var ma, mi, r ,b: integer):boolean;//得到本程序的版本号
var
buf, p: pChar;
sver: ^VS_FIXEDFILEINFO ;
i: LongWord;
begin
i:= GetFileVersionInfoSize(pchar(fn), i);
new(sver);
p:= pchar(sver);
GetMem(buf, i);
ZeroMemory(buf, i);
result:= false;
if GetFileVersionInfo(pchar(fn), 0, 4096, pointer(buf)) then
if VerQueryValue(buf, '\', pointer(sver), i) then begin
ma:= sVer^.dwFileVersionMS shr 16;
mi:= sver^.dwFileVersionMS and $0000ffff;
r:= sver^.dwFileVersionLS shr 16;
b:= sver^.dwFileVersionLS and $0000ffff;
result:= true;
end;
Dispose(p);
FreeMem(buf);
end;
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论