flat assembler
Message board for the users of flat assembler.

Index > Windows > Further begginer DLL trouble

Author
Thread Post new topic Reply to topic
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf 24 Nov 2006, 14:09
Hello,

I am (still) trying to make my first DLL, which is using API calls rather directly.

Here is my DLL code:
Code:
format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc' ;*

section '.idata' import data readable writeable

                library USER32,         'USER32.dll'

                import  USER32,\
                        MessageBox,     'MessageBoxA'

section '.main' code readable executable

proc DllEntryPoint      uses esi edi ebx,hinstDLL,fdwReason,lpvReserved
                mov     eax,1
                ret
endp

proc Plop         uses esi edi ebx,mesage
        mov     eax,[mesage]
        stdcall [MessageBox],0,eax,0,MB_OK
        ret
endp

section '.edata' export data readable

export  'DummyDLL.DLL',\
        Plop,        'Plop'    


And here is the code that uses it as a simple EXE (to ease debugging):
Code:
format PE GUI 4.0
entry zacetek

include 'win32a.inc' ;*

section '.idata' import data readable writeable

                library KERNEL32,       'KERNEL32.dll',\
                        Dummy,          'DummyDLL.DLL'

                import  KERNEL32,\
                        ExitProcess,    'ExitProcess'

                import  Dummy,\
                        Plop,           'Plop'

section '.main' code readable executable
zacetek:
                stdcall [Plop],entryText

                invoke  ExitProcess,0

fileName        db      'output.txt',0
entryText       db      'Works!',0    


I am debbugging this in Olly and all seems well (call in EXE executes, pushes before the MessageBox call push exactly the right values to the stack), except when the MessageBox API call itself is executed, the program crashes.

I have tried several variations in the code, based on suggestions in older posts here, but I can't figure out the problem. Could somebody please help allong?
Post 24 Nov 2006, 14:09
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 24 Nov 2006, 15:04
I said replace "invoke" with "stdcall" but now you have to do the opposite Laughing

In the other thread (if I understand it right), Plop proc was located at same source but in this case you are placing it in a DLL and calling it from an EXE trought the import table so there is no need for "stdcall [Plop]", you can use invoke which does the same but places the square brackets for you.

I can't see the problem, and I'm on Ubuntu now so I can't test
Post 24 Nov 2006, 15:04
View user's profile Send private message Reply with quote
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf 24 Nov 2006, 15:34
LocoDelAssembly wrote:
I said replace "invoke" with "stdcall" but now you have to do the opposite Laughing

In the other thread (if I understand it right), Plop proc was located at same source but in this case you are placing it in a DLL and calling it from an EXE trought the import table so there is no need for "stdcall [Plop]", you can use invoke which does the same but places the square brackets for you.

I can't see the problem, and I'm on Ubuntu now so I can't test


I considered that. So stdcalls replaced with invokes and [] around the function name removed, it still has the same problem.

Unless this kind of thing is a problem with the Olly debugger, it would seem the problem is in the point that the DLL's imports are never actually loaded, hence the call points to a memory location that isn't the "MessageBoxA" imported function. I'm thinking this can be overcome with something trivial but I don't know what it is. Razz
Post 24 Nov 2006, 15:34
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 24 Nov 2006, 16:03
add "section '.reloc' fixups data discardable" at the end of the DLL source
Post 24 Nov 2006, 16:03
View user's profile Send private message Reply with quote
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf 24 Nov 2006, 16:05
LocoDelAssembly wrote:
add "section '.reloc' fixups data discardable" at the end of the DLL source


Whoa it works! Smile

And what does this code do ? Razz Beside making space at the end of the DLL? Could I have guessed it? ;P
Post 24 Nov 2006, 16:05
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 24 Nov 2006, 16:10
Since a DLL can be loaded at a different base address of the one that fasm has choosen you need to add that section to make Windows able to relocate absolute referencies (like call dword ptr [function] where function label is an absolute offset). Note that I copied it from DLL example of fasm package because I didn't realise this error neather Razz
Post 24 Nov 2006, 16:10
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.