#region CPL License /* Nuclex.NAnt.Tasks Copyright (C) 2009 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.ComponentModel; namespace Nuclex.NAnt.Tasks.Helpers { /// Helper class for environment variables internal static class EnvironmentHelper { /// Initializes the environment helper class static EnvironmentHelper() { // We can determine whethr this process is a 64-bit process by // checking the size of a pointer ExecutingAs64BitCode = (IntPtr.Size == 8); if(OsHelper.IsWindows) { if(OsHelper.IsXpOrLater) { // Finding out whether we're running as an x86 on a 64-bit OS // is a little bit more tricky and requires a Windows API call using(Process self = Process.GetCurrentProcess()) { if(!IsWow64Process(self.Handle, out UsingWoW6432)) { throw new Win32Exception( "Could not determine whether this is a WOW64 process" ); } } } } else { UsingWoW6432 = false; } // We're running on a 64 bit OS if either we're running as native // 64 bit code or when the OS tells us we're a WOW64 process. RunningOn64BitOS = ExecutingAs64BitCode || UsingWoW6432; } /// /// Determines whether the specified process is running under WOW64 /// /// Handle of the process that will be queried /// Receives the result of the query /// True if the function has succeeded [DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsWow64Process( [In] IntPtr processHandle, [Out] out bool wow64Process ); /// /// Whether the current application is running on a 64 bit operating system /// /// /// Warning: This does make no guarantees as to whether the current process is /// actually running as a 64 bit process. Unless explicitely flagged, .NET /// assemblies can run as either, 32 bit or 64 bit code, depending on /// the architecture of the entry assembly. /// public static readonly bool RunningOn64BitOS; /// /// Whether the currently executing assembly is running in 64 bit mode /// public static readonly bool ExecutingAs64BitCode; /// /// Whether the currently executing assembly is running as 32 bit code on /// a 64 bit operating system /// public static readonly bool UsingWoW6432; } } // namespace Nuclex.NAnt.Tasks.Helpers