方法就是通过读取程序文件的头部来判断,具体代码如下:
1 #include <stdio.h> 2 #include <windows.h> 3 4 int CrnGetImageFileMachine(LPCSTR lpFileName) 5 { 6 IMAGE_DOS_HEADER idh={0}; 7 FILE *f = fopen(lpFileName, "rb"); 8 fread(&idh, sizeof(idh), 1, f); 9 IMAGE_FILE_HEADER ifh; 10 fseek(f, idh.e_lfanew + 4, SEEK_SET); 11 fread(&ifh, sizeof(ifh), 1, f); 12 fclose(f); 13 return ifh.Machine; 14 } 15 16 int main(int c,char** v) 17 { 18 if(c != 2) 19 { 20 printf("usage: %s exepath/dllpath ",v[0]); 21 getchar(); 22 return 0; 23 } 24 printf("你查看的文件是: %s ",v[1]); 25 printf("它的位数是:"); 26 int n = CrnGetImageFileMachine(v[1]); 27 if (n == 0x014C) 28 printf("x86"); 29 else if (n == 0x0200) 30 printf("IA64"); 31 else if (n == 0x8664) 32 printf("x64"); 33 else 34 printf("Unknown"); 35 36 printf(" "); 37 getchar(); 38 return 0; 39 }