HOWTO Get file version ? [SOLVED]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

HOWTO Get file version ? [SOLVED]

Post by Haruks »

Hi,

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

Thank you.
Last edited by Haruks on Fri May 07, 2010 2:10 am, edited 1 time in total.
=D
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: HOWTO Get file version ?

Post 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.
Last edited by netmaestro on Fri May 07, 2010 2:52 am, edited 2 times in total.
BERESHEIT
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: HOWTO Get file version ?

Post by Haruks »

@netmaestro

Thanks! this will help me a lot!
nice example.
=D
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: HOWTO Get file version ? [SOLVED]

Post by netmaestro »

You're most welcome, enjoy! :mrgreen:
BERESHEIT
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

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

Post 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!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: HOWTO Get file version ? [SOLVED]

Post 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:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

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

Post 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:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: HOWTO Get file version ? [SOLVED]

Post by ts-soft »

You can found a translated version here: http://www.purebasic.fr/english/viewtop ... 17#p323517 :mrgreen:
And yes, it is Delphi
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: HOWTO Get file version ? [SOLVED]

Post 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!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply