Search code examples
c#.net.net-assembly

Get value of prefer-32bit flag from assembly


I have simple installation program which chose folder to install and registry hive based on Environment.Is64BitOperatingSystem and ProcessorArchitecture of main exe file of installed application. E.g. if ProcessorArchitecture is MSIL and Windows is 64bit I know that application will be executed as 64bit and thus it should be installed in "C:\Program Files" and application should use 64bit view on windows registry.

But I have problem with assemblies compiled in .NET framework 4.5 with set build flag Prefer-32bit. Application like this will run on 64bit system as 32bit process and thus it should be stored in "Program Files (x86)" folder. However ProcessorArchitecture of this application will be still MSIL so my decision algorithm will fail. Is it possible to get Prefer-32bit flag from assembly?

Thanks


Solution

  • Module.GetPEKind method returns PortableExecutableKinds flags that contain a Preferred32Bit value.

    Assembly assembly = Assembly.LoadFile(file);
    Module manifest = assembly.ManifestModule;
    PortableExecutableKinds kind;
    ImageFileMachine platform;
    manifest.GetPEKind(out kind, out platform);
    if((kind & PortableExecutableKinds.Preferred32Bit) != 0)
    {
        //is Prefer-32bit
    }