There
are several ways to determine the 64 bit operating system on this post we will
look on two most preferred ways of detecting it in .Net framework.
Solution
1:
With
the help of following extern methods
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr GetModuleHandle(string
moduleName);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr
hModule, [MarshalAs(UnmanagedType.LPStr)]string procName);
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool
IsWow64Process([In] IntPtr
hProcess, [Out] out
bool lpSystemInfo);
static bool
DoesWin32MethodExist(string moduleName, string methodName)
{
IntPtr moduleHandle = GetModuleHandle(moduleName);
if (moduleHandle == IntPtr.Zero)
{
return false;
}
return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
}
static bool Is64Bit()
{
bool retVal;
IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
return retVal;
}
public static bool Is64BitOperatingSystem()
{
if (IntPtr.Size ==
8) // 64-bit
programs run only on Win64
{
return true;
}
else // 32-bit programs run on both 32-bit and 64-bit Windows
{
// Detect whether the current process is a 32-bit process
// running on a 64-bit system.
bool flag;
return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process")
&&
IsWow64Process(GetCurrentProcess(), out
flag)) && flag);
}
}
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.ToLongDateString());
bool f64bitOS = Is64BitOperatingSystem();
Console.WriteLine("The
current operating system {0} 64-bit.", f64bitOS ? "is" : "is
not");
}
Solution
2:
Determine whether the current operating system is a 64
bit operating system through WMI. The function is also able to query the bits
of OS on a remote machine.
public static bool Is64BitOperatingSystem(string
machineName, string domain, string userName, string
password)
{
ConnectionOptions options = null;
if (!string.IsNullOrEmpty(userName))
{
// Build a ConnectionOptions object for the remote
connection
// if you plan to connect to the remote with a different
user
// name and password than the one you are currently using.
options
= new ConnectionOptions();
options.Username
= userName;
options.Password
= password;
options.Authority = "NTLMDOMAIN:" + domain;
}
// Else the connection will use the currently logged-on
user
// Make a connection to the target computer.
ManagementScope scope = new
ManagementScope("\\\\"
+ (string.IsNullOrEmpty(machineName) ? "." : machineName) + "\\root\\cimv2", options);
scope.Connect();
// Query Win32_Processor.AddressWidth which dicates the
current
// operating mode of the processor (on a 32-bit OS, it
would be
// "32"; on a 64-bit OS, it would be
"64").
// Note: Win32_Processor.DataWidth indicates the capability
of
// the processor. On a 64-bit processor, it is
"64".
// Note: Win32_OperatingSystem.OSArchitecture tells the
bitness
// of OS too. On a 32-bit OS, it would be
"32-bit". However, it
// is only available on Windows Vista and newer OS.
ObjectQuery query = new
ObjectQuery("SELECT
AddressWidth FROM Win32_Processor");
// Perform the query and get the result.
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,
query);
ManagementObjectCollection queryCollection =
searcher.Get();
foreach (ManagementObject
queryObj in queryCollection)
{
if (queryObj["AddressWidth"].ToString()
== "64")
{
return true;
}
}
return false;
}
static void Main(string[] args)
{
// Solution 2. Is64BitOperatingSystem (WMI)
// Determine whether the current operating system is a 64
bit
// operating system through WMI. The function is also able
to
// query the bitness of OS on a remote machine.
try
{
f64bitOS
= Is64BitOperatingSystem(".", null, null, null);
Console.WriteLine("The
current operating system {0} 64-bit.", f64bitOS ? "is" : "is
not");
}
catch (Exception
ex)
{
Console.WriteLine("Is64BitOperatingSystem
throws the exception: {0}", ex.Message);
}
Console.ReadLine();
}
No comments:
Post a Comment