1

There is an enum of all supported processor architectures here: http://msdn.microsoft.com/en-us/library/system.reflection.processorarchitecture.aspx

Is there any way to determine which one corresponds to the running environment? System.Reflection.Assembly.GetExecutingAssembly().ProcessorArchitecture returns MSIL -- obviously wrong.

EDIT: Bojan Resnik posted an answer and deleted it. I see that some clarification is needed from the partial trace I got.

The assembly needs to run on multiple architectures and do different things based on what assembly instructions the running process accepts. Essentially, I need to select which version of a native DLL to load. I have one for each architecture.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • Dupe: http://stackoverflow.com/questions/767613/identifying-the-cpu-architecture-type-using-c – Mehrdad Afshari Sep 25 '09 at 16:46
  • Not a dupe. If I'm running in WOW I want x86 back not x64. – Joshua Sep 25 '09 at 17:28
  • Do you actually need to distinguish between all those architectures, or just x86/x64? – Pavel Minaev Sep 29 '09 at 22:52
  • I would like to give an appropriate error if I see Itanium rather than die due to loading X64 on Itanium. – Joshua Sep 29 '09 at 23:30
  • Related answer on [Identifying the CPU architecture type using C#](http://stackoverflow.com/a/25284569/1352471) which would also fit to this question, without p/invoke using Module.GetPEKind and ImageFileMachine enumeration! – metadings Aug 13 '14 at 11:44

3 Answers3

2

P/Invoking GetSystemInfo is trivial from .Net and is much lighter weight than WMI. Also, it returns the architecture as seen by the process so on a x64 machine a WOW process will see x86 and a native process will see x64.

Stephen Martin
  • 9,495
  • 3
  • 26
  • 36
0

You can use System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture for this. This was added to .NET in the .NET Framework 4.7.1.

Phil Deets
  • 86
  • 4
  • Somebody left a comment over here saying it doesn't work on ARM64; however none of this existed at the time of me asking the question anyway. You notice I talked about Itanium and not arm. https://stackoverflow.com/a/45373756/14768 ; not a bad try at updating the answer for newer platforms though. – Joshua Dec 02 '21 at 00:50
  • The question is specifically asking about what processor architecture is used by the process. System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture correctly returns x86 when using the .NET Framework on ARM64. The .NET Framework on ARM64 jits from MSIL to x86, which then is JIT'd again from x86 to ARM64 by Windows. Note that .NET Core 2.1 and newer do support JIT compiling directly to ARM64. – Phil Deets Dec 02 '21 at 00:52
  • Which would explode in my use case because I would then load a x86_32 native DLL into an ARM64 process. – Joshua Dec 02 '21 at 00:54
  • From Windows perspective, the .NET Framework process would be an x86 process which could load x86 native DLLs. You would actually fail to load ARM64 DLLs since it is an x86 process and x86 runs in a WOW64 container in ARM64. – Phil Deets Dec 02 '21 at 00:57
-1

Here's a few WMI settings you may want to try. I don't have a 64-bit system handy at the moment, but it should be easy to check. Source code is below. Note that you may end up having to use a combination of calls (e.g. one to find wow, another to find native 32 vs. 64, etc.).

Also, check out http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/b1cfef99-5247-47c5-93d4-31eb6849df48 for some more discussion.

using System;
using System.Management;
class Program
{
    static void Main(string[] args)
    {
        foreach (ManagementBaseObject o in new ManagementClass("Win32_OperatingSystem").GetInstances())
        {
            Console.WriteLine("Win32_OperatingSystem.OSArchitecture = " + o.Properties["OSArchitecture"].Value);
            break;
        }
        foreach (ManagementBaseObject o in new ManagementClass("Win32_ComputerSystem").GetInstances())
        {
            Console.WriteLine("Win32_ComputerSystem.SystemType = " + o.Properties["SystemType"].Value);
            break;
        }
        Console.ReadKey();
    }
}
Justin Grant
  • 44,807
  • 15
  • 124
  • 208