2017-10-18 79 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 

修復該問題。