Win32 API用于判断给定的二进制文件(EXE或DLL)是x86、x64还是ia64

7

我正在寻找一种编程方式来判断一个二进制文件是x86、x64还是ia64。

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

背景:在尝试加载第三方dll之前,我需要确定它的位数。

感谢任何指导。


可能是重复的问题:如何查找本机dll是否编译为x64或x86? - mmmmmm
3个回答

13

对于EXE文件

可以使用GetBinaryType(...)函数。

这里有一个关于托管exe的同样的问题

对于DLL(和EXE)文件

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

这里是一些代码,我在Google Code Search中找到的。

没有清理和错误检查。

// 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 );

1
谢谢回复。看起来那个特定的API对于.dll文件会失败。 - user15071

2
我在 Github 上开源了一个项目,该项目专门检查 VC++ 可再发行 DLL,并基于 Shay 的回答创建了一个代码片段,成功地查找、加载和检查 DLL 的 x86/x64 兼容性。完整的代码片段如下:
/******************************************************************
Function Name:  CheckProductUsingCurrentDirectory
Description:    Queries the current working directory for a given binary.
Inputs:         pszProductFolderToCheck - the product name to look up.
pBinaryArchitecture - the desired processor architecture
of the binary (x86, x64, etc..).
Results:        true if the requested product is installed
false otherwise
******************************************************************/
bool CheckProductUsingCurrentDirectory(const LPCTSTR pszProductBinaryToCheck, Architecture pBinaryArchitecture){
        bool bFoundRequestedProduct = false;

        //Get the length of the buffer first
        TCHAR currentDirectory[MAX_PATH];
        DWORD currentDirectoryChars = GetCurrentDirectory(MAX_PATH, currentDirectory);

        //exit if couldn't get current directory
        if (currentDirectoryChars <= 0) return bFoundRequestedProduct;

        TCHAR searchPath[MAX_PATH];
        //exit if we couldn't combine the path to the requested binary
        if (PathCombine(searchPath, currentDirectory, pszProductBinaryToCheck) == NULL) return bFoundRequestedProduct;

        WIN32_FIND_DATA FindFileData;
        HANDLE hFind= FindFirstFile(searchPath, &FindFileData);

        //exit if the binary was not found
        if (hFind == INVALID_HANDLE_VALUE) return bFoundRequestedProduct;

        HANDLE hFile = CreateFile(searchPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
        if (hFile == INVALID_HANDLE_VALUE) goto cleanup;

        HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, pszProductBinaryToCheck);
        if (hMapping == INVALID_HANDLE_VALUE) goto cleanup;

        LPVOID addrHeader = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
        if (addrHeader == NULL) goto cleanup; //couldn't memory map the file

        PIMAGE_NT_HEADERS peHdr = ImageNtHeader(addrHeader);
        if (peHdr == NULL) goto cleanup; //couldn't read the header

        //Found the binary, AND its architecture matches. Success!
        if (peHdr->FileHeader.Machine == pBinaryArchitecture){
                bFoundRequestedProduct = true;
        }

cleanup: //release all of our handles
        FindClose(hFind);
        if (hFile != INVALID_HANDLE_VALUE)
                CloseHandle(hFile);
        if (hMapping != INVALID_HANDLE_VALUE)
                CloseHandle(hMapping);
        return bFoundRequestedProduct;
}

这个问题和Shay的答案在我创建这个项目时对我很有帮助,所以我想把这个项目发布在这里。

0

您可以检查PE头文件以读取IMAGE_FILE_MACHINE字段。这里有一个C#实现,应该很容易适应C++。


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