Check if unmanaged DLL is 32-bit or 64-bit?

后端 未结 5 1734
一生所求
一生所求 2020-11-30 21:47

How can I programmatically tell in C# if an unmanaged DLL file is x86 or x64?

5条回答
  •  囚心锁ツ
    2020-11-30 22:03

    I know it has been a while since this was updated. I was able to get away with the "Bad Image Format" exceptions by loading the file into it's own AppDomain.

            private static (string pkName, string imName) FindPEKind(string filename)
        {
            // some files, especially if loaded into memory
            // can cause errors. Thus, load into their own appdomain
            AppDomain tempDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
            PEWorkerClass remoteWorker =
                (PEWorkerClass)tempDomain.CreateInstanceAndUnwrap(
                    typeof(PEWorkerClass).Assembly.FullName,
                    typeof(PEWorkerClass).FullName);
    
            (string pkName, string imName) = remoteWorker.TryReflectionOnlyLoadFrom_GetManagedType(filename);
    
            AppDomain.Unload(tempDomain);
            return (pkName, imName);
        }
    

    At this point, I do the following:

            public (string pkName, string imName) TryReflectionOnlyLoadFrom_GetManagedType(string fileName)
        {
            string pkName;
            string imName;
            try
            {
                Assembly assembly = Assembly.ReflectionOnlyLoadFrom(assemblyFile: fileName);
                assembly.ManifestModule.GetPEKind(
                    peKind: out PortableExecutableKinds peKind,
                    machine: out ImageFileMachine imageFileMachine);
    
                // Any CPU builds are reported as 32bit.
                // 32bit builds will have more value for PortableExecutableKinds
                if (peKind == PortableExecutableKinds.ILOnly && imageFileMachine == ImageFileMachine.I386)
                {
                    pkName = "AnyCPU";
                    imName = "";
                }
                else
                {
                    PortableExecutableKindsNames.TryGetValue(
                        key: peKind,
                        value: out pkName);
                    if (string.IsNullOrEmpty(value: pkName))
                    {
                        pkName = "*** ERROR ***";
                    }
    
                    ImageFileMachineNames.TryGetValue(
                        key: imageFileMachine,
                        value: out imName);
                    if (string.IsNullOrEmpty(value: pkName))
                    {
                        imName = "*** ERROR ***";
                    }
                }
    
                return (pkName, imName);
            }
            catch (Exception ex)
            {
                return (ExceptionHelper(ex), "");
            }
        }
    

    Running this against my Widows\Assembly directory gives me zero errors with over 3600 files processed. note: I use a dictionary to load the values being returned.

    I hope it helps. YMMV

提交回复
热议问题