方法就是通过读取程序文件的头部来判断,具体代码如下:
#include <stdio.h>
#include <windows.h> int CrnGetImageFileMachine(LPCSTR lpFileName)
{
IMAGE_DOS_HEADER idh={};
FILE *f = fopen(lpFileName, "rb");
fread(&idh, sizeof(idh), , f);
IMAGE_FILE_HEADER ifh;
fseek(f, idh.e_lfanew + , SEEK_SET);
fread(&ifh, sizeof(ifh), , f);
fclose(f);
return ifh.Machine;
} int main(int c,char** v)
{
if(c != )
{
printf("usage: %s exepath/dllpath\n",v[]);
getchar();
return ;
}
printf("你查看的文件是: %s\n",v[]);
printf("它的位数是:");
int n = CrnGetImageFileMachine(v[]);
if (n == 0x014C)
printf("x86");
else if (n == 0x0200)
printf("IA64");
else if (n == 0x8664)
printf("x64");
else
printf("Unknown"); printf("\n");
getchar();
return ;
}