retrieving file version information

Jun 7, 2009 at 4:24am
In an app i'm working on, I want the application's version to be displayed on a status bar at the bottom of the window. So I included a file version information resource in my resource file. In windows explorer, the data shows up just fine (version, copyright, ect.). However, the build number and version number are changed with each build/release of the project. So I wanted to create a class that would retrieve the information to display.

The class uses the API functions GetFileVersionInfoSize(), GetFileVersionInfo(), and VerQueryValue(). Unfortunately, it doesn't work. I've read several articles I found googling, and re-read the MSDN pages multiple times. It doesn't cause a crash, it just fails. More specifically, GetFileVersionInfo() fails. It returns 0 indicating the failure, so I get the last system error, and it turns out to be "The data is invalid."

Thing is, I know it's not invalid, it shows just fine in explorer. Does anyone have any ideas what could be going wrong?

source code for function that retrieves the version data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bool version::getinfo()
{
    string temp;
    bool bResult = false;
    DWORD size;
    DWORD dummy;
    char filename[ 130 ];
    unsigned int len;
    GetModuleFileName( NULL, filename, 128 );
    size = GetFileVersionInfoSize( filename, &dummy );
    if( size == 0 )
    {
        this->ver = "No Version Information!";
        return true;
    }
    char* buffer = new char[ size ];
    VS_FIXEDFILEINFO* data = NULL;
    if( buffer = NULL ){ return true; }
    bResult = GetFileVersionInfo( filename, 0, size, (void*)buffer );
    if( !bResult )
    {
        this->ver = STRLASTERROR; // STRLASTERROR is a custom macro
        return true;
    }
    bResult = VerQueryValue( buffer, "\\", (void**)&data, &len );
    if( !bResult || data == NULL || len != sizeof(VS_FIXEDFILEINFO) )
    {
        this->ver = "Could Not Retrieve Values!";
        return true;
    }
    // here I would extract the needed values
    delete[] buffer;
    this->valid = true;
    return false;
}
Jun 8, 2009 at 3:21pm
You overwrite your buffer pointer. Try
if( buffer == NULL ){ return true; }
instead.
Jun 8, 2009 at 6:24pm
hahahaha! wow, it's been a long time since I've done that mistake! nice catch, it works just as intended now, thank you so much!
best regards frhm.
Jun 9, 2009 at 6:48am
Your welcome. Actually, I looked for a code snippet like yours. I solved your problem. And you saved me a lot of hours work. Thanks very much yourself.
//frhm
Topic archived. No new replies allowed.