69,374
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile;
DWORD dwFileSize;
//char szFileName[] = "D:\\Windows技术详解\\Result\\X64Test\\Debug\\X64Test1.exe";
char szFileName[] = "D:\\Windows技术详解\\Result\\X64Test\\Debug\\360杀毒64位.exe";
BOOL bRet = FALSE;
BOOL bFileX64 = FALSE;
HMODULE hLibrary;
hLibrary = LoadLibrary(szFileName);
if (NULL != hLibrary)
{
FreeLibrary(hLibrary);
bFileX64 = FALSE;
}
else
{
bFileX64 = TRUE;
}
hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
dwFileSize = GetFileSize(hFile, 0);
BYTE *pData = new BYTE[dwFileSize];
if (NULL == pData)
{
return bRet;
}
DWORD dwBytesRead = 0;
ReadFile(hFile, pData, dwFileSize, &dwBytesRead, NULL);
CloseHandle(hFile);
WORD wWord = *(WORD*)(pData + 0x3c);
wWord = wWord + 0x18;
WORD wWord1 = *(WORD*)(pData + wWord);
return 0;
}
IMAGE_DOS_HEADER DosHead = { 0 };
IMAGE_NT_HEADERS NtHead = { 0 };
IMAGE_SECTION_HEADER ImageSectionHeader = { 0 };
FILE* fp;
_wfopen_s(&fp, szFile, L"rb");
fread(&DosHead, sizeof(IMAGE_DOS_HEADER), 1, fp);
fseek(fp, DosHead.e_lfanew, SEEK_SET);
fread(&NtHead, sizeof(IMAGE_NT_HEADERS), 1, fp);
if (NtHead.FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
{
//32位
}
if (NtHead.FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
{
//64位
}
//读0x3C WORD
//这个WORD加上0x18
//再Seek到这个位置,读WORD
//0x1xx就是32位
//0x2xx就是64位
#pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS”
typedef unsigned short int WORD;
#include <stdio.h>
FILE *f;
WORD w;
int main(int argc,char **argv) {
if (argc==1) {
printf("Usage:\n %s filename.exe\nto show 32/64 bit program\n",argv[0]);
return 1;
}
f=fopen(argv[1],"rb");
if (NULL==f) {
printf("Can not open file %s\n",argv[1]);
return 2;
}
if (fseek(f,0x3C,SEEK_SET)) {
printf("Can not seek to 0x3C\n");
fclose(f);
return 3;
}
if (1!=fread(&w,2,1,f)) {
printf("Can not read a word at offset 0x3C\n");
fclose(f);
return 4;
}
if (fseek(f,w+0x18,SEEK_SET)) {
printf("Can not seek to word ptr (0x3C)+0x18\n");
fclose(f);
return 5;
}
if (1!=fread(&w,2,1,f)) {
printf("Can not read a word at offset word ptr (0x3C)+0x18\n");
fclose(f);
return 6;
}
fclose(f);
w&=0xFF00;
if (0x0100==w) printf("%s is a 32 bit program",argv[1]);
else if (0x0200==w) printf("%s is a 64 bit program",argv[1]);
else printf("%s is a 32/64 program",argv[1]);
return 0;
}