如何获取执行文件的版本信息?(50分)

斗士

Unregistered / Unconfirmed
GUEST, unregistred user!
执行文件的版本信息在文件属性中可以看到,<br>如*.exe文件<br>那么如何获取版本信息,如版本号等<br>用哪一个API函数。
 
GetFileVersionInfo
 
procedure GetBuildInfo(var V1, V2, V3, V4: Word);<br>&nbsp; var<br>&nbsp; &nbsp; VerInfoSize: DWORD;<br>&nbsp; &nbsp; VerInfo: Pointer;<br>&nbsp; &nbsp; VerValueSize: DWORD;<br>&nbsp; &nbsp; VerValue: PVSFixedFileInfo;<br>&nbsp; &nbsp; Dummy: DWORD;<br>&nbsp; begin<br>&nbsp; VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);<br>&nbsp; GetMem(VerInfo, VerInfoSize);<br>&nbsp; GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);<br>&nbsp; VerQueryValue(VerInfo, '/', Pointer(VerValue), VerValueSize);<br>&nbsp; with VerValue^ do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; V1 := dwFileVersionMS shr 16;<br>&nbsp; &nbsp; V2 := dwFileVersionMS and $FFFF;<br>&nbsp; &nbsp; V3 := dwFileVersionLS shr 16;<br>&nbsp; &nbsp; V4 := dwFileVersionLS and $FFFF;<br>&nbsp; &nbsp; end;<br>&nbsp; FreeMem(VerInfo, VerInfoSize);<br>&nbsp; end;<br><br>其中,PChar(ParamStr(0))是你本程序的名字,<br>你可以改写成其他应用程序,用一个OpenDialog得到FileName即可。
 
to pen_qs<br>不明白函数的用法,举个例子,直接得到字符串值。<br>to bubble<br>不知道最后的结果在哪<br>
 
type<br>&nbsp; PFixedFileInfo = ^TFixedFileInfo;<br>&nbsp; TFixedFileInfo = record<br>&nbsp; &nbsp; &nbsp;dwSignature &nbsp; &nbsp; &nbsp; : DWORD;<br>&nbsp; &nbsp; &nbsp;dwStrucVersion &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp;wFileVersionMS &nbsp; &nbsp;: WORD; &nbsp;// 次版本号<br>&nbsp; &nbsp; &nbsp;wFileVersionLS &nbsp; &nbsp;: WORD; &nbsp;// 主版本号<br>&nbsp; &nbsp; &nbsp;wProductVersionMS : WORD; &nbsp;// 建立次数(build)<br>&nbsp; &nbsp; &nbsp;wProductVersionLS : WORD; &nbsp;// 发行次数(release)<br>&nbsp; &nbsp; &nbsp;dwFileFlagsMask &nbsp; : DWORD;<br>&nbsp; &nbsp; &nbsp;dwFileFlags &nbsp; &nbsp; &nbsp; : DWORD;<br>&nbsp; &nbsp; &nbsp;dwFileOS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp;dwFileType &nbsp; &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp;dwFileSubtype &nbsp; &nbsp; : DWORD;<br>&nbsp; &nbsp; &nbsp;dwFileDateMS &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp;dwFileDateLS &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; end; // TFixedFileInfo<br><br>function FileInfo(const FileName :String):TFixedFileInfo;<br>var<br>&nbsp; dwHandle &nbsp; &nbsp; &nbsp; &nbsp;: Cardinal;<br>&nbsp; dwVersionSize &nbsp; : Cardinal;<br>&nbsp; strSubBlock &nbsp; &nbsp; : String;<br>&nbsp; pTemp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Pointer;<br>&nbsp; pData &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Pointer;<br>begin<br>&nbsp; &nbsp;strSubBlock := '/';<br>&nbsp; &nbsp;// 取得文件版本信息的大小<br>&nbsp; &nbsp;dwVersionSize := GetFileVersionInfoSize(PChar(FileName), dwHandle);<br><br>&nbsp; &nbsp;if dwVersionSize &lt;&gt; 0 then &nbsp; begin<br>&nbsp; &nbsp; &nbsp; GetMem(pTemp, dwVersionSize);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//取文件版本信息<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if GetFileVersionInfo(PChar(FileName),dwHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwVersionSize,pTemp) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //查询文件版本信息<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if VerQueryValue(pTemp,PChar(strSubBlock),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pData,dwVersionSize) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := PFixedFileInfo(pData)^;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FreeMem(pTemp);<br>&nbsp; &nbsp; &nbsp; end; // try<br>&nbsp; &nbsp;end; // if dwVersionSize<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp;Ver:TFixedFileInfo;<br>begin<br>&nbsp;Ver:=FileInfo(Application.ExeName); &nbsp; &nbsp; &nbsp; <br>&nbsp;Form1.Caption:=IntToStr(Ver.wFileVersionLS)+'.'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(Ver.wFileVersionMS)+'.'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(Ver.wProductVersionLS)+' Build '+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(Ver.wProductVersionMS);<br>end;<br>
 
to tsueg<br>谢谢你的热心,非常详细,解决问题。<br>想问一下如何更好地学习API函数。<br>以后遇见类似问题如何下手。
 
谢谢各位。
 
顶部