473,385 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 "";
}
Nov 17 '05 #1
10 6268
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 [.NET/C# MVP] wrote:
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.
Nov 17 '05 #2
Willy Denoyette [MVP] wrote:
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?
Nov 17 '05 #3
Did you made the changes I posted?

public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;

Should be...

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

Willy.

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Willy Denoyette [MVP] wrote:
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?

Nov 17 '05 #4
Willy Denoyette [MVP] wrote:
Did you made the changes I posted?
public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;

Should be...

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

Willy.

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Willy Denoyette [MVP] wrote:
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl.. .
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!
Nov 17 '05 #5
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]
- mv*@spam.guard.caspershouse.com

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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 "";
}

Nov 17 '05 #6

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Nov 17 '05 #7
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 [.NET/C# MVP] wrote:
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.
Nov 17 '05 #8
Willy Denoyette [MVP] wrote:
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?
Nov 17 '05 #9
Did you made the changes I posted?

public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;

Should be...

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

Willy.

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Willy Denoyette [MVP] wrote:
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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?

Nov 17 '05 #10
Willy Denoyette [MVP] wrote:
Did you made the changes I posted?
public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;

Should be...

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

Willy.

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Willy Denoyette [MVP] wrote:
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl.. .
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!
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: IK | last post by:
Hello All, Please excuse me for posting this here, but I don't find any other group where I will get a proper answer. This is about clarifying the C++ part of COM. I understand that COM is a...
5
by: Keith Patrick | last post by:
Could someone tell me if it's possible (and if so, how) to call an explicitly-implemented interface method from a subclass? I have a class in which I have to explicity implement some methods, but...
2
by: Sabin Finateanu | last post by:
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: private struct...
4
by: Rubbrecht Philippe | last post by:
Hi there, I would like to develop an interface that when implemented in a class requires a number of shared properties or methods to be available. It seems a Shared Member can not be used as...
16
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
8
by: Jef Driesen | last post by:
I'm working on an image segmentation algorithm. An essential part of the algorithm is a graph to keep track of the connectivity between regions. At the moment I have a working implementation, but...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
1
by: Norman Chong | last post by:
Hi group, I want to get some information about the Windows version and ServicePack. I used the WinAPI but got the following problem: When I try to pass my OSVERSIONINFOEX structure ByRef to the...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
1
by: Fredo | last post by:
I'm getting the following error when I'm running my application (application name changed for the example): Unhandled Exception: System.TypeLoadException: Method get_ImageFile in type...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.