GetVersionEx实现问题 [英] GetVersionEx Implementation Problem
问题描述
嗨!我有这种获取操作系统套件和产品类型的方法,但是它确保
总是返回null。有人可以帮帮我吗?这是方法
和其他必要的代码:
[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私人常数
私人const Int32 VER_NT_WORKSTATION = 1;
private const Int32 VER_NT_DOMAIN_CO NTROLLER = 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> < br $>
///
///< / 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"工作站;
}
else if(osVersionInfo.wProductType == VER_NT_SERVER)
{
// Windows NT 4.0服务器
返回"服务器;
}
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 >
返回专业;
}
}
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"数据中心服务器;
}
else if((osVersionInfo.wSuiteMask& VER_SUITE_ENTERPRISE)==
VER_SUITE_ENTERPRISE)
{
// Windows 2000 Advanced Server
return" Advanced Server" ;;
}
else
{
// Windows 2000 Server
返回"服务器;
}
}
其他
{
if(( osVersionInfo.wSuiteMask& VER_SUITE_DATACENTER)==
VER_SUITE_DATACENTER)
{
// Windows Server 2003 Datacenter Edition
返回数据中心版;
}
else if((osVersionInfo.wSuiteMask& VER_SUITE_ENTERPRISE)==
VER_SUITE_ENTERPRISE)
{
// Windows Server 2003企业版
return"企业版;
}
否则if((osVersionInfo.wSuiteMask& VER_SUITE_BLADE)==
VER_SUITE_BLADE)
{
// Windows Server 2003网络版
返回"网络版" ;;
}
其他
{
// Windows Server 2003标准版
return"标准版;
}
}
}
}
}
返回"";
}
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 "";
}
推荐答案
我想要使用操作系统版本,但该属性没有给我任何关于操作系统套件或产品类型的信息。这就是我这样做的原因
方式。 Nicholas Paldino [.NET / C#MVP]写道:
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,
为什么不在Environment类上使用静态OSVersion属性?它应该给你很多你想要的东西。
如果那不是你想要你想要的,你试过在定义吗? rel =nofollowhref =http://www.pinvoke.net? target =_ blank> http://www.pinvoke.net?
希望这会有所帮助。
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.
Willy Denoyette [MVP]写道:
Willy Denoyette [MVP] wrote:
" Sabin Finateanu" < FS **** @ rdslink.ro>在消息中写道
新闻:%2 *************** @ TK2MSFTNGP10.phx.gbl ...
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
嗨!我有这个获取操作系统套件和产品类型的方法,但它总是返回null。有人可以帮帮我吗?这是方法和其他必要的代码:
[StructLayout(LayoutKind.Sequential)]
私有结构OSVERSIONINFOEX
公共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;
}
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;
}
只有第一次看...
公共简短wServicePackMajor;
公共简短wServicePackMinor;
公共简短wSuiteMask;
Willy。
A firts look only...
public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
Willy.
我做了一些调试,似乎GetVersionEx总是
返回false。谁能告诉我什么?
I''ve made some debugging and it seems that the GetVersionEx always
returns false. Can anyone tell me whay?
你做了我发布的更改吗?
Did you made the changes I posted?
public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;
public int wServicePackMajor;
public int wServicePackMinor;
public int wSuiteMask;
应该是。 ..
公开简短wServicePackMajor;
公开简短wServicePackMinor;
公开简短wSuiteMask;
Willy。
" Sabin Finateanu" < FS **** @ rdslink.ro>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ... Willy Denoyette [MVP]写道:
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>在消息中写道
新闻:%2 *************** @ TK2MSFTNGP10.phx.gbl ...
"Sabin Finateanu" <fs****@rdslink.ro> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
嗨!我有这个获取操作系统套件和产品类型的方法,但它总是返回null。有人可以帮帮我吗?这是方法和其他必要的代码:
[StructLayout(LayoutKind.Sequential)]
私有结构OSVERSIONINFOEX
公共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;
}
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;
}
只有第一次看...
公共简短wServicePackMajor;
公共简短wServicePackMinor;
公共简短wSuiteMask;
Willy。
A firts look only...
public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
Willy.
我做了一些调试,似乎GetVersionEx总是
返回false。谁能告诉我什么?
I''ve made some debugging and it seems that the GetVersionEx always
returns false. Can anyone tell me whay?
这篇关于GetVersionEx实现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!