flat assembler
Message board for the users of flat assembler.

Index > Windows > Cannot load DLL written in FASM

Author
Thread Post new topic Reply to topic
abc101



Joined: 31 Jan 2011
Posts: 5
abc101
I'm encountering a problem. I wrote simple DLL in FASM and then tried to load it in C++.NET, but an error occured (something like "The program format is incorrect"). Can anyone help me? Here's the code:

Code:
 format PE DLL
entry DllEntryPoint

include "win32a.inc"

section ".code" code readable executable

proc DllEntryPoint hDll:DWORD, reason:DWORD, reserved:DWORD
  mov eax,TRUE
  ret
endp

proc SampleProc
  mov eax, 5
  ret
endp

section ".edata" export readable writable

export "Example.DLL",\
  SampleProc,"SampleProc"

section '.reloc' fixups data discardable      
Post 31 Jan 2011, 11:16
View user's profile Send private message Reply with quote
MSWarrior



Joined: 21 Mar 2008
Posts: 11
MSWarrior
I think that you must have at least one function imported for the DLL to work.
Try to create import section with just one function and see if it works.
Post 31 Jan 2011, 11:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17271
Location: In your JS exploiting you and your system
revolution
The DLL opens fine with OllyDbg under WinXPSP2. Perhaps something is different/wrong with C++.NET?
Post 31 Jan 2011, 11:49
View user's profile Send private message Visit poster's website Reply with quote
abc101



Joined: 31 Jan 2011
Posts: 5
abc101
Quote:
I think that you must have at least one function imported for the DLL to work.
Try to create import section with just one function and see if it works.


Well, when I added an API call, everything became OK:

Code:
format PE DLL
entry DllEntryPoint

include "win32a.inc"

section ".code" code readable executable

proc DllEntryPoint hDll:DWORD, reason:DWORD, reserved:DWORD
  mov eax,TRUE
  ret
endp

proc SampleProc
  invoke GetLastError
  mov eax, 5
  ret
endp

section ".idata" import readable writable

library kernel32,"KERNEL32.DLL",\
user32,"USER32.DLL"

include "api\kernel32.inc"
include "api\user32.inc"

section ".edata" export readable writable

export "Example.DLL",\
  SampleProc,"SampleProc"

section '.reloc' fixups data discardable    


But this "invoke GetLastError" is senseless and it makes the program slow. Sad
Post 31 Jan 2011, 11:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17271
Location: In your JS exploiting you and your system
revolution
abc101 wrote:
But this "invoke GetLastError" is senseless ...
Agreed. Blame C++.NET.
Post 31 Jan 2011, 11:59
View user's profile Send private message Visit poster's website Reply with quote
abc101



Joined: 31 Jan 2011
Posts: 5
abc101
Quote:
The DLL opens fine with OllyDbg under WinXPSP2. Perhaps something is different/wrong with C++.NET?


C++ works correctly. I tried to write the same DLL in MASM and C++ loaded it successfully.
Post 31 Jan 2011, 11:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17271
Location: In your JS exploiting you and your system
revolution
abc101 wrote:
C++ works correctly. I tried to write the same DLL in MASM and C++ loaded it successfully.
Maybe MASM puts an import section in the binary? Check the binary to see.
Post 31 Jan 2011, 12:00
View user's profile Send private message Visit poster's website Reply with quote
abc101



Joined: 31 Jan 2011
Posts: 5
abc101
Quote:

Maybe MASM puts an import section in the binary? Check the binary to see.


There's 2 sections only in the MASM DLL: ".text" and ".rdata". No ".reloc", ".idata" or something else.
Post 31 Jan 2011, 12:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17271
Location: In your JS exploiting you and your system
revolution
I don't know the reason. But if you are worried about speed problems you can do this:
Code:
;...
proc DllEntryPoint hDll:DWORD, reason:DWORD, reserved:DWORD
  mov eax,TRUE
  ret
  invoke GetLastError
endp

proc SampleProc
  mov eax, 5
  ret
endp
;...    
Post 31 Jan 2011, 12:09
View user's profile Send private message Visit poster's website Reply with quote
abc101



Joined: 31 Jan 2011
Posts: 5
abc101
Quote:

;...
proc DllEntryPoint hDll:DWORD, reason:DWORD, reserved:DWORD
mov eax,TRUE
ret
invoke GetLastError
endp


It really works. Smile Maybe it's not exactly what I wanted, but thanks anyway. Smile
Post 31 Jan 2011, 12:25
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.