Writing DLLs in Delphi

Adding Version Information to the DLL

Since the proliferation of DLLs and VBXs produced by independent manufacturers, a major issues has arisen with the particular version of the DLL or VBX that is installed on a machine and being used.

For example, a customer installs your new mega application and it works perfectly. Some time later, they install another application that replaces one of the DLLs your application uses, with an earlier version which is not compatible with the version you ship. All of a sudden your application does not work and you get the product support phone calls and angst.

Luckily most professional companies that produce DLLs and VBXs include version information within the DLL or VBX and some even return this as a property. It is very simple to add this version information to the DLLs we write in Delphi by adding a version resource to the DLL when we compile the project. A version-information resource contains data that identifies the version, language, and distribution. Using windows API calls we can read this information.

The easiest method to create the version resource is to use a freeware product called Delphir.exe, obtainable from: ftp.borland.com/pub/techinfo/techdocs/language/delphi/gen or by contacting the author at Compuserve address 100065.55@compuserve.com.

This is a marvellous utility, with concise help, that is easy to use and will automatically add the version resource every time you compile your DLL.

To read the version resource in VB the code below can be used.

  Declare Function GetModuleHandle Lib "Kernel" 
                                       (ByVal lpModuleName As String) As Integer
  Declare Function GetModuleFileName Lib "Kernel" 
                                       (ByVal hModule As Integer, 
                                        ByVal lpFilename As String, 
                                        ByVal nSize As Integer) As Integer
  Declare Function GetFileVersionInfoSize% Lib "ver.dll" 
                                      (ByVal lpszFileName$, lpdwHandle&)
  Declare Function GetFileVersionInfo% Lib "VER.DLL" 
                                      (ByVal lpszFileName$, 
                                       ByVal handle As Any, 
                                       ByVal cbBuf&, ByVal lpvData$) 
  ' this function returns fileversion string in a version resource
  ' Getversion =   true on success
  ' false on no version resource
  ' Dllname$   =   the name of the dll. it is assumed the dllname = dll filename
  ' strversion$ =  the string in file version

  Function Getversion (Dllname$, strversion$)
    Dim OK%, hModule
    Dim pos As Integer, hVer&, VerSize&
    Dim version As String
    Dim filename$, Match$
    Getversion = False
   ' see if dll is already loaded in memory
    hModule = GetModuleHandle(Dllname$)
    If hModule = 0 Then                 ' if hModule=0 then module doesn't exist
        filename$ = Dllname$ + ".DLL"   ' if not loaded assume 
                                          filename = dll name
    Else
     filename$ = Space$(255)
        OK = GetModuleFileName(hModule, filename$, Len(filename$))
    End If
   ' read version info from file
   VerSize& = GetFileVersionInfoSize(filename$, hVer&)
    ' if VerSize&= 0 then version info doesnt exist
    If VerSize& = 0 Then
        Exit Function
    End If
    ' Get version info and fill version string:
    If VerSize& > 64000 Then VerSize& = 64000
    version = Space$(VerSize&)
    OK% = GetFileVersionInfo(filename$, hVer&, VerSize&, version)
    ' Find position in the string where the FileVersion stamp is:
    Match$ = "FileVersion"
    pos = InStr(1, version, Match$) + Len(Match$) + 1
    strversion$ = Mid$(version, pos)    ' get string after Match posn in version
    pos = InStr(1, strversion$, Chr$(0))' reads up to next chr$(0)
    strversion$ = Left$(strversion$, pos - 1)
    Getversion = True
  End Function


Image of arrow to previous article

Image of line
[HOME] [TABLE OF CONTENTS] [SEARCH]