Ian*_*oyd 5 versioning delphi resources winapi delphi-5
我怎样才能获得正在运行的应用程序的版本?
我一直在使用GetFileVersionInfo(ParamStr(0), ...):
filename := PChar(ExtractShortPathName(ParamStr(0)));
//Get the number of bytes he have to allocate for the file information structure
dwInfoLength := GetFileVersionInfoSize(lptstrFilename, {var}dwHandle);
//Get version info
GetMem(pInfoData, dwInfoLength);
GetFileVersionInfo(lptstrFilename, dwHandle, dwInfoLength, pInfoData);
//Set what information we want to extract from pInfoData
lpSubBlock := PChar(Chr(92)+Chr(0));
//Extract the desired data from pInfoData into the FileInformation structure
VerQueryValue(pInfoData, lpSubBlock, PFileInformation, LengthOfReturned);
Run Code Online (Sandbox Code Playgroud)
这种技术的问题在于它需要Windows加载器在可以读取资源之前加载图像.我使用IMAGE_FILE_NET_RUN_FROM_SWAPimage标志构建我的应用程序(以避免在繁琐的网络上出现页面内异常).
这会导致Windows加载程序再次通过网络加载整个图像,而不是仅仅查看"我".由于我在启动时检查并保存了我自己的版本,因此6秒的应用程序启动变成了10秒的应用程序启动.
我如何阅读我的版本,我正在运行的应用程序?
我会假设Windows没有API来读取正在运行的进程的版本,只有我加载的文件(如果文件不再存在,那么它无法读取任何版本信息).
但我也假设有可能从我的进程自己的内存中读取版本资源(当然不是Administrators或Debuggers组的成员).
我可以阅读我的流程版本吗?
相关奖金问题:我如何PE Image从我而不是通过网络加载资源?
在Stackoverflow上找到它:
我已经知道如何确定一个应用程序版本,但@StijnSanders提出了"更好"的方式,正是因为我正在打击的原因:
当您想知道当前正在运行的可执行文件的版本时,我强烈建议不要使用GetFileVersion!我有两个很好的理由这样做:
- 可执行文件可能无法访问(已断开连接的驱动器/共享)或已更改(.exe已重命名为.bak并由新的.exe替换,而不会停止正在运行的进程).
- 您尝试读取的版本数据实际上已经加载到内存中,并且可以通过加载此资源来使用,这总是比执行额外(相对较慢)的磁盘操作更好.
我改编成:
function GetModuleVersion(Instance: THandle; out iMajor, iMinor, iRelease, iBuild: Integer): Boolean;
var
fileInformation: PVSFIXEDFILEINFO;
verlen: Cardinal;
rs: TResourceStream;
m: TMemoryStream;
resource: HRSRC;
begin
//You said zero, but you mean "us"
if Instance = 0 then
Instance := HInstance;
//UPDATE: Workaround bug in Delphi if resource doesn't exist
resource := FindResource(Instance, 1, RT_VERSION);
if resource = 0 then
begin
iMajor := 0;
iMinor := 0;
iRelease := 0;
iBuild := 0;
Result := False;
Exit;
end;
m := TMemoryStream.Create;
try
rs := TResourceStream.CreateFromID(Instance, 1, RT_VERSION);
try
m.CopyFrom(rs, rs.Size);
finally
rs.Free;
end;
m.Position:=0;
if not VerQueryValue(m.Memory, '\', (*var*)Pointer(fileInformation), (*var*)verlen) then
begin
iMajor := 0;
iMinor := 0;
iRelease := 0;
iBuild := 0;
Exit;
end;
iMajor := fileInformation.dwFileVersionMS shr 16;
iMinor := fileInformation.dwFileVersionMS and $FFFF;
iRelease := fileInformation.dwFileVersionLS shr 16;
iBuild := fileInformation.dwFileVersionLS and $FFFF;
finally
m.Free;
end;
Result := True;
end;
Run Code Online (Sandbox Code Playgroud)
警告:由于Delphi中的错误,上面的代码有时会崩溃:
rs := TResourceStream.CreateFromID(Instance, 1, RT_VERSION);
Run Code Online (Sandbox Code Playgroud)
如果没有版本信息,Delphi会尝试引发异常:
procedure TResourceStream.Initialize(Instance: THandle; Name, ResType: PChar);
procedure Error;
begin
raise EResNotFound.CreateFmt(SResNotFound, [Name]);
end;
begin
HResInfo := FindResource(Instance, Name, ResType);
if HResInfo = 0 then Error;
...
end;
Run Code Online (Sandbox Code Playgroud)
当然,这个bug PChar并不总是指向ansi char的指针.对于非命名资源,它们是整数常量,强制转换为a PChar.在这种情况下:
Name: PChar = PChar(1);
Run Code Online (Sandbox Code Playgroud)
当Delphi尝试构建异常字符串时,取消引用0x00000001它触发的指针并访问违规.
修复是先手动调用FindResource(Instance, 1, RT_VERSION):
var
...
resource: HRSRC;
begin
...
resource := FindResource(Instance, 1, RT_VERSION);
if (resource = 0)
begin
iMajor := 0;
iMinor := 0;
iRelease := 0;
iBuild := 0;
Result := False;
Exit;
end;
m := TMemoryStream.Create;
...
Run Code Online (Sandbox Code Playgroud)
注意:任何代码都将发布到公共域中.无需归属.
| 归档时间: |
|
| 查看次数: |
12923 次 |
| 最近记录: |