How to tell if a file is an EXE or a DLL?

How to tell if a file is an EXE or a DLL?

 1 void DumpFile(LPWSTR filename)
 2 {
 3     HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
 4 
 5     HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
 6 
 7     LPVOID lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);    
 8 
 9     PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
10 
11     PIMAGE_NT_HEADERS pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader + (DWORD)pDosHeader->e_lfanew);
12 
13     if ((pNTHeader->FileHeader.Characteristics & IMAGE_FILE_DLL))
14          printf("dll"); 
15     if ((pNTHeader->FileHeader.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE))
16          printf("exe"); 
17     else 
18          printf("????");
19 
20     UnmapViewOfFile(lpFileBase);
21     CloseHandle(hFileMapping);
22     CloseHandle(hFile);
23 }

 

posted @ 2015-09-06 17:24  IAmAProgrammer  阅读(601)  评论(0编辑  收藏  举报