#include <windows.h>
#include <stdio.h>
int main(int argc,char **argv)
{
PIMAGE_DOS_HEADER pImageDosHeader;
HANDLE hFile;
HANDLE hMapObject;
PUCHAR uFileMap;
if (argc<2)
{
return -1;
}
if (!(hFile=CreateFile(argv[1],GENERIC_READ,0,NULL,OPEN_EXISTING,0,0)))
{
return -1;
}
if (!(hMapObject = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL)))
{
return -1;
}
if (!(uFileMap=MapViewOfFile(hMapObject,FILE_MAP_READ,0,0,0)))
{
return -1;
}
pImageDosHeader = (PIMAGE_DOS_HEADER)uFileMap;
if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
{
return -1;
}
printf("e_magic: 0x%04X (%c%c)
", pImageDosHeader->e_magic, *uFileMap, *(uFileMap + 1));
printf("e_lfanew: 0x%08X
",pImageDosHeader->e_lfanew);
UnmapViewOfFile(uFileMap);
CloseHandle(hMapObject);
CloseHandle(hFile);
return 0;
}
在VS2010上运行出问题,解决办法:
项目名称右键选择"属性"-“链接器”-"系统" 找到"子系统",将其改为 “控制台 (/SUBSYSTEM:CONSOLE)”</span>