Page 1 of 1

HOWTO Get file version ? [SOLVED]

Posted: Thu May 06, 2010 11:19 pm
by Haruks
Hi,

Have any way to get file version?
For example, get version of the file "c:\windows\notepad.exe"

Thank you.

Re: HOWTO Get file version ?

Posted: Fri May 07, 2010 2:00 am
by netmaestro
I'm afraid this isn't something that is native in Purebasic. However, as PB has unlimited access to the Windows API, it can be managed with a few imports and some API coding. I made this demo for you, I hope it helps:

Code: Select all

;///////////////////////////////////////////////////////////////////
;
; Demo:             Windows API File Version Info
; Author:           Lloyd Gallant (netmaestro)
; Date:             May 06, 2010
; Target Compiler:  PureBasic 4.20 and later
; Target OS:        Microsoft Windows All (x86)
; License:          Free, no restrictions, no warranty whatsoever
;
;//////////////////////////////////////////////////////////////////

Pattern$ = "Executables (*.exe)|*.exe;|Dynamic Link Library (*.dll)|*.dll;|All files (*.*)|*.*"
Filename$ = OpenFileRequester("Please choose file to load", "", Pattern$, 0)

Import "version.lib"
  GetFileVersionInfoSizeA( *lptstrFilename, *lpdwHandle )
  GetFileVersionInfoA( *lptstrFilename, dwHandle, dwLen, *lpData)
  VerQueryValueA( *pBlock, *lpSubBlock, *lplpBuffer, *puLen)
EndImport

Global Dim Stringname$(12)
Restore stringnames
For i=1 To 12
  Read$ Stringname$(i)
Next

Structure LANGANDCODEPAGE 
  wLanguage.w
  wCodePage.w
EndStructure

infosize = GetFileVersionInfoSizeA( @filename$, @handle.i)
If infosize
  *lpData = AllocateMemory(infosize)
  If GetFileVersionInfoA( @filename$, 0, infosize, *lpdata)
    If VerQueryValueA( *lpdata, @"\VarFileInfo\Translation", @*lplpBuffer, @puLen)
      *readptr.LANGANDCODEPAGE = *lplpBuffer
      For i=0 To puLen/SizeOf(LANGANDCODEPAGE) - 1
        langandcodepage$ = RSet(Hex(*readptr\wLanguage),4,"0") + RSet(Hex(*readptr\wCodePage), 4, "0")
        For j=1 To 12
          SubBlock$ ="\\StringFileInfo\\" + langandcodepage$ + "\\" + Stringname$(j)
          If VerQueryValueA( *lpdata, @SubBlock$, @*stringbuffer, @Bytes) And Bytes
            Debug Stringname$(j)+": "+PeekS(*stringbuffer, Bytes)
          EndIf
        Next
        *readptr + SizeOf(LANGANDCODEPAGE)
      Next
    EndIf
  Else
    Debug "No version information available"
  EndIf
  FreeMemory(*lpData)
Else
  Debug "No version information available"
EndIf

DataSection 
  stringnames:
  Data.s "Comments",        "InternalName",     "ProductName"
  Data.s "CompanyName",     "LegalCopyright",   "ProductVersion"
  Data.s "FileDescription", "LegalTrademarks",  "PrivateBuild"
  Data.s "FileVersion",     "OriginalFilename", "SpecialBuild"
EndDataSection
As you can see, there really isn't much to it. Perhaps someday the Purebasic team will make some native commands for it. If it's important to you, you can make a feature request. They do read and consider all of them.

Re: HOWTO Get file version ?

Posted: Fri May 07, 2010 2:09 am
by Haruks
@netmaestro

Thanks! this will help me a lot!
nice example.

Re: HOWTO Get file version ? [SOLVED]

Posted: Fri May 07, 2010 3:06 am
by netmaestro
You're most welcome, enjoy! :mrgreen:

Re: HOWTO Get file version ? (n,n,n,n)

Posted: Thu Dec 27, 2012 4:38 pm
by charvista
Hello,

I was searching for an API to get the version of the executable created with PB, and found this. However, it does not return the info I need to get. :cry:

In PureBasic (v.5.00), Compiler/Compiler Options.../ under the tab "Version Info", we have the two first parameters:

File Version (n,n,n,n)
Product Version (n,n,n,n)

Can netmaestro's program be updated to get them as well?

Thanks in advance!
PS: The file version we see when we run this program are the 5th and 6th parameters!

Re: HOWTO Get file version ? [SOLVED]

Posted: Thu Dec 27, 2012 7:44 pm
by skywalk
Yeah, I noticed this too.
The IDE - Version Info Tab states "Fields marked with * are required."
So, you have to enter your FileVersion and ProductVersion into the 5th and 6th parameters also.
I found the values in the 1st 2 fields are placed in the resource block and are read differently from the code posted above.
Read/Set File Version in Resource Block
No benefit to reading this though, since they should be identical. :wink:

Re: HOWTO Get file version ? (n,n,n,n)

Posted: Thu Dec 27, 2012 9:44 pm
by charvista
Thanks Skywalk,

True, but as the PureBasic manual explained, the 5th param (ProductVersion) should read "v4" for example, and the 6th param (FileVersion) as "4.0".
(see PureBasic Help, tab "Search", find "Compiling your programs" and scroll until you see the screenshot of "Version Info").

But thanks to your link, I found that one of the elements we need is dwFileVersionMS, so I searched the internet, and found this page: http://fgaillard.com/2012/08/reading-ve ... -your-exe/, so maybe you or someone else can translate it to PureBasic? I think it is Delphi code... It would be great to enrich PB with this valuable info... :wink:

Re: HOWTO Get file version ? [SOLVED]

Posted: Thu Dec 27, 2012 10:01 pm
by ts-soft
You can found a translated version here: http://www.purebasic.fr/english/viewtop ... 17#p323517 :mrgreen:
And yes, it is Delphi

Re: HOWTO Get file version ? [SOLVED]

Posted: Thu Dec 27, 2012 10:41 pm
by charvista
:lol: LOL it is linking to the top of THIS post! :lol:

Never mind, I finally found the code in PureBasic by searching further : http://www.purebasic.fr/english/viewtop ... MS#p308752
And this one works great! OK, it displays only the FileVersion (1st param), but that's okay to me, as it is what we see in the properties of the EXE file through the Explorer.

Thank you for your time!