flat assembler
Message board for the users of flat assembler.

Index > Windows > DLL Question

Author
Thread Post new topic Reply to topic
Todd



Joined: 05 Oct 2003
Posts: 10
Location: US
Todd 11 Oct 2003, 17:36
Hi, I'm new with fasm, and win32 in general, but I was wondering how to export a function from a Dll, and to have it return a value. For anyone who knows a little C++, here's what I'm trying to say:

Code:
int function_name(int param1, int param2);
    


Now, I just need to know how to do that in Fasm, and export it from a Dll for use in another program. The examples that come with fasm only show how to use a VOID type of return value, I need an Int. Thanks in advance for any help you can give me.
Post 11 Oct 2003, 17:36
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 11 Oct 2003, 18:15
Follow the scheme as the fasm example dll.
Then make your function and to get a return value, store it in eax before returning (ret).

If your return value is not an integer, use eax value as pointer to your custom data structure.

Assembly makes no distinction between VOID and other functions. The difference is in the caller (eax value is neglected). You can use other registers but remember: working in Win32 requires that you preserve them.
(If you change the contents of eax, ecx, edx is OK, but others could cause an undesired effect)

Regards,
Post 11 Oct 2003, 18:15
View user's profile Send private message Yahoo Messenger Reply with quote
Todd



Joined: 05 Oct 2003
Posts: 10
Location: US
Todd 11 Oct 2003, 20:22
Thank you for your reply, that helps me out some. I've tried this out, but I can't seem to get it to work. Maybe someone could tell me what it is I'm doing wrong here. I realize that this function does nothing, but I'm just testing returning a value. When I import this function into another program it generates an error, usually something like "testdll1.dll can't load at the desired address". Here's my code:

Code:
format PE GUI 4.0 DLL
entry DllEntryPoint
include '%include%\win32a.inc'

section '.code' code readable executable
   proc DllEntryPoint, hinstDLL,fdwReason,lpvReserved
      enter
      mov eax,TRUE
      return

   proc TestFunction
      enter
      mov eax,65
      return

section '.edata' export data readable
   export 'testdll1.dll',\
          TestFunction,'TestFunction'

section '.reloc' fixups data discardable
    
Post 11 Oct 2003, 20:22
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 11 Oct 2003, 22:37
For some obscure reason, Windows requires that you import at least one function from the win API, also if you don't need it. So this will work for you:
Code:
format PE GUI 4.0 DLL 
entry DllEntryPoint 
include '%include%\win32a.inc' 

section '.code' code readable executable 
   proc DllEntryPoint, hinstDLL,fdwReason,lpvReserved 
      enter 
      invoke GetModuleHandle
      mov eax,TRUE 
      return 

   proc TestFunction 
      enter 
      mov eax,65 
      return 

section '.edata' export data readable 
   export 'testdll1.dll',\ 
          TestFunction,'TestFunction' 

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'
  import kernel,GetModuleHandle,'GetModuleHandleA'

section '.reloc' fixups data discardable 
    
Post 11 Oct 2003, 22:37
View user's profile Send private message Yahoo Messenger Reply with quote
Todd



Joined: 05 Oct 2003
Posts: 10
Location: US
Todd 12 Oct 2003, 00:52
Oh, that seems like a very strange rule, but it works. Thanks!
Post 12 Oct 2003, 00:52
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.