C + + Judge PE file instance _c language

Source: Internet
Author: User
Tags readfile

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.