Add executable code (Win32 ASM) to the PE File)

Source: Internet
Author: User

; Main. ASM
PE File Operation demo Main Program, provides the dialog box interface and file opening function
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
; Use nmake or the following command to compile and link:
; Ml/C/coff main. ASM
; RC main. RC
; Link/subsystem: Windows main. OBJ main. Res
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
. 386
. Model flat, stdcall
Option Casemap: None
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include File Definition
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
Include windows. inc
Include user32.inc
Includelib user32.lib
Include kernel32.inc
Includelib kernel32.lib
Include comdlg32.inc
Includelib comdlg32.lib
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ Equivalent Definition
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
Ico_main equ 1000
Dlg_main equ 1000
Idc_info equ 1001
Idm_main equ 2000
Idm_open equ 2001
Idm_exit equ 2002
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
; Data Segment
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
. Data?
Hinstance dd?
Hrichedit dd?
Hwinmain dd?
Hwinedit dd?
Szfilename dB max_path DUP (?)

. Const
Szdlledit dB 'riched20. dll ', 0
Szclassedit dB 'richedit20a ', 0
Szfont dB 'body', 0
Szextpe dB 'pe files', 0, '*. EXE; *. dll; *. scr; *. Fon; *. drv', 0
DB 'all files (*. *) ', 0,' *. * ', 0, 0
The szerr db' file format is incorrect! ', 0
Szerrformat db' is not a file in PE format! ', 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
; Code segment
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>

. Code
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
_ Appendinfo proc _ lpsz
Local @ stcr: charrange

Pushad
Invoke getwindowtextlength, hwinedit
MoV @ stcr. CPMin, eax
MoV @ stcr. cpmax, eax
Invoke sendmessage, hwinedit, em_exsetsel, 0, ADDR @ stcr
Invoke sendmessage, hwinedit, em_replacesel, false, _ lpsz
Popad
RET

_ Appendinfo endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
Include _ processpefile. ASM
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
_ Init proc
Local @ stcf: charformat

Invoke getdlgitem, hwinmain, idc_info
MoV hwinedit, eax
Invoke loadicon, hinstance, ico_main
Invoke sendmessage, hwinmain, wm_seticon, icon_big, eax
Invoke sendmessage, hwinedit, em_settextmode, tm_plaintext, 0
Invoke rtlzeromemory, ADDR @ stcf, sizeof @ stcf
MoV @ stcf. cbsize, sizeof @ stcf
MoV @ stcf. yheight, 9*20
MoV @ stcf. dwmask, cfm_face or cfm_size or cfm_bold
Invoke lstrcpy, ADDR @ stcf. szfacename, ADDR szfont
Invoke sendmessage, hwinedit, em_setcharformat, 0, ADDR @ stcf
Invoke sendmessage, hwinedit, em_exlimittext, 0,-1
RET

_ Init endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
; Error handler
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
_ Handler proc _ lpexceptionrecord, _ l1_h, _ lpcontext, _ lpdispatchercontext

Pushad
MoV ESI, _ lpexceptionrecord
MoV EDI, _ lpcontext
Assume ESI: PTR exception_record, EDI: PTR Context
MoV eax, _ l1_h
Push [eax + 0ch]
Pop [EDI]. regebp
Push [eax + 8]
Pop [EDI]. regeip
Push eax
Pop [EDI]. regesp
Assume ESI: Nothing, EDI: Nothing
Popad
MoV eax, predictioncontinueexecution
RET

_ Handler endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
_ Openfile proc
Local @ stof: openfilename
Local @ hfile, @ dwfilesize, @ hmapfile, @ lpmemory

Invoke rtlzeromemory, ADDR @ stof, sizeof @ stof
MoV @ stof. lstructsize, sizeof @ stof
Push hwinmain
Pop @ stof. hwndowner
MoV @ stof. lpstrfilter, offset szextpe
MoV @ stof. lpstrfile, offset szfilename
MoV @ stof. nmaxfile, max_path
MoV @ stof. Flags, ofn_pathmustexist or ofn_filemustexist
Invoke getopenfilename, ADDR @ stof
. If! Eax
JMP @ F
. Endif
; **************************************** ****************************
; Open the file and create the file mapping
; **************************************** ****************************
Invoke createfile, ADDR szfilename, generic_read, file_assist_read or/
File_share_write, null, open_existing, file_attribute_archive, null
. If eax! = Invalid_handle_value
MoV @ hfile, eax
Invoke getfilesize, eax, null
MoV @ dwfilesize, eax
. If eax
Invoke createfilemapping, @ hfile, null, page_readonly, 0, 0, null
. If eax
MoV @ hmapfile, eax
Invoke mapviewoffile, eax, file_map_read, 0, 0
. If eax
MoV @ lpmemory, eax
; **************************************** ****************************
; Create a seh structure for error handling
; **************************************** ****************************
Assume FS: Nothing
Push EBP
Push offset _ errformat
Push offset _ Handler
Push FS: [0]
MoV FS: [0], ESP
; **************************************** ****************************
; Check whether PE files are valid
; **************************************** ****************************
MoV ESI, @ lpmemory
Assume ESI: PTR image_dos_header
. If [esi]. e_magic! = Image_dos_signature
JMP _ errformat
. Endif
Add ESI, [esi]. e_lfanew
Assume ESI: PTR image_nt_headers
. If [esi]. signature! = Image_nt_signature
JMP _ errformat
. Endif
Invoke _ processpefile, @ lpmemory, ESI, @ dwfilesize
JMP _ errorexit
_ Errformat:
Invoke MessageBox, hwinmain, ADDR szerrformat, null, mb_ OK
_ Errorexit:
Pop FS: [0]
Add ESP, 0ch
Invoke unmapviewoffile, @ lpmemory
. Endif
Invoke closehandle, @ hmapfile
. Endif
Invoke closehandle, @ hfile
. Endif
. Endif
@@:
RET

_ Openfile endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
_ Procdlgmain proc uses ebx edi esi hwnd, wmsg, wparam, lparam

MoV eax, wmsg
. If eax = wm_close
Invoke enddialog, hwnd, null
. Elseif eax = wm_initdialog
Push hwnd
Pop hwinmain
Call _ init
. Elseif eax = wm_command
MoV eax, wparam
. If AX = idm_open
Call _ openfile
. Elseif AX = idm_exit
Invoke enddialog, hwnd, null
. Endif
. Else
MoV eax, false
RET
. Endif
MoV eax, true
RET

_ Procdlgmain endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
Start:
Invoke loadlibrary, offset szdlledit
MoV hrichedit, eax
Invoke getmodulehandle, null
MoV hinstance, eax
Invoke dialogboxparam, hinstance, dlg_main, null, offset _ procdlgmain, null
Invoke freelibrary, hrichedit
Invoke exitprocess, null
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
End start

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.