Use like this
CString str = GetProductName() + " v" + GetVersion();
This are the functions you have to add to your class.
CString YOURCLASS::GetProductName()
{
CString str;
// get the filename of the executable containing the version resource
TCHAR szFilename[MAX_PATH + 1] = {0};
if (GetModuleFileName(NULL, szFilename, MAX_PATH) == 0)
{
TRACE("GetModuleFileName failed with error %d\n", GetLastError());
return str;
}
// allocate a block of memory for the version info
DWORD dummy;
DWORD dwSize = GetFileVersionInfoSize(szFilename, &dummy);
if (dwSize == 0)
{
TRACE("GetFileVersionInfoSize failed with error %d\n", GetLastError());
return str;
}
char* data;
data = (char*) malloc(dwSize);
if (data == NULL)
{
TRACE("malloc failed\n");
return str;
}
// load the version info
if (!GetFileVersionInfo(szFilename, NULL, dwSize, &data[0]))
{
TRACE("GetFileVersionInfo failed with error %d\n", GetLastError());
return str;
}
// get the name string
LPVOID pvProductName = NULL;
unsigned int iProductNameLen = 0;
// replace "040904e4" with the language ID of your resources
if (!VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductName"), &pvProductName, &iProductNameLen))
{
TRACE("Can't obtain ProductName from resources\n");
return str;
}
str.SetString((LPCTSTR)pvProductName, iProductNameLen);
return str;
}
CString YOURCLASS::GetVersion()
{
CString str;
// get the filename of the executable containing the version resource
TCHAR szFilename[MAX_PATH + 1] = {0};
if (GetModuleFileName(NULL, szFilename, MAX_PATH) == 0)
{
TRACE("GetModuleFileName failed with error %d\n", GetLastError());
return str;
}
// allocate a block of memory for the version info
DWORD dummy;
DWORD dwSize = GetFileVersionInfoSize(szFilename, &dummy);
if (dwSize == 0)
{
TRACE("GetFileVersionInfoSize failed with error %d\n", GetLastError());
return str;
}
char* data;
data = (char*) malloc(dwSize);
if (data == NULL)
{
TRACE("malloc failed\n");
return str;
}
// load the version info
if (!GetFileVersionInfo(szFilename, NULL, dwSize, &data[0]))
{
TRACE("GetFileVersionInfo failed with error %d\n", GetLastError());
return str;
}
// get version string
LPVOID pvProductVersion = NULL;
unsigned int iProductVersionLen = 0;
// replace "040904e4" with the language ID of your resources
if (!VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductVersion"), &pvProductVersion, &iProductVersionLen))
{
TRACE("Can't obtain ProductName and ProductVersion from resources\n");
return str;
}
str.SetString((LPCTSTR)pvProductVersion, iProductVersionLen);
return str;
}