Similar Threads
1. GetVersionEx Implementation Problem
Hi! I have this method that gets the OS suite and product type, but it
is always returning null. Can anyone help me, please? Here is the method
and other necessary code:
[StructLayout(LayoutKind.Sequential)]
private struct OSVERSIONINFOEX
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll", EntryPoint="GetVersionEx")]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX
osVersionInfo);
#region Private Constants
private const Int32 VER_NT_WORKSTATION = 1;
private const Int32 VER_NT_DOMAIN_CONTROLLER = 2;
private const Int32 VER_NT_SERVER = 3;
private const Int32 VER_SUITE_SMALLBUSINESS = 1;
private const Int32 VER_SUITE_ENTERPRISE = 2;
private const Int32 VER_SUITE_TERMINAL = 16;
private const Int32 VER_SUITE_DATACENTER = 128;
private const Int32 VER_SUITE_SINGLEUSERTS = 256;
private const Int32 VER_SUITE_PERSONAL = 512;
private const Int32 VER_SUITE_BLADE = 1024;
#endregion
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string OSProductType()
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
OperatingSystem osInfo = Environment.OSVersion;
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(osVersionInfo);
if(!GetVersionEx(ref osVersionInfo))
{
return "";
}
else
{
if(osInfo.Version.Major == 4)
{
if(osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
// Windows NT 4.0 Workstation
return " Workstation";
}
else if(osVersionInfo.wProductType == VER_NT_SERVER)
{
// Windows NT 4.0 Server
return " Server";
}
else
{
return "";
}
}
else if(osInfo.Version.Major == 5)
{
if(osVersionInfo.wProductType == VER_NT_WORKSTATION)
{
if((osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) ==
VER_SUITE_PERSONAL)
{
// Windows XP Home Edition
return " Home Edition";
}
else
{
// Windows XP / Windows 2000 Professional
return " Professional";
}
}
else if(osVersionInfo.wProductType == VER_NT_SERVER)
{
if(osInfo.Version.Minor == 0)
{
if((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) ==
VER_SUITE_DATACENTER)
{
// Windows 2000 Datacenter Server
return " Datacenter Server";
}
else if((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) ==
VER_SUITE_ENTERPRISE)
{
// Windows 2000 Advanced Server
return " Advanced Server";
}
else
{
// Windows 2000 Server
return " Server";
}
}
else
{
if((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) ==
VER_SUITE_DATACENTER)
{
// Windows Server 2003 Datacenter Edition
return " Datacenter Edition";
}
else if((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) ==
VER_SUITE_ENTERPRISE)
{
// Windows Server 2003 Enterprise Edition
return " Enterprise Edition";
}
else if((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) ==
VER_SUITE_BLADE)
{
// Windows Server 2003 Web Edition
return " Web Edition";
}
else
{
// Windows Server 2003 Standard Edition
return " Standard Edition";
}
}
}
}
}
return "";
}
2. Determine if NT running with GetVersionEx
3. GetVersionEx with Vista
Hi guys,
Okay, thinking caps on, I have an extremely difficult question
for you. ;-)
How, in Visual C++, do you detect which version of Windows is
running ? In particular, I want to detect if my .exe is running on Vista.
Now, before you all roll your eyes, assume that I'm a C++ virgin and
tell me that this topic has been covered many many times, let me throw
a spanner in the works.
The following code tests whether my .exe is being run on Vista, and
(for this article's sake) it also displays a dialog showing the version
of Windows that it's being run on.
BOOL CMikesApp::IsVista()
{
OSVERSIONINFO osver;
memset(&osver, 0, sizeof(OSVERSIONINFO));
osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
CString str;
str.Format(_T("Version: %d.%d Platform ID: %d"), osver.dwMajorVersion,
osver.dwMinorVersion, osver.dwPlatformId);
MessageBox(NULL, str, _T("Version test"), MB_OK);
if (!GetVersionEx( &osver ))
return FALSE;
if ( osver.dwPlatformId == VER_PLATFORM_WIN32_NT
&& osver.dwMajorVersion >= 6 )
return TRUE;
return FALSE;
}
Looks good, doesn't it ?
One problem though. If I compile this code as, say, MIKE.EXE, then
it works fine. It shows that my PC is running v6.0 - which is Windows
Vista - and the function returns TRUE.
Now, try compiling it into an .exe with "install" in it's filename, like
"MIKES_INSTALLER.EXE".
First, you'll notice Vista popping up one of it's "is this a safe .exe
to run ?" security messages... (fair enough..), but THEN my dialog
tells me that I'm running Windows v5.1, and returns FALSE, as it
thinks it's running on Windows XP !!!
WHAT ?!!!!!!
Okay, somebody help me out here, as I'm baffled.
So, if I compile my .exe into a filename with "install" in it's filename,
then Windows Vista will get GetVersionEx() to report back with an
incorrect Windows Version Number ?!!!!!!!
Is this a bug, or one of MS's strange security measures ?
Oh, I'm confused.
I'm very confused.
Mike
mike at iSomewhere dot com