GetVersionEx Implementation Problem

S

Sabin Finateanu

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 "";
}
 
N

Nicholas Paldino [.NET/C# MVP]

Sabin,

Why not use the static OSVersion property on the Environment class? It
should give you a good deal of what you want.

If that doesn't give you want you want, have you tried looking for this
definition on http://www.pinvoke.net?

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sabin Finateanu said:
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 "";
}
 
W

Willy Denoyette [MVP]

Sabin Finateanu said:
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;
}
A firts look only...

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;

Willy.
 
S

Sabin Finateanu

I wanted to use the OS Version but that property doesn't give me any
information on the OS suite or product type. That's why i did it this
way. Nicholas Paldino said:
Sabin,

Why not use the static OSVersion property on the Environment class? It
should give you a good deal of what you want.

If that doesn't give you want you want, have you tried looking for this
definition on http://www.pinvoke.net?

Hope this helps.

I wanted to use the OS Version but that property doesn't give me any
information on the OS suite or product type. That's why i did it this way.
 
S

Sabin Finateanu

Willy said:
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;
}

A firts look only...

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;

Willy.
I've made some debugging and it seems that the GetVersionEx always
returns false. Can anyone tell me whay?
 
W

Willy Denoyette [MVP]

Did you made the changes I posted?


Should be...

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;

Willy.

Sabin Finateanu said:
Willy said:
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;
}

A firts look only...

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;

Willy.
I've made some debugging and it seems that the GetVersionEx always
returns false. Can anyone tell me whay?
 
S

Sabin Finateanu

Willy said:
Did you made the changes I posted?




Should be...

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;

Willy.

Willy said:
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;
}


A firts look only...

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;

Willy.

I've made some debugging and it seems that the GetVersionEx always
returns false. Can anyone tell me whay?
Now it works. I just realized that uint16 = short. I forgot that.
Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top