Reusable functions: simulate a Windows loader to load PE files

Source: Internet
Author: User
/** Description: Simulate the Windows reprinter to load the PE file, which makes it easier to process the PE file. Parameter: szfilename: PE file path return: if the operation succeeds, the first address of the PE file image in the memory is returned. If the operation fails, 0 */ulong mappefile (tchar * szfilename) {handle hfile; ulong numr; image_dos_header dosheader = {0} is returned }; image_nt_headers ntheader; ulong dwimagesize; ulong dwnumofsections; ulong dwheadsize; callback ction; char * pbase; ulong nindex; hfile = createfile (szfilename, generic_read | generic_write, file_assist_r EAD | file_share_write, null, open_existing, file_attribute_normal, null); If (hfile = invalid_handle_value) {outputdebugstring (_ T ("failed to open the file! "); Return 0;} readfile (hfile, & dosheader, sizeof (image_dos_header), & numr, null); setfilepointer (hfile, dosheader. e_lfanew, null, file_begin); readfile (hfile, & ntheader, sizeof (image_nt_headers), & numr, null); dwimagesize = ntheader. optionalheader. sizeofimage; dwnumofsections = ntheader. fileheader. numberofsections; dwheadsize = ntheader. optionalheader. sizeofheaders; pbase = (char *) malloc (dwimagesize); memset (pbase, 0, dwimagesize); setfilepointer (hfile, 0, null, file_begin); readfile (hfile, pbase, dwheadsize, & numr, null); Direction ction = (pimage_section_header) (dosheader. e_lfanew + sizeof (ntheader. signature) + sizeof (image_file_header) + ntheader. fileheader. sizeofoptionalheader + (ulong) pbase); For (nindex = 0, direction; nindex <dwnumofsections; nindex ++, direction ++) {setfilepointer (hfile, Direction-> pointertorawdata, null, file_begin); readfile (hfile, & pbase [ction-> virtualaddress], ction-> sizeofrawdata, & numr, null);} closehandle (hfile); Return (ulong) pbase ;}

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.