Check file version ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Check file version ?

  1. #1
    Join Date
    Nov 1999
    Location
    Sweden
    Posts
    5

    Check file version ?

    I am working on a program where it is necessary for me to distinguish from wich version of Microsoft Office a certain program file comes from. I am for example using a API to find all Winword.exe on the machine, this works great, but I may end up with 3 winword.exe. I am now unable to part these files from one another. I found a API that can detirmine the version of these program files, but unfortunatly it doesn't work on all versions. I need something to part these files from each other, if it is possible.

    I have just started working with VB, so it would be kind of anyone who answers to make it easy to understand. )


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Check file version ?

    you said you found an api for retrieving the version from an exe, but it didn't work on all exes.
    This may be because some versions of winword.exe are 16 bit versions.
    the 32 bit version apis only work for 32 bit modules, the 16 bit version apis only for 16 bit modules.
    The only way to use them both in a 32 bit vb app is to write a 16 ole server (e.g. in the 16 bit version of vb 4) and call that from your 32 bit vb app.
    use the 32-bit version apis in your 32 bit vb app and the 16 bit version apis in your 16 bit ole server.


  3. #3
    Join Date
    Nov 1999
    Location
    Sweden
    Posts
    5

    Re: Check file version ?

    Ok, thanks for the answer.

    I am not sure that I with my knowledge in VB can do this, but I will sure look in to it. Do you or anyone else have any good idea how I can get around this problem. Is there a way to solve my problem without checking the version of the file.

    I somehow have to find out wich Office versions are installed on a computer and then find the path to all their main programs.


  4. #4
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Check file version ?

    Try this code I wrote a while ago, it should work for 16/32-bit Files..
    Code:
    private Declare Function GetFileVersionInfo Lib "version.dll" Alias "GetFileVersionInfoA" (byval lptstrFilename as string, byval dwHandle as Long, byval dwLen as Long, lpData as Any) as Long
    private Declare Function GetFileVersionInfoSize Lib "version.dll" Alias "GetFileVersionInfoSizeA" (byval lptstrFilename as string, lpdwHandle as Long) as Long
    
    public Function GetVersion(byval sFile as string) as string
        Dim sVerInfo as string
        Dim iVerLen as Integer
        Dim iVerPos as Long
        iVerLen = GetFileVersionInfoSize(sFile, 0&)
        sVerInfo = Space(iVerLen)
        Call GetFileVersionInfo(sFile, 0&, iVerLen, byval sVerInfo)
        iVerPos = InStr(sVerInfo, "FileVersion") + 12
        If iVerPos = 12 then
            'No Version Info, Check for 32bit File -
            '32Bit Files Return Unicode Strings
            sVerInfo = StrConv(sVerInfo, vbFromUnicode)
            iVerPos = InStr(sVerInfo, "FileVersion") + 13
        End If
        If iVerPos > 12 then GetVersion = GetVersion & mid(sVerInfo, iVerPos, InStr(iVerPos, sVerInfo, Chr(0)) - 1)
    End Function
    
    private Sub Command1_Click()
        on error GoTo UserCancelled
        With CommonDialog1
            .DialogTitle = "Select a File.."
            .CancelError = true
            .Filter = "All Files|*.dll;*.exe"
            .ShowOpen
            Caption = GetVersion(.FileName)
        End With
    UserCancelled:
    End Sub
    Aaron Young
    Analyst Programmer
    adyoung@win.bright.net
    aarony@redwingsoftware.com
    Last edited by Cimperiali; July 23rd, 2004 at 05:31 PM. Reason: Adding Code tags
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  5. #5
    Join Date
    Nov 1999
    Location
    Sweden
    Posts
    5

    Re: Check file version ?

    Thank You very much.

    It is working great !!

    /Leyan



  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Check file version ?

    Ok, I was absolutely wrong.
    Sorry for the confusion.
    Aaron Youngs code is the right way to go.



  7. #7
    Join Date
    Nov 1999
    Location
    Sweden
    Posts
    5

    Re: Check file version ?

    I used the code I got from Aaron, and my application is working great, not a single bug......on my computer.....as soon as someone else uses my app they get a invalid procedure call on the line:
    Code:
    If iVerPos > 12 Then GetVersion = GetVersion & Mid(sVerInfo, iVerPos, InStr(iVerPos, sVerInfo, Chr(0)) - 1)
    Do anyone have a clue why ?
    /Leyan
    Last edited by Cimperiali; July 23rd, 2004 at 05:32 PM. Reason: subst &gt with >

  8. #8
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Check file version ?

    I couldn't reproduce the Error here, but
    It could be the Instr Function is Returning Zero, making the Mid Function Call Illeagal, try this at the End of the GetVersion Routine instead..
    Code:
        If iVerPos > 12 then
            iVerLen = InStr(iVerPos, sVerInfo, Chr(0)) - 1
            If iVerLen > 0 then GetVersion = GetVersion & mid(sVerInfo, iVerPos, iVerLen)
        End If

    If the file you checked on your PC was the Same one that Caused the Error on the Other, I don't know how that could happen.


    Aaron Young
    Analyst Programmer
    adyoung@win.bright.net
    aarony@redwingsoftware.com
    Last edited by Cimperiali; July 23rd, 2004 at 05:33 PM. Reason: Code tags and &gt to >
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  9. #9
    Join Date
    Nov 1999
    Location
    Sweden
    Posts
    5

    Re: Check file version ?

    I tried this on my computer at home, and it worked with this change. I will try it on a couple of other machines on Monday when I get back to work, but I guess you solved it for me.

    I am very greatful. Thank you.

    (Some of the files I checked on the other computers were the same ones, that sure made me pusseled too. Maybe it is something in my code, because to be honest I haven't tried your code alone on other computers)

    Leyan


  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Something from the deep
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center