Discussion:
Get Version Number form resource
(too old to reply)
w***@gmail.com
2014-06-11 15:50:09 UTC
Permalink
But is there any easier way? Your approach is needed for getting
version
number out of an external file. I just want to get the version number out
of
the application itself.
He gives you working code, and you complain it's too complex? Just feed the
thing the name of the executable and be done with it.
--
Brad Wilson bradw<at>pobox<dot>com
Try my CD player for Win9x/NT! http://www.quality.nu/cdplayer/
Mercenary software engineer, Objectivist philosopher in training
"Reason and morality are the only weapons that determine the course of
history. The collectivists dropped them because they had no right to
carry them. Pick them up: you have." -- Ayn Rand
May be it is the easiest... But He can complain A LOT...!!!!
M§ libraries are a big BULL SHIT... A bit peace of CRAP...!!!
I have the same problem. And after googling for a problem which only should be a mater of 5 min, but I am now 45min and no elegant solution like.

CString myVersion = this.GetVersion();


Win32 and MFC are shit from the base. Bad structure, no coherence, etc...

For example: How can a Dialog inherit from Timer...!!!! Gooood....!!! If I want to use a timer in a Dialog I instantiate it in my class.
w***@gmail.com
2014-06-11 16:19:55 UTC
Permalink
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;
}
Cholo Lennon
2014-06-12 12:33:53 UTC
Permalink
Post by w***@gmail.com
But is there any easier way? Your approach is needed for getting
version
number out of an external file. I just want to get the version number out
of
the application itself.
He gives you working code, and you complain it's too complex? Just feed the
thing the name of the executable and be done with it.
--
Brad Wilson bradw<at>pobox<dot>com
Try my CD player for Win9x/NT! http://www.quality.nu/cdplayer/
Mercenary software engineer, Objectivist philosopher in training
"Reason and morality are the only weapons that determine the course of
history. The collectivists dropped them because they had no right to
carry them. Pick them up: you have." -- Ayn Rand
May be it is the easiest... But He can complain A LOT...!!!!
M§ libraries are a big BULL SHIT... A bit peace of CRAP...!!!
I have the same problem. And after googling for a problem which only should be a mater of 5 min, but I am now 45min and no elegant solution like.
CString myVersion = this.GetVersion();
Win32 and MFC are shit from the base. Bad structure, no coherence, etc...
For example: How can a Dialog inherit from Timer...!!!! Gooood....!!! If I want to use a timer in a Dialog I instantiate it in my class.
Answering a post from 1999! That may be a record here...

Regards
--
Cholo Lennon
Bs.As.
ARG
Continue reading on narkive:
Loading...