This article illustrates the method of C + + to judge PE file. Share to everyone for your reference.
The implementation methods are as follows:
#include <afxdlgs.h> is to use CFileDialog
Main.h is as follows:
Copy Code code as follows:
#include <afxwin.h>
Class Cmyapp:public CWINAPP
{
BOOL InitInstance ();
};
Main.cpp is as follows:
Copy Code code as follows:
#include "Main.h"
#include <afxdlgs.h>
CMyApp Theapp;
BOOL cmyapp::initinstance ()
{
CFileDialog Dlg (TRUE);
if (dlg. DoModal ()!= Idok)
{
return FALSE;
}
Open File
HANDLE hfile =:: CreateFile (dlg. GetPathName (), generic_read, file_share_read, NULL, open_existing, file_attribute_normal, NULL);
if (Invalid_handle_value = = hfile)
{
return FALSE;
}
Image_dos_header Dosheader;
Image_nt_headers32 Ntheader;
DWORD Dwreturn;
BOOL Bispe=false;
:: ReadFile (hfile, &dosheader, sizeof (Image_dos_header), &dwreturn, NULL);
if (Dwreturn = = sizeof (Image_dos_header))
{
if (dosheader.e_magic = = image_dos_signature)//mz
{
if ( -1!=:: SetFilePointer (hfile, Dosheader.e_lfanew, NULL, File_begin))
{
:: ReadFile (hfile, &ntheader, sizeof (IMAGE_NT_HEADERS32), &dwreturn, NULL);
if (Dwreturn = = sizeof (IMAGE_NT_HEADERS32))
{
if (ntheader.signature = = image_nt_signature)//pe
{
Bispe = TRUE;
}
}
}
}
}
:: CloseHandle (hfile); Write with the CreateFile area above
if (BISPE)
{
MessageBoxA (NULL, "is a PE file", "", MB_OK);
}
Else
{
MessageBoxA (NULL, "not a PE file", "", MB_OK);
}
return FALSE;
}
I hope this article will help you with the C + + program design.