简体   繁体   English

查找Windows是否是没有WMI的OEM

[英]Find if Windows is OEM without WMI

It can be done using WMI. 它可以使用WMI完成。

But is there a simple way (similar to Environment.Is64BitOperatingSystem )? 但有一种简单的方法(类似于Environment.Is64BitOperatingSystem )?

EDIT: There are permission limitations on WMI. 编辑:WMI有权限限制。 As far as I know Is64BitOperatingSystem is much "better" in this respect, and maybe others. 据我所知, Is64BitOperatingSystem在这方面要好得多,也许还有其他人。 (Please correct me if I'm wrong.) (如果我错了,请纠正我。)

I'm trying to make sure the code will always run, without throwing exceptions. 我正在努力确保代码始终运行,而不会抛出异常。

Well, first off, the Environment.Is64BitOperatingSystem is able to do its job without requiring any special permissions because it doesn't actually do any checks whatsoever. 好吧,首先, Environment.Is64BitOperatingSystem能够完成其工作而不需要任何特殊权限,因为它实际上不会进行任何检查。 The 64-bit version of the .NET Framework is hard-coded to return true, and the 32-bit version to return false. 64位版本的.NET Framework硬编码返回true,32位版本返回false。 Simple as that. 就那么简单。

Things are going to be a bit more complicated for the information you wish to obtain, unfortunately. 不幸的是,对于您希望获得的信息,事情会变得复杂一些。

You don't say how you're obtaining this information through WMI, but I assume that you're querying the SerialNumber property of the Win32_OperatingSystem class, then taking the information you already know about the format of Windows product IDs to determine whether the OS was installed using an OEM key. 您没有说明如何通过WMI获取此信息,但我假设您正在查询Win32_OperatingSystem类的SerialNumber属性,然后获取您已了解的有关Windows产品ID格式的信息以确定操作系统是否存在是使用OEM密钥安装的。 If there's an OEM as the second part of that key, then you return true, otherwise false. 如果有一个OEM作为该密钥的第二部分,则返回true,否则返回false。

Of course, that section of the key could also be numeric, with certain numeric values indicating an OEM key. 当然,密钥的该部分也可以是数字,某些数值表示OEM密钥。 For example, I remember that certain values in the 600s indicate OEM keys, but I couldn't tell you exactly which ones. 例如,我记得600s中的某些值表示OEM密钥,但我无法准确地告诉您哪些。 Hopefully you have a master list of all of these (from where, I wonder?), and are checking against them as well. 希望你有一个所有这些的主列表(从哪里,我想知道?),并正在检查它们。 Otherwise, you'll get a number of false negatives. 否则,你会得到一些漏报。

Anyway, assuming that's your approach (and it's the only one I can think of), the question becomes "How can I obtain the Windows product ID without using WMI?" 无论如何,假设这是你的方法(而且这是我能想到的唯一方法),问题就变成了“如何在不使用WMI的情况下获取Windows产品ID?”

You could try querying the registry for it, but this is a fragile approach. 您可以尝试查询注册表,但这是一种脆弱的方法。 It's not documented to be in a certain location in the registry, so this is subject to change between Windows versions. 它没有记录在注册表中的某个位置,因此这可能会在Windows版本之间发生变化。 On current versions, it's in 在当前版本中,它在

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId

To make this work, you'll also have to properly handle 32-bit vs 64-bit, and your application will require elevation—only administrators can muck about with HKEY_LOCAL_MACHINE . 为了使其工作,您还必须正确处理32位与64位,并且您的应用程序将需要仅提升管理员可以使用HKEY_LOCAL_MACHINE

As far as I know, this information is not otherwise exposed by the Win32 API. 据我所知,Win32 API没有公开此信息。 Certainly there is no IsOEMVersion function, and it's neither one of the members of the OSVERSIONINFOEX structure nor one of the flags used by the IsOS function. 当然没有IsOEMVersion函数,它既不是OSVERSIONINFOEX结构的成员之一,也不是IsOS函数使用的标志之一。

There is, obviously, a reason that WMI exists. 显然,WMI存在的原因。 If it does what you want, then you should use it. 如果它能满足您的需求,那么您应该使用它。 There's nothing inherently "unsafe" about it. 关于它没有任何内在的“不安全”。 If the information you wish to query requires certain privileges to access, well then it does for a reason and you're not going to have much success in finding alternate routes of accessing it that bypass those security restrictions. 如果您要查询的信息需要某些特权才能访问,那么它确实存在,并且您不会在寻找绕过这些安全限制的替代访问路径方面取得很大成功。

If it is absolutely necessary that you have partially-trusted code harness the vast power of WMI, you can create an intermediate fully-trusted assembly that exposes the necessary functionality, and then call that from the partially-trusted assembly. 如果绝对有必要使用部分受信任的代码来利用WMI的强大功能,则可以创建一个中间完全受信任的程序集,该程序集公开必要的功能,然后从部分受信任的程序集中调用它。

But in this particular case, you should seriously question why you need or want this information. 但在这种特殊情况下,您应该认真地质疑为什么需要或想要这些信息。 What difference does it make if it's an OEM version of the operating system? 如果它是操作系统的OEM版本,它会有什么不同? It behaves identically from your perspective (both as a user and as a developer). 它的行为与您的观点完全相同(作为用户和开发人员)。 If you'd given us more information about what you are planning to use this information to accomplish, we might be able to give some better suggestions. 如果您向我们提供了有关您计划使用此信息的更多信息,我们可能会提供一些更好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM