Author Topic: DLL IMPORT AND EXPORT In NASM Under -Fwin32  (Read 27332 times)

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
« Reply #15 on: September 18, 2017, 09:02:34 PM »
@Dibya
Since you're not showing me your code, there's not much I can do. But I wrote a simple but classic example of creating a DLL library and linking to multiple libraries as an example and reference. You can use it to slowly extend the imports and externs one at a time to add more library as a practice and see how it goes. All of these are using GoLink as the main linker.

The library (prog.dll):
Code: [Select]
;----------------------
;Creating a DLL library with 2 services
;1. to print a null-terminated string
;2. To exit the code gracefully
;-----------------------
;nasm -f win32 prog.asm
;golink /dll prog.obj kernel32.dll msvcrt.dll
;-----------------------
global MyExit
export MyExit
global MyPrint
export MyPrint

extern _ExitProcess@4  ;from kernel32
extern printf          ;from msvcrt (C)

section .text

;To print an ASCIIZ string
;Push the address of the string
MyPrint:
        push    ebp
        mov     ebp,esp

        mov     esi,[ebp+8]
        push    esi
        call    printf
        add     esp,4   ;CDECL clean up

        mov     esp,ebp
        pop     ebp
        ret     4       ;stdcall clean up

;Exit the code
;No arguments
MyExit:
        push    0
        call    _ExitProcess@4
        ret

Now an example on how to access the library (prog.dll) AND another library from "user32.dll" just to make it more interesting;

Code: [Select]
;--------------------------------
;To access services provided by prog.dll (the lib)
;nasm -f win32 test.asm
;golink /console test.obj prog.dll user32.dll
;--------------------------------
global Start

extern MyExit           ;import from prog.dll
extern MyPrint          ;import from prog.dll
extern _MessageBoxA@16  ;import from user32.dll

section .data
hello db 'Hello World',0ah,0
msg db 'Hello',0

section .text
Start:
        push    hello
        call    MyPrint

        push    0
        push    msg
        push    msg
        push    0
        call    _MessageBoxA@16

        call    MyExit

Take it slowly and don't miss out any details.

Enjoy.


Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
« Reply #16 on: September 19, 2017, 04:30:19 AM »
Thanks Dreamcoder
Now i got it .
Now i can work on developing a visual studio like IDE for nasm ..
It will also help on my other projects
Thanks a lot
Edit ::
I have tried to use the dll in a c program which just call the dll .
When i used a c dll with xor eax,eax ret 4 (if error success message hello will be displayed ) my program says hello but when i use dll compiled with nasm i get badimage error and program does not work .
Edit:: I got it . I removed /mix and everything is fine .
Going some off topic
if a nasm IDE is required which features will be essential suggest me some thing i am going to work from tomorrow
« Last Edit: September 19, 2017, 06:15:21 AM by Dibya »