Reputation: 27723
It can be done using WMI.
But is there a simple way (similar to Environment.Is64BitOperatingSystem
)?
EDIT: There are permission limitations on WMI. As far as I know Is64BitOperatingSystem
is much "better" in this respect, and maybe others. (Please correct me if I'm wrong.)
I'm trying to make sure the code will always run, without throwing exceptions.
Upvotes: 1
Views: 524
Reputation: 245001
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. The 64-bit version of the .NET Framework is hard-coded to return true, and the 32-bit version to return 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. If there's an OEM
as the second part of that key, then you return true, otherwise false.
Of course, that section of the key could also be numeric, with certain numeric values indicating an OEM key. For example, I remember that certain values in the 600s indicate OEM keys, but I couldn't tell you exactly which ones. 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?"
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. 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
.
As far as I know, this information is not otherwise exposed by the 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.
There is, obviously, a reason that WMI exists. 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.
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? 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.
Upvotes: 4