Mega Code Archive

 
Categories / Delphi / Examples
 

Retrieve application release date

Title: Retrieve application release date Question: In your application you sure will be able to show version info, but how you can add release date to that information? Answer: Main target In the PE (Portable Executable, executables for Windows systems at 32 bit) header there're the old NE (New Executable, for Windows systems at 16 bit) headers and the MZ (MS-DOS systems) headers. Since those headers still exists, but got discontinued, you can still examine them. In the MZ specifications, there's a 4 bytes DWord value indicating release date. Delphi compiler does not manage them (don't know why... ask Borland) they're always set on a constant value: June 19th, 1992. Conversion problem The base date of that TDate similar field isn't the same you have in your Delphi, that's why we need a constant providing us the difference between the 2 bases. const UnitDateDelta=$63E1; A type is needed... To get that value you will need to include the ImageHlp unit to your uses clausole and then specify this type: type TLoadedImage = _LOADED_IMAGE; Reading the value The function for loading date stamp is this: function GetFileDTS(const fn: string; var dts: TDateTime): boolean; var LI: TLoadedImage; begin result:=false; if FileExists(fn) then begin Win32Check(MapAndLoad(PChar(fn),nil,@LI,false,true)); dts:=LI.FileHeader.FileHeader.TimeDateStamp/SecsPerDay+UnixDateDelta; UnMapAndLoad(@LI); result:=true; end; end; This functions returns true if the value can be read. The fn arguments is the file path and name for a valid executable file, the dts variable will be set to the correct date stamp. How to update the field Once you try this you will notify that after compiling the source you still get always June 19th, 1992 as result. What to do now? You can change the date and time stamp before distributing your application using such utilities like PExplorer (http://www.heaventools.com) or you can create yourself an utility for doing that, using this function to set the field to the current date: function SetFileDTS(const fn: string; const dts: TDateTime): boolean; var LI: TLoadedImage; begin result:=false; if FileExists(fn) then begin Win32Check(MapAndLoad(pChar(fn), nil, @LI, false, false)); LI.FileHeader.FileHeader.TimeDateStamp:=round((Time-UnixDateDelta)*SecsPerDay); UnMapAndLoad(@LI); result:=true; end; end; Will ASPack, UPX, a.s.o. work with this header changed? The header is located in a section of the file that compressors cannot pack, so it will be still available for you the compression provided. You can compress files before and after changing the date and time stamp, without troubles.