用于判断给定二进制文件(EXE 或 DLL)是 x86、x64 还是 ia64 的 Win32 API [英] Win32 API to tell whether a given binary (EXE or DLL) is x86, x64, or ia64

查看:26
本文介绍了用于判断给定二进制文件(EXE 或 DLL)是 x86、x64 还是 ia64 的 Win32 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种编程方式来判断二进制文件是 x86、x64 还是 ia64.

I am trying to find a programmatic way to tell if a binary is x86, x64, or ia64.

平台:Windows.语言:c/c++.

Platform: Windows. Language: c/c++.

背景:在尝试加载第三方 dll 之前,我需要找出它的位数.

Background: Before trying to load a third-party dll, I need to find out its bitness.

感谢任何指点.

推荐答案

对于 EXE

使用 GetBinaryType(...)

这是同样的问题 用于托管 exe.

使用 ImageNtHeader(...) 获取文件的 PE 数据,然后检查 IMAGE_FILE_HEADER.Machine 字段.

Use the ImageNtHeader(...) to get the PE data of the file and then check the IMAGE_FILE_HEADER.Machine field.

这是我使用 Google 代码搜索找到的一些代码

无需清理和错误检查

// map the file to our address space
// first, create a file mapping object
hMap = CreateFileMapping( 
  hFile, 
  NULL,           // security attrs
  PAGE_READONLY,  // protection flags
  0,              // max size - high DWORD
  0,              // max size - low DWORD      
  NULL );         // mapping name - not used

// next, map the file to our address space
void* mapAddr = MapViewOfFileEx( 
  hMap,             // mapping object
  FILE_MAP_READ,  // desired access
  0,              // loc to map - hi DWORD
  0,              // loc to map - lo DWORD
  0,              // #bytes to map - 0=all
  NULL );         // suggested map addr

peHdr = ImageNtHeader( mapAddr );

这篇关于用于判断给定二进制文件(EXE 或 DLL)是 x86、x64 还是 ia64 的 Win32 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆