2017-10-18 37 views
1

我拿了一个示例代码,它从FASM示例目录创建一个简单的DLL,并根据我的需要进行调整。然而,当我做一些(从我的POV无辜)更改,生成的二进制文件被损坏 - 运行使用此库的exe生成错误代码0xC000007B又名INVALID_IMAGE_FORMAT。调整示例DLL代码时获取无效图像

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错误,还是我做错了什么?

+1

此代码根本不涉及调用约定(特别是'stdcall',其中'DllEntryPoint'需要)。 “JMP”对于DLL函数('ShowLastError')是不安全的,您需要'INVOKE',就像您使用其他导入的DLL函数('FormatMessage'和'MessageBox')一样。特别是因为'ShowLastError'带有一个你没有传递给它的输入参数 –

+0

@RemyLebeau这个DLL甚至不会被加载,所以这与我的问题是正交的。 – arrowd

+1

它仍然值得修复。在任何情况下,“INVALID_IMAGE_FORMAT”的罪魁祸首都是一个加载64位DLL的32位进程,反之亦然。这是这种情况吗? –

回答

1

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

section '.reloc' fixups data readable discardable 

只是

data fixups 
end data 

修复该问题。

相关问题