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;
}
|