private static string GetCurrentProcessArchitecture()
        {
            string str = null;
            IntPtr module = NativeMethodsShared.LoadLibrary("kernel32.dll");
            try
            {
                if (!(module != NativeMethodsShared.NullIntPtr))
                {
                    return str;
                }
                IntPtr procAddress = NativeMethodsShared.GetProcAddress(module, "IsWow64Process");
                if (procAddress == NativeMethodsShared.NullIntPtr)
                {
                    return "x86";
                }
                IsWow64ProcessDelegate delegateForFunctionPointer = (IsWow64ProcessDelegate) Marshal.GetDelegateForFunctionPointer(procAddress, typeof(IsWow64ProcessDelegate));
                bool flag = false;
                if (!delegateForFunctionPointer(Process.GetCurrentProcess().Handle, out flag))
                {
                    return str;
                }
                if (flag)
                {
                    return "x86";
                }
                NativeMethodsShared.SYSTEM_INFO lpSystemInfo = new NativeMethodsShared.SYSTEM_INFO();
                NativeMethodsShared.GetSystemInfo(ref lpSystemInfo);
                switch (lpSystemInfo.wProcessorArchitecture)
                {
                    case 0:
                        return "x86";

                    case 6:
                        return "IA64";

                    case 9:
                        return "AMD64";
                }
                str = null;
            }
            finally
            {
                if (module != NativeMethodsShared.NullIntPtr)
                {
                    NativeMethodsShared.FreeLibrary(module);
                }
            }
            return str;
        }
 public void ValidateCurrentProcessorArchitectureCall()
 {
     NativeMethodsShared.SYSTEM_INFO systemInfo = new NativeMethodsShared.SYSTEM_INFO();
     NativeMethodsShared.GetSystemInfo(ref systemInfo);
     Assert.AreEqual(ProcessorArchitectureIntToString(systemInfo), BuildUtilities.ProcessorArchitecture.CurrentProcessArchitecture, "BuildUtilities.ProcessorArchitecture.CurrentProcessArchitecture returned an invalid match");
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the processor architecture of the currently running process
        /// </summary>
        /// <returns>null if unknown architecture or error, one of the known architectures otherwise</returns>
        static private string GetCurrentProcessArchitecture()
        {
            string architecture = null;

            IntPtr kernel32Dll = NativeMethodsShared.LoadLibrary("kernel32.dll");
            try
            {
                if (kernel32Dll != NativeMethodsShared.NullIntPtr)
                {
                    // This method gets the current architecture from the currently running msbuild.
                    // If the entry point is missing, we're running on Kernel older than WinXP
                    // http://msdn.microsoft.com/en-us/library/ms684139.aspx
                    IntPtr isWow64ProcessHandle = NativeMethodsShared.GetProcAddress(kernel32Dll, "IsWow64Process");

                    if (isWow64ProcessHandle == NativeMethodsShared.NullIntPtr)
                    {
                        architecture = ProcessorArchitecture.X86;
                    }
                    else
                    {
                        // entry point present, check if running in WOW
                        IsWow64ProcessDelegate isWow64Process = (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer(isWow64ProcessHandle, typeof(IsWow64ProcessDelegate));
                        bool isWow64 = false;
                        bool success = isWow64Process(Process.GetCurrentProcess().Handle, out isWow64);

                        if (success)
                        {
                            // if it's running on WOW, must be an x86 process
                            if (isWow64)
                            {
                                architecture = ProcessorArchitecture.X86;
                            }
                            else
                            {
                                // it's a native process. Check the system architecture to determine the process architecture.
                                NativeMethodsShared.SYSTEM_INFO systemInfo = new NativeMethodsShared.SYSTEM_INFO();

                                NativeMethodsShared.GetSystemInfo(ref systemInfo);

                                switch (systemInfo.wProcessorArchitecture)
                                {
                                    case NativeMethodsShared.PROCESSOR_ARCHITECTURE_INTEL:
                                        architecture = ProcessorArchitecture.X86;
                                        break;

                                    case NativeMethodsShared.PROCESSOR_ARCHITECTURE_AMD64:
                                        architecture = ProcessorArchitecture.AMD64;
                                        break;

                                    case NativeMethodsShared.PROCESSOR_ARCHITECTURE_IA64:
                                        architecture = ProcessorArchitecture.IA64;
                                        break;

                                    case NativeMethodsShared.PROCESSOR_ARCHITECTURE_ARM:
                                        architecture = ProcessorArchitecture.ARM;
                                        break;

                                    // unknown architecture? return null
                                    default:
                                        architecture = null;
                                        break;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (kernel32Dll != NativeMethodsShared.NullIntPtr)
                {
                    NativeMethodsShared.FreeLibrary(kernel32Dll);
                }
            }

            return architecture;
        }