如何确定本地DLL文件是否编译为x64或x86?

151

我想确定一个本地汇编程序是以 x64 还是 x86 编译的,从一个托管代码应用程序 (C#) 中进行。

我认为这个信息应该在 PE 头文件中某个位置,因为操作系统加载器需要知道这个信息,但我找不到它。当然,我更喜欢在托管代码中完成,但如果必要,我可以使用本机 C++。


请明确一下,所提到的dll也是一个.Net程序集吗?您在帖子标题中说原生DLL,但在描述中说原生程序集…如果您仍然在关注这篇来自09年的帖子,请澄清一下。 - Vikas Gupta
1
你可能也想查看这个链接:check-if-unmanaged-dll-is-32-bit-or-64-bit - Matt
12个回答

1
由于第三方工具总是安装在 %Program files (x86)% 中(即使是 x64 安装!)并且需要适当的 x32|x64 fortran 运行时在 %path% 中被首先列出才能正确运行,我将 c++powershell 的解决方案整合到 Matlab 中以返回以下内容:
  • 类型: 可执行文件|库|其他
  • 代码类型: 本地|混合|托管
  • 平台: x32|x64|AnyCpu|x32Preferred|其他
一旦在内存中有原始 PE 结构,就应该很容易适应其他语言。
function [simplifiedInfo] = GetPortableExecutableSimplifiedInfo(filename)
%[
    % Checking arguments
    if (nargin <1), error('MATLAB:minrhs', 'Not enough input argments.'); end

    % Initializing simplified info    
    simplifiedInfo.Kind = 'Other';
    simplifiedInfo.CodeType = 'Other';
    simplifiedInfo.Platform = 'Other';

    % Obtaining raw info
    [rawInfo, PEConstants] = GetPortableExecutableRawInfo(filename);

    % Determining 'Kind' of PE
    if (isfield(rawInfo, 'PEOptionalHeader') && (rawInfo.COFFHeader.Characteristics.IMAGE_FILE_EXECUTABLE_IMAGE))
        if (rawInfo.COFFHeader.Characteristics.IMAGE_FILE_DLL)
            simplifiedInfo.Kind = 'Library';
        else
            simplifiedInfo.Kind = 'Executable';
        end
    else
        % No optional header or no IMAGE_FILE_EXECUTABLE_IMAGE flag ... 
        % Maybe just .obj or other thing
        simplifiedInfo.Kind = 'Other';
    end

    % Determining 'CodeType'
    % NB: 'COR20Header' is present for MSIL code, but not for native code
    if (isfield(rawInfo, 'COR20Header'))
        if (rawInfo.COR20Header.Flags.COMIMAGE_FLAGS_ILONLY)
            simplifiedInfo.CodeType = 'Managed';
        else
            simplifiedInfo.CodeType = 'Mixed';
        end
    else
        simplifiedInfo.CodeType = 'Native';
    end

    % Determining platform
    if (rawInfo.COFFHeader.Machine == PEConstants.IMAGE_FILE_MACHINE_AMD64)
        simplifiedInfo.Platform = 'x64';
    elseif (rawInfo.COFFHeader.Machine == PEConstants.IMAGE_FILE_MACHINE_I386)
        if (isfield(rawInfo, 'COR20Header'))
            % PE contains MSIL code, need more checks
            if (rawInfo.COR20Header.Flags.COMIMAGE_FLAGS_32BITREQUIRED)
                if (rawInfo.COR20Header.Flags.COMIMAGE_FLAGS_32BITPREFERRED)
                    simplifiedInfo.Platform = 'x32Preferred';
                else
                    simplifiedInfo.Platform = 'x32';
                end
            else
                simplifiedInfo.Platform = 'AnyCpu';
            end
        else
            % This is native code so ...
            simplifiedInfo.Platform = 'x32';
        end
    else
        % ARM, ...
        simplifiedInfo.Platform = 'Other';
    end
%]
end

可以在这里获取内部GetPortableExecutableRawInfo函数的源代码。


0

显然,你可以在可移植可执行文件的头部找到它。corflags.exe实用程序能够向你展示它是否针对x64。希望这能帮助你找到更多关于它的信息。


3
谢谢Steven,但是corflags.exe不能用于本地程序集。 - Ohad Horesh
1
Windows 10:>corflags libzmq.dll \n\n ... corflags:错误 CF008:指定的文件没有有效的托管头部。 - Grault

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接