C語言程式設計獲取PE檔案DOS頭

星空你好發表於2016-02-02
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _tmain(int argc, TCHAR *argv[])
{
	PIMAGE_DOS_HEADER pImageDosHeader;
	HANDLE hFile;
	HANDLE hMapObject;
	PUCHAR uFileMap;
	DWORD dw;

	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)))
	{
		dw = GetLastError(); 
		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)\n", pImageDosHeader->e_magic, *uFileMap, *(uFileMap + 1));
	printf("e_lfanew:	0x%08X\n",pImageDosHeader->e_lfanew);
	UnmapViewOfFile(uFileMap);
	CloseHandle(hMapObject);
	CloseHandle(hFile);
	return 0;
}

相關文章