mfc >> GetVersionEx

by Ed » Sun, 12 Mar 2006 23:51:26 GMT

Could someone provide me with a code snippet on the use of GetVersionEx or
any other way to determine if a computer is running WindowsNT 4.0
I want to lock an application to run only on WindowsNT 4.0 and not on any
other version of windows.
I am using Visual C++ 6.0

Thanks in advance.




mfc >> GetVersionEx

by Raymond Cruz » Mon, 13 Mar 2006 00:18:16 GMT


GetVersionEx(&osVersionInfo);
if(osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
osVersionInfo.dwMajorVersion == 4) { ... }







mfc >> GetVersionEx

by Ed » Mon, 13 Mar 2006 01:22:19 GMT

In the code given

GetVersionEx(&osVersionInfo);
if(osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
osVersionInfo.dwMajorVersion == 4)
{ ... }

what is the variable "osVersionInfo" declared as?

Thanks







GetVersionEx

by Raymond Cruz » Mon, 13 Mar 2006 06:46:31 GMT


GetVersionEx

by Ed » Tue, 14 Mar 2006 21:32:31 GMT

Anyone else here interested in helping me out rather that simply point to
MSDN?
I just want to make sure the current opperating system is Windows NT 4.0
I can't allways understand the way MSDN explains things.
Thanks.
btw, this is NOT a school project. I am trying to add this check to my
software.








GetVersionEx

by Scott McPhillips [MVP] » Tue, 14 Mar 2006 23:07:53 GMT




OSVERSIONINFO osVersionInfo;
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osVersionInfo);


Hold that makes it clear.

--
Scott McPhillips [VC++ MVP]



GetVersionEx

by Ed » Wed, 15 Mar 2006 07:27:46 GMT

Thank you very much Scott.
Now it works perfectly :)










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