WIN32汇编语言教程:第17章 PE文件 · 17.1 PE文件的结构(7)

;####################################################################
_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
;####################################################################
; 错误 Handler
;####################################################################
_Handler   proc      _lpExceptionRecord,_lpSEH,\
                          _lpContext,_lpDispatcherContext
 
                  pushad
                  mov    esi,_lpExceptionRecord
                  mov    edi,_lpContext
                  assume esi:ptr EXCEPTION_RECORD,edi:ptr CONTEXT
                  mov    eax,_lpSEH
                  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,ExceptionContinueExecution
                  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
;********************************************************************
; 打开文件并建立文件 Mapping
;********************************************************************
                  invoke CreateFile,addr szFileName,GENERIC_READ,\
                         FILE_SHARE_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,0
                                  .if    eax
                                         mov    @lpMemory,eax
;********************************************************************
; 创建用于错误处理的 SEH 结构
;********************************************************************
              assume fs:nothing
             push      ebp
              push      offset _ErrFormat
              push      offset _Handler
              push      fs:[0]
              mov    fs:[0],esp
;********************************************************************
; 检测 PE 文件是否有效
;********************************************************************
              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

上页:第17章 PE文件 · 17.1 PE文件的结构(6) 下页:第17章 PE文件 · 17.1 PE文件的结构(8)

第17章 PE文件

版权所有 © 云南伯恩科技 证书:粤ICP备09170368号