{取应用程式的版本号程式,如有版本号,返回值为版本号的值,否则返回值为空
返回值的格式为如1.0.0.0 } 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;// ***********************上例在环境中测试过***********************************************/
以下程序在DELPHI7 W2003测试通过:
function GetFileVersion(AFileName: string): string;varn, Len: DWORD;Buf : PChar;Value: Pointer;szName: array [0..255] of Char;Transstring: string;beginLen := GetFileVersionInfoSize(PChar(AFileName), n);if Len > 0 thenbegin 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; 如何用Delphi编程读出一个DLL文件的版本号,请给示例 好,给几个函数还不行,那就给段代码吧: 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;如何获取一个可执行文件的版本号?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;