我想确定一个本地汇编程序是以 x64 还是 x86 编译的,从一个托管代码应用程序 (C#) 中进行。
我认为这个信息应该在 PE 头文件中某个位置,因为操作系统加载器需要知道这个信息,但我找不到它。当然,我更喜欢在托管代码中完成,但如果必要,我可以使用本机 C++。
我想确定一个本地汇编程序是以 x64 还是 x86 编译的,从一个托管代码应用程序 (C#) 中进行。
我认为这个信息应该在 PE 头文件中某个位置,因为操作系统加载器需要知道这个信息,但我找不到它。当然,我更喜欢在托管代码中完成,但如果必要,我可以使用本机 C++。
%Program files (x86)%
中(即使是 x64 安装!)并且需要适当的 x32|x64 fortran 运行时在 %path%
中被首先列出才能正确运行,我将 c++ 和 powershell 的解决方案整合到 Matlab 中以返回以下内容:
可执行文件|库|其他
本地|混合|托管
x32|x64|AnyCpu|x32Preferred|其他
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
函数的源代码。
显然,你可以在可移植可执行文件的头部找到它。corflags.exe实用程序能够向你展示它是否针对x64。希望这能帮助你找到更多关于它的信息。
>corflags libzmq.dll \n\n ... corflags:错误 CF008:指定的文件没有有效的托管头部。
- Grault