Recent

Author Topic: How do I determine the Version number of a DLL?  (Read 3191 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 429
How do I determine the Version number of a DLL?
« on: December 24, 2021, 09:05:04 pm »
Platform:  OS: Windows Server 2016    Lazarus 2.0.10   FPC   3.2.0
I'm working with several different versions of a DLL.  The file name is always the same.  I have a Windows app, with a main form, and I'd like to display the version number of the DLL in use, on the main form, in a Label.  I cannot find a function that can extract this information.

Does anyone know of a function or code snippet that can accomplish this?  Thanks in advance.

MarkMLl

  • Hero Member
  • *****
  • Posts: 4447
Re: How do I determine the Version number of a DLL?
« Reply #1 on: December 24, 2021, 09:54:54 pm »
In the general case, can't be done. It would have to rely on the DLL author exporting a function (etc.) which declared the version number.

I've done quite a lot of custom hacking based on a DLL (or on Linux, shared object) exporting a magic number which defined the programming interface, but that relies on having full control of both the DLL and the program that's using it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: How do I determine the Version number of a DLL?
« Reply #2 on: December 24, 2021, 10:39:40 pm »
See the Forum thread FileVersionInfo did not work with a DLL for the solution.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

RedOctober

  • Sr. Member
  • ****
  • Posts: 429
Re: How do I determine the Version number of a DLL?
« Reply #3 on: December 25, 2021, 01:11:36 am »
Hi MarkMLI and Trev.  Thanks to your help, I was able to modify the code shown in the other examples, to determine the version of a single DLL, which is what I need. The code below works.


Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.   FileUtil, fileinfo, winpeimagereader;
  4.  
  5. ...
  6.  
  7. type
  8.   TDllInfo = class(TObject)
  9.     FileName : String;
  10.     ModulName : String;
  11.     Info : string;
  12.   end;
  13.  
  14. ...
  15.  
  16.  
  17. function GetDLLVer(const dll_fnm: String): String;
  18.  
  19. ...
  20.  
  21. implementation
  22.  
  23. ...
  24.  
  25.  
  26. function GetDLLVer(const dll_fnm: String): String;
  27. var
  28.   i: Integer;
  29.   FileVerInfo:TFileVersionInfo;
  30.   FileName, ModulName: String;
  31.   aDllInfo : TDllInfo;
  32. begin
  33.   FileVerInfo := nil;
  34.   try
  35.     FileVerInfo:= TFileVersionInfo.Create(nil);
  36.     FileVerInfo.FileName:= dll_fnm;
  37.     try
  38.       FileVerInfo.ReadFileInfo;
  39.       ModulName:= FileVerInfo.VersionStrings.Values['InternalName'];
  40.       if ModulName <> '' then begin
  41.         aDllInfo := TDllInfo.Create;
  42.         aDllInfo.FileName:= dll_fnm;
  43.         aDllInfo.ModulName:= ModulName;
  44.         aDllInfo.Info := FileVerInfo.VersionStrings.Values['FileVersion'];
  45.         Result := aDllInfo.Info;
  46.       end;
  47.     finally
  48.       ; // nothing to do
  49.     end;
  50.   finally
  51.     FileVerInfo.Free;
  52.   end;
  53. end;
  54.  
  55.  
  56.  


 

TinyPortal © 2005-2018