关于获取文件的版本信息。(50分)

  • 主题发起人 主题发起人 Eddy
  • 开始时间 开始时间
E

Eddy

Unregistered / Unconfirmed
GUEST, unregistred user!
var<br>&nbsp; dwHandle,dwInfoSize : DWORD;<br>&nbsp; InfoBuffer : Array of Char;<br>&nbsp; pInfoVal : Array of Byte;<br>&nbsp; asVal : AnsiString;<br>&nbsp; InfoValSize : UINT;<br>begin<br>&nbsp; dwInfoSize := GetFileVersionInfoSize(PChar(Application.ExeName),dwHandle);<br>&nbsp; if dwInfoSize &lt;&gt; 0 then begin<br>&nbsp; &nbsp; SetLength(InfoBuffer,dwInfoSize);<br>&nbsp; &nbsp; if GetFileVersionInfo(PChar(Application.ExeName),0,dwInfoSize,@InfoBuffer) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp;VerQueryValue(InfoBuffer,'//VarFileInfo//Translation',pInfoVal,InfoValSize);<br>&nbsp; &nbsp; &nbsp; &nbsp; asVal := '//StringFileInfo//' + IntToHex(^((Word ^)pchInfoVal),4) + IntToHex(^((Word ^)@pchInfoVal[2]),4) + '//FileVersion//';<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>以上是我用来获取可执行文件的版本信息的一段程序,其他的错误暂且不说<br>当程序编译到VerQueryValue这个函数时总说参数类型不对:<br>“Types of actual and formal var parameters must be identical”<br>谁能帮我解决这个问题?
 
"//",C才用,delphi用“/"就可以了。
 
"//'的确不对,不过这里的错误不是"//",下面是我些的寒暑:<br><br>function GetVersion(filename:string);<br>var<br>&nbsp; InfoSize, Wnd: DWORD;<br>&nbsp; VerBuf: Pointer;<br>&nbsp; szName: array[0..255] of Char;<br>&nbsp; Value: Pointer;<br>&nbsp; Len: UINT;<br>&nbsp; TransString:string;<br>begin<br><br>&nbsp; &nbsp; InfoSize := GetFileVersionInfoSize(PChar(FileName), Wnd);<br>&nbsp; &nbsp; if InfoSize &amp;lt;&amp;gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; GetMem(VerBuf, InfoSize);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; if GetFileVersionInfo(PChar(FileName), Wnd, InfoSize, VerBuf) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value :=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VerQueryValue(VerBuf, '/VarFileInfo/Translation', Value, Len);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Value &amp;lt;&amp;gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TransString := IntToHex(MakeLong(HiWord(Longint(Value^)), LoWord(Longint(Value^))), 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := '';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StrPCopy(szName, '/StringFileInfo/'+Transstring+'/FileVersion');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if VerQueryValue(VerBuf, szName, Value, Len) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := StrPas(PChar(Value));<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; FreeMem(VerBuf);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br>
 
cAkk再问一个问题:Value指针变量就不必释放了吗,会不会造成内存漏洞?若要释放该如何释放?我试了几个方法都不行。唉,看来我还得恶补Object Pascal。
 
不必释放吧? 因为我根本没有为Value申请内存啊.
 
但Value确实指向一段内存(包含我所要的信息),这段内存就不管了吗?
 
我是这么做的:<br><br>uses 中加一个shellapi;<br><br><br>var &nbsp;pp:pchar;pbuflen:cardinal;pp1:^VS_FIXEDFILEINFO;<br>&nbsp; &nbsp; &nbsp;versize, versize1:CARDINAL;<br>begin<br>&nbsp; &nbsp; versize:=getfileversioninfosize(pchar(verfile), versize1);<br>&nbsp; &nbsp; pp:=stralloc(versize+10);<br>&nbsp; &nbsp; if getfileversioninfo(pchar(verfile), 0, versize, pp) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;verqueryvalue(pp, '/', pointer(pp1), pbuflen);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; ... &nbsp; &nbsp;//可以直接用pp1^.xxxx来引用.<br>&nbsp; &nbsp; strdispose(pp);<br>end;<br><br><br>VS_FIXEDFILEINFO 参见MSDN
 
&gt;&gt;但Value确实指向一段内存(包含我所要的信息),这段内存就不管了吗?<br>如果哦没有申请内存,那么这段内存以后会被OS用作他用.<br>如果我申请乐内存,那么该段内存将一直被占用直到被free掉.<br>
 
多人接受答案了。
 
后退
顶部