flat assembler
Message board for the users of flat assembler.

Index > Windows > Question about DLL linking... (newbie ;))

Author
Thread Post new topic Reply to topic
denial



Joined: 12 Sep 2004
Posts: 98
denial 13 Sep 2004, 12:46
Hello,

at the moment I'm trying to link a Visual C++ DLL into a Windows-Console FASM Application... I read through the PEDLL example that comes with FASM, however I did not understand everything. Is there a tutorial or something out?

My general questions I got:

- I saw in the example that the parameters for the linked functions (MessageBox,...) get pushed onto the stack. I was wondering how that exactly works and how the used function knows where to take them?

- What does these lines mean?

Code:
  dd RVA dlldemo_table,0,0,RVA dlldemo_name,RVA dlldemo_table
  dd 0,0,0,RVA kernel_name,RVA kernel_table
  dd 0,0,0,0,0
    


I can see that a double-word gets declared but I don't know what RVA means...
And what's about the last line? Looks strange to me Wink

It would be nice if you could help me.
thank you Smile[/code]
Post 13 Sep 2004, 12:46
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 13 Sep 2004, 14:54
Hi denial,
In PEDLL import section is just simply built manually. You simply don't need to understand what are those zeroes Smile Try this example:
Code:
format PE DLL
entry DLLEntryPoint

include "%fasminc%/win32a.inc"

; data section
section '.data' data readable writeable

szTitle db "DLL example", 0
szText  db "Hello form DLL!", 0


; code section
section '.code' code readable executable

proc DLLEntryPoint, hinstDLL,fdwReason,lpvReserved
 enter
       mov     eax,1
       return
endp


proc Hello
        enter
       invoke  MessageBox, HWND_DESKTOP,szText,szTitle,MB_OK+MB_ICONINFORMATION
    return
endp

; imports
section '.idata' import data readable
library user32,"USER32.DLL"

import user32,\
       MessageBox,"MessageBoxA"

; exports
section '.edata' export data readable

export "SIMPLEDLL.DLL",\
       Hello,"Hello"

; fixup section - needed in DLL
section '.reloc' fixups data discardable    
Post 13 Sep 2004, 14:54
View user's profile Send private message Visit poster's website Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial 13 Sep 2004, 15:10
Thank you for your answer,
but if I'm not confused about your example now, I think you got me wrong Wink

This is an example how to write a DLL in FASM isn't it? What I need is more information about use a DLL in FASM written in another language, Visual C++ for example.
Maybe you or somebody help can help me?
Post 13 Sep 2004, 15:10
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 13 Sep 2004, 15:20
You can import functions from any dll, exactly the way you do it for Windows system dll's (user32.dll for example). You have to create import section (or "data import..." block in any other section and write there the libraryes and functions you want to import. See the syntax of "library" and "import" macroses (include/macro/import.inc file), they are pretty clear (IMHO).
Don't be afraid to import all functions from the DLL in one block (better create one big .inc file for the whole DLL) - FASM will include in import data only the functions you use in your program. See for example the include files for Windows system dll's: include/apia/user32.inc

Regards.

P.S. Of course you can load dll's dynamicaly using Windows API functions: LoadLibrary, LoadLibraryEx, GetProcAddress, FreeLibrary. Read Win32 API guide for details.
Post 13 Sep 2004, 15:20
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial 13 Sep 2004, 16:21
Thank you Smile
Could you explain the parameters for these two macros (import, library)?

macro library [name,string]
name is of course the file, but what is string?

macro import name,[label,string]
name is here the name of the function... What is label and string for?

Thank you again Wink
Post 13 Sep 2004, 16:21
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 13 Sep 2004, 16:33
see an example:
Code:
library kernel32,"KERNEL32.DLL"    

name (kernel32) is just some name (that will be used with import macro) and "string" is actual DLL file name.

Code:
import kernel32,\
ExitProcess,"ExitProcess"    

with import macro we specify which functions to load from a DLL. In this case we import ExitProcess from kernel32.dll. "name" is the same name that you specified as first parameter of library macro. Then "label" is a name that you will refer to when calling given function; and finally "string" is a function name in a DLL.

Hope my description was clear enough Smile
Post 13 Sep 2004, 16:33
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 13 Sep 2004, 17:34
Code:
; In you import section

; Also, you can specify more than one library:
library  library1, 'mylib1.dll', library2, 'yourlib2.dll'

;... or better (more readable):
library  library1, 'mylib1.dll',  \
         library2, 'yourlib2.dll'

;...and more that one function per "import" statement...
import library1,    \
       Function1, 'SomeFunctionName',  \
       Function2, 'SomeOtherFunction',  \
       TheBestFunc, 'SlowCPPFunc'
; Note - it is not obligatory to use the same names for functions as they are defined in the dll.

; In your code section

; call these functions this way:

    invoke  Function1, arg1, arg2  ; macro "invoke"
; or:
    stdcall   [Function2], arg1, arg2  ; macro "stdcall" with indirect addressing.
; or even:
    push arg2
    push arg1
    call  [TheBestFunc]  ; instruction "call"
    


Regards
Post 13 Sep 2004, 17:34
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial 13 Sep 2004, 17:43
Thank you very, very much for your help... Now I understood everything. Smile
It's a great bord here - the support is fast and friendly. Smile

Regards
denial
Post 13 Sep 2004, 17:43
View user's profile Send private message Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial 13 Sep 2004, 18:38
Oh I got one more problem I posted but I solved it myself now. Thank you.
Post 13 Sep 2004, 18:38
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.