Click to See Complete Forum and Search --> : GetVersionEx problems


links
September 20th, 2007, 10:24 AM
I'm busy writing some code to do a version check of Windows before my application runs.

I've broken up the test into two seperate functions for compatibility reasons.

The problem is that my app doesn't compile. I get the following error:

"windows version.h(60) : error C2664: 'GetVersionExW' : cannot convert parameter 1 from 'OSVERSIONINFOEX *__w64 ' to 'LPOSVERSIONINFOW'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

The first function compiles, its only the second one that's giving problems. If I understand MSDN correctly my implementation should work.

I'm using VC++ 2005 SP1 and compiling under Unicode. As always any help will be appreciated!

Here is the code:

class Windows_Version
{
public:
Windows_Version();

bool GetVersion9x();

bool GetVersionNT();

private:
OSVERSIONINFO _osverOld;
OSVERSIONINFOEX _osver;
};

Windows_Version::Windows_Version()
{
_osverOld.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
}

bool Windows_Version::GetVersion9x()
{
GetVersionEx (&_osverOld);
bool result = 0;

// Windows 95
if ((_osverOld.dwMajorVersion = 4) && (_osverOld.dwMinorVersion = 0) && (_osverOld.dwPlatformId = 1))
{

bool result = 1;
}
else
// Windows NT 4.0
if ((_osverOld.dwMajorVersion = 4) && (_osverOld.dwMinorVersion = 0) && (_osverOld.dwPlatformId = 2))
{

bool result = 1;
}
else
// Windows 98
if ((_osverOld.dwMajorVersion = 4) && (_osverOld.dwMinorVersion = 10))
{

bool result = 1;
}
else
// Windows Me
if ((_osverOld.dwMajorVersion = 4) && (_osverOld.dwMinorVersion = 90))
{

bool result = 1;
}
return result;
}

bool Windows_Version::GetVersionNT()
{
_osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

GetVersionEx (&_osver);

bool result = 0;

// Windows 2000
if ((_osver.dwMajorVersion = 5) && (_osver.dwMinorVersion = 0))
{

bool result = 1;
}
else
{
// Windows XP
if ((_osver.dwMajorVersion = 5) && (_osver.dwMinorVersion = 1))
{
if (_osver.wServicePackMajor = 2)
{

bool result = 1;
}
else
{

bool result = 1;
}
}
else
{
// Windows Server 2003
if ((_osver.dwMajorVersion = 5) && (_osver.dwMinorVersion = 2))
{

bool result = 1;
}
else
{
// Windows Vista
if ((_osver.dwMajorVersion = 6) && (_osver.dwMinorVersion = 0))
{

bool result = 1;
}
}
}
}
return result;
}

0xC0000005
September 20th, 2007, 12:34 PM
It's just another one of those weird idiosyncrasies of the Windows API. GetVersionEx() does not take an LPOSVERSIONINFOEX - it takes an LPOSVERSIONINFO.

However, you can pass it an LPOSVERSIONINFOEX by setting the size correctly (as you have done) and then casting to LPOSVERSIONINFO.
_osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx ((LPOSVERSIONINFO)&_osver);

links
September 20th, 2007, 03:52 PM
Oh thank you so much! I thought I was going insane. I'm still a newbie so spotting these kind of errors is challenging. Thank you again.