windows - 调整示例 DLL 代码时获取无效图像

标签 windows winapi assembly dll fasm

我使用了一个示例代码,该代码从 FASM 示例目录创建了一个简单的 DLL,并根据我的需要对其进行了调整。但是,当我进行一些(从我的 POV 来看是无辜的)更改时,生成的二进制文件会损坏 - 运行使用此库的 exe 会产生错误代码 0xC000007B 又名 INVALID_IMAGE_FORMAT。

DLL代码:

; DLL creation example

format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc'

section '.text' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

proc ShowErrorMessage hWnd,dwError
  local lpBuffer:DWORD
        lea     eax,[lpBuffer]
        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
        ret
endp

proc ShowLastError hWnd
        ret
endp

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         GetLastError,'GetLastError',\
         SetLastError,'SetLastError',\
         FormatMessage,'FormatMessageA',\
         LocalFree,'LocalFree'

  import user,\
         MessageBox,'MessageBoxA'

section '.edata' export data readable

  export 'DLL.DLL',ShowErrorMessage,'ShowErrorMessage',ShowLastError,'ShowLastError'

section '.reloc' fixups data readable discardable

可执行代码:

format PE GUI 4.0
entry start

include 'win32a.inc'

section '.text' code readable executable

  start:
jmp ShowLastError

section '.idata' import data readable writeable

  library mydll,'DLL.DLL'

  import mydll,\
         ShowLastError,'ShowLastError'

当我改变时,说,

export 'DLL.DLL',ShowErrorMessage,'ShowErrorMessage',ShowLastError,'ShowLastError'

行至

export 'DLL.DLL',ShowLastError,'ShowLastError'

代码损坏。如果我将 ShowErrorMessage 正文更改为 ret,也会发生同样的情况。

我对此完全困惑。这是 FASM 错误,还是我做错了什么?

最佳答案

我无法找到对此的解释,但至少我找到了解决方法。更改以下行

section '.reloc' fixups data readable discardable

只是

data fixups
end data

解决了问题。

关于windows - 调整示例 DLL 代码时获取无效图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46810589/

相关文章:

windows - .data 部分是否加载到内存中?

PHP hash_hmac() 操作系统差异

C# WinAPI 点击菜单项

c++ - 如何比系统更喜欢 AddFontMemResourceEx 加载的字体?

visual-studio - 为什么寄存器 ax 中的内存整数之和是正确的,而寄存器 eax 中的内存整数之和却不是正确的?

assembly - 汇编器无法打开文件

windows - 如何查到来电信息?

windows - 使用 WSL 和 postgresql 安装 rails

c# - Windows 8 Metro TextBlock 动画

c++ - 在 Windows 中获取目录大小是否有比遍历其所有子项更快的方法?