Announcement

Collapse
No announcement yet.

Program Name and Version from Resource File

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Program Name and Version from Resource File

    I am attempting to write a routine that pulls the program name and version from my resource file, but can not find a example like it. I have found some samples for writing a resource file, but not reading back at run time.

    For Example:
    Test Resourc.rc
    Code:
    #include "resource.h"
    // * Version info.
    //
    //
    
    VS_VERSION_INFO VERSIONINFO
    FILEVERSION 5, 0, 0, 0
    PRODUCTVERSION 5, 0, 0, 0
    FILEOS VOS_WINDOWS32
    FILETYPE VFT_APP
    BEGIN
      BLOCK "StringFileInfo"
      BEGIN
        BLOCK "040904B0"     //Does this have to be this value or what does the value mean?
        BEGIN
          VALUE "CompanyName",      "Your Company, Inc.\000"
          VALUE "FileDescription",  "Program Desc\000"
          VALUE "FileVersion",      "05.00.0000\000"
          VALUE "InternalName",     "Internal Name\000"
          VALUE "OriginalFilename", "Original Name.EXE\000"
          VALUE "LegalCopyright",   "Copyright \251 2007 Your Company, Inc.\000"
          VALUE "ProductName",      "Program Name\000"
          VALUE "ProductVersion",   "05.00.0000\000"
          VALUE "Comments",         "Yadda Yadda\000"
        END
      END
      BLOCK "VarFileInfo"
      BEGIN
        VALUE "Translation", 0x409, 0x4B0
      END
    END
    I think the route has something to do with loadstring, but unsure what to do? Can someone point me at an example? I would like to ultimately only have to change version numbers in the resource file, and not hunt through exe code for each place that I may have had to display version numbers (sometimes they slip through and I miss one so it shows the incorrect version)

    Thanx for any tips
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Your lucky day..
    Code:
    ' Get version string of current Executable. Use FileVersionString to get for another file.
    ' return program version string from resource file as "Major.minor build nnnn"
    ' changed 5/27/03 to return major.minor.build, build used to be format$=0000, now it's just
    ' a number. e.g., was "2.2 Build 0003" now is "2.2.3"
    ' STANDARD PROGRAM VERSION STRING  format: major.minor.build
    
    FUNCTION ProgramVersionString() AS STRING
    
      LOCAL major AS LONG, Minor AS LONG, Build AS LONG
      LOCAL szFile AS ASCIIZ * %MAX_PATH
      LOCAL ResSize AS LONG
      LOCAL ffi  AS VS_FIXEDFILEINFO PTR
    
      LOCAL  ret AS LONG
      LOCAL Buffer AS STRING, DLLDate AS STRING
    
      Major=0: Minor = 0:  Build = 0
    
      GetModuleFileName BYVAL %NULL, szFile, SIZEOF(szFile)
      ResSize = GetFileVersionInfoSize (szFile, ret)
      IF ResSize= 0 THEN
        FUNCTION = "Error"
        EXIT FUNCTION
      END IF
    
      Buffer    = SPACE$(ResSize)
      Ret       = GetFileVersionInfo(szFile, %NULL, ResSize, BYVAL STRPTR(Buffer))
    
    
    
    ' ** Read the VS_FIXEDFILEINFO info
      VerQueryValue BYVAL STRPTR(Buffer), "\", ffi, SIZEOF(@ffi)
      Major      = @ffi.dwProductVersionMs  \ &h10000
      Minor      = @ffi.dwProductVersionMs MOD &h10000
      Build      = @ffi.dwProductVersionLS
    
      FUNCTION = LTRIM$(STR$(Major)) & "." & TRIM$(STR$(Minor)) & "." & LTRIM$(STR$(Build))
    
    END FUNCTION
    ' get version of any file
    ' removed leading space from major 8/24/07
    FUNCTION FileVersionString(szFile AS ASCIIZ) AS STRING
      LOCAL major AS LONG, Minor AS LONG, Build AS LONG, SubBuild AS LONG
      LOCAL ResSize AS LONG
      LOCAL ffi  AS VS_FIXEDFILEINFO PTR
      LOCAL  ret AS LONG
      LOCAL Buffer AS STRING, DLLDate AS STRING
    
      Major=0: Minor = 0:  Build = 0
    
      ResSize = GetFileVersionInfoSize (szFile, ret)
      IF ResSize= 0 THEN
        FUNCTION = "No Version Info"
        EXIT FUNCTION
      END IF
    
      Buffer = SPACE$(ResSize)
      Ret =  GetFileVersionInfo(szFile, %NULL, ResSize, BYVAL STRPTR(Buffer))
    
    ' ** Read the VS_FIXEDFILEINFO info
      VerQueryValue BYVAL STRPTR(Buffer), "\", ffi, SIZEOF(@ffi)
      Major        = @ffi.dwProductVersionMs  \ &h10000
      Minor        = @ffi.dwProductVersionMs MOD &h10000
      Build        = @ffi.dwProductVersionLS MOD &h10000  ' this is for MY software which uses VERSION_MAJOR, VERSION_MINOR, 0, VERSION_BUILD under FILEVERSION
    '  SubBuild     = @ffi.dwProductVersionLS \  &h10000
      ' combine
    
    ' 8/24/07 WAS:
      FUNCTION = STR$(Major) & "." & TRIM$(STR$(Minor)) & "." & LTRIM$(STR$(Build))   ' & "." & LTRIM$(STR$(SubBuild))
    ' changed to: (to remove leading space)
      FUNCTION = FORMAT$(Major) & "." & FORMAT$(Minor) & "." & FORMAT$(Build)   ' & "." & FORMAT$(SubBuild)
    END FUNCTION
    .. now you can post a demo using same in source code forum.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Thank you MCM,

      It's a very handy INC file, but if used in a dll then the answer comes from the parent program that mapped my dll into its space.

      Which is why I was looking at resource file and pulling the string straight from there.

      I did see in POFF's something similar, but much more detailed and when I saw "GetModuleHandle" I realized would not work for if my code was in a DLL.

      If there is a replacement function for getting the name of my DLL then I may be in business though cause then I could use it to get the information I need.
      (I could always "Hard-Code" it but that defeats the purpose)

      In my case I need to have the Name and Version in the top of the main window and the "About" Window, but in this case would only work if compiled as an executable, and not a dll

      I had a few ideas, but so far no luck because I do not know where my DLL was loaded from. (I can get the parent path, but no clue if the parent path is in the same directory as my dll or in another one and the parent did a loadlibrary or hardcoded the path to my dll from there)?
      Engineer's Motto: If it aint broke take it apart and fix it

      "If at 1st you don't succeed... call it version 1.0"

      "Half of Programming is coding"....."The other 90% is DEBUGGING"

      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

      Comment


      • #4
        When I saw "GetModuleHandle" I realized would not work for if my code was in a DLL.
        Really? The l ast time I looked "getmodulehandle" ...

        A. Can be called against any module, eg "GetModuleHandle ("mydll.dll")
        B. Is passed to LibMain when the DLL is attached to the process and may be saved in a global variable.

        But I suppose you mean, you know the name of the DLL file, but not its full path so you can't use the "FileversionString" function. But I think you could try.......

        Code:
        #COMPILE EXE "My_main_exe.exe" 
        
        .....
           hDll =  GetModuleHandle ("mydll.dll")
           GetModuleFileNAme hDll, szLib, SIZEOF(szLib)
           sVersion = FileVersionString (szLib)
        Ya think that might be worth a shot? (Heck, it's how I do it)

        But you might be more comfy with ...

        Code:
        #COMPILE DLL 
        
        GLOBAL g_hInstance AS LONG
        
        FUNCTION LibMain (hInst, other stuff) .....
            DLL_Process_Attach
                 g_hinstance = hInst
        ....
        
        FUNCTION ThisDllsInstanceHandle () EXPORT AS LONG
            FUNCTION = g_hInstance
        END FUNCTION
        ...

        I shall leave the rest to your imagination.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          I should have bet you would have replied before I finished fleshing out an idea

          Truth is, I think I have a idea fleshed out and believe it or not "NO" globals

          of course you are on the mark with
          Code:
             hDll =  GetModuleHandle ("mydll.dll")
             GetModuleFileNAme hDll, szLib, SIZEOF(szLib)
             sVersion = FileVersionString (szLib)
          Because after my post I stopped and thought "What IFFFfffff???"
          and sure enough I thought of what you just posted...just took me a lil longer to think of it (no age jokes here please )

          I have a few more tests to run, but if I am right, then I do not care if my code is a dll, or an exe, or if its a dll then how it was loaded because I can find who the owner is, and where it is (I may have problems with multiple owners, but from our past conversations I suspect that will NOT be a problem)

          Forgive me if I mis-read this one
          .. now you can post a demo using same in source code forum.
          Were you being sarcastic? or did you mean I should really build a simple example for posting in the Source Code Forum? (I had to ask, its been a lonnnng day)
          Engineer's Motto: If it aint broke take it apart and fix it

          "If at 1st you don't succeed... call it version 1.0"

          "Half of Programming is coding"....."The other 90% is DEBUGGING"

          "Document my code????" .... "WHYYY??? do you think they call it CODE? "

          Comment


          • #6
            >Because after my post I stopped and thought ..

            Relative Position Error: "after" and "thought"
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Relative Position Error: "after" and "thought"
              Relative Position Error: "after" and NOT thinking more on it is more like it

              Posting you get
              1. Vent frustration
              2. Help from others (because you asked), and ideas that you may not have thought of
              3. a chance to step back, re-evaluate, and try a different avenue


              Maybe its just me, but thats why they keep the psych-ward on call just like they keep the fire-dept on call each year I get older...(danged candles )
              Engineer's Motto: If it aint broke take it apart and fix it

              "If at 1st you don't succeed... call it version 1.0"

              "Half of Programming is coding"....."The other 90% is DEBUGGING"

              "Document my code????" .... "WHYYY??? do you think they call it CODE? "

              Comment


              • #8
                Old Thread Resurrection.

                Found this thread looking for information on GetFileVersionInfoSize, (search did not find that, found from GetFileVersionInfo), because I was wondering what the second parameter to GetFileVersionInfoSize was. Apparently it's just a parameter needed by the call, or is used for undocumented functions of the call. That aside, considering that version info can be 4 values, the code needs a tad updating to indicate this:
                Code:
                LOCAL FilePath                      AS ASCIIZ * %MAX_PATH
                LOCAL VerSize, VerCheck             AS DWORD
                LOCAL VerPtr                        AS DWORD PTR
                LOCAL Version                       AS STRING
                LOCAL Work                          AS STRING
                LOCAL Major, Minor, Build, Compile  AS LONG
                LOCAL VersionInfo                   AS VS_FIXEDFILEINFO PTR
                
                    FilePath = EXE.FULL$
                    VerPtr = VARPTR(VerCheck)
                    VerSize = GetFileVersionInfoSize(FilePath, VerPtr)
                    Major=0: Minor = 0: Build = 0: Compile = 0
                    IF VerSize > 0 THEN
                        Version = SPACE$(VerSize)
                        VerCheck = GetFileVersionInfo(FilePath, %NULL, VerSize, BYVAL STRPTR(Version))
                        VerQueryValue BYVAL STRPTR(Version), "\", VersionInfo, SIZEOF(@VersionInfo)
                        Major = @VersionInfo.dwProductVersionMs \ &h10000
                        Minor = @VersionInfo.dwProductVersionMs MOD &h10000
                        Build = @VersionInfo.dwProductVersionLS \ &h10000
                        Compile = @VersionInfo.dwProductVersionLS MOD &h10000
                    END IF
                Work = "ProgramName: "& FORMAT$(Major) &"."& FORMAT$(Minor) _
                        &"."& FORMAT$(Build) &"."& FORMAT$(Compile)
                Furcadia, an interesting online MMORPG in which you can create and program your own content.

                Comment


                • #9
                  >because I was wondering what the second parameter to GetFileVersionInfoSize was.

                  ?????

                  ====> http://msdn.microsoft.com/en-us/default.aspx
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Cliff, you can store all kinds of stuff in the VarFileInfo strings, eg

                    http://www.powerbasic.com/support/pb...ad.php?t=41575

                    I have some code around here somewhere to retrieve the VarFileInfo strings, but maybe there is some here already.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      Search is your friend. Nice piece of code in Source Code Forum here from Kev Peel....

                      http://www.powerbasic.com/support/pb...light=resource
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        MCM.
                        Notice this was a thread resurrected by Colin, not me

                        Although I gotta admit when I first saw the title and my name attached to it, I started scratching my head cause I solved the problem about 20 months ago (according to the post date) so I had to wonder for a moment
                        Engineer's Motto: If it aint broke take it apart and fix it

                        "If at 1st you don't succeed... call it version 1.0"

                        "Half of Programming is coding"....."The other 90% is DEBUGGING"

                        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                        Comment

                        Working...
                        X