如何自动化将生成日期定义为我的代码可见的常量? [英] How can I automate getting the date of build into a constant visible to my code?
问题描述
我想在我的代码中定义一个持有可执行文件构建日期的常量。我自然希望自动化该过程。
我知道我可以使用例如Perl编写一个预构建脚本来写出一个 .inc
包含日期的文件。我更喜欢使用环境变量或构建变量的更轻量级的解决方案。 msbuild是否提供任何有助于帮助的变量?有没有人知道这个问题的更好的解决方案?
您可以从 PE标题:
使用
ImageHlp;
函数LinkerTimeStamp(const FileName:string):TDateTime;超载;
var
LI:TLoadedImage;
begin
Win32Check(MapAndLoad(PChar(FileName),nil,@LI,False,True));
结果:= LI.FileHeader.FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
UnMapAndLoad(@LI);
结束
对于当前模块的加载映像,以下内容似乎有效:
函数LinkerTimestamp:TDateTime;超载;
begin
结果:= PImageNtHeaders(HInstance + Cardinal(PIMageDosHeader(HInstance)^ ._ lfanew))^。FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
结束
早期版本的Delphi没有正确更新它,但它已经在Delphi 2010左右修复。对于早期版本,我使用了 IDE插件,在编译成功后自动更新。 / p>
注意:该值存储为UTC,以便显示,您可能需要将其转换为适当的时区。
I would like to define in my code a constant holding the date on which the executable was built. I would naturally like to automate that process.
I know that I can write a pre-build script using, for example, Perl, to write out a .inc
file containing the date. I would prefer a more lightweight solution using, perhaps, environment variables or build variables. Does msbuild provide any variables that would help? Does anyone know a neater solution to the problem?
You can read the linker timestamp from the PE header of the executable:
uses
ImageHlp;
function LinkerTimeStamp(const FileName: string): TDateTime; overload;
var
LI: TLoadedImage;
begin
Win32Check(MapAndLoad(PChar(FileName), nil, @LI, False, True));
Result := LI.FileHeader.FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
UnMapAndLoad(@LI);
end;
For the loaded image of the current module, the following seems to work:
function LinkerTimestamp: TDateTime; overload;
begin
Result := PImageNtHeaders(HInstance + Cardinal(PImageDosHeader(HInstance)^._lfanew))^.FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
end;
Earlier versions of Delphi didn't update it correctly but it has been fixed around Delphi 2010 or so. For the earlier versions, I used an IDE plugin to update it automatically after a successful compile.
Note: The value is stored as UTC so for display purposes you may need to convert it to an appropriate timezone.
这篇关于如何自动化将生成日期定义为我的代码可见的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!