NASM Forum > Programming with NASM

Import by ordinal

(1/2) > >>

wyvern666:
Anybody knows how to import by ordinal with NASM and ALINK?. Currently the only way i can do it is writing the import section by hand, but that is a lot of ugly work.

Thanks.

Mathi:
I guess you are trying to do something like this.
MessageBox is called by ordinal in this example. 
[Ordinal will change depending on the dll version. We might need to use tools like PE/DLL Viewer to get the correct one.]


--- Code: ---;;Assemble with:
;;nasm -fobj HelloOrd.asm
;;Link with:
;;alink -c -oPE -subsys gui HelloOrd.obj

%include "win32n.inc"

extern GetProcAddress
import GetProcAddress kernel32.dll

extern LoadLibraryA
import LoadLibraryA kernel32.dll

global ..start

segment .data USE32

message db "Hello World!",0
msgtitle db "Hello",0

dllname1 db "user32.dll",0
apiname1 db "MessageBoxA",0

hMod dd 0

OrdinalMsgBox dd 0

segment .bss USE32


segment .code USE32 class=code

..start:

push dword dllname1
call [LoadLibraryA]

mov [hMod],eax  ;; Save Module Handle for User32.dll


push dword 2039         ;; Alternatively use function name ;;;  push dword apiname1
push dword [hMod]
call [GetProcAddress]   ;; Get the Address of MessageBox api function.
mov [OrdinalMsgBox],eax
cmp eax,0
jz ExitOut              ;; if zero then something is wrong

push dword MB_OK
push dword msgtitle
push dword message
push dword 0
call [OrdinalMsgBox]   ;; call the function.

ExitOut:
ret

--- End code ---

wyvern666:
Hi Mathi, thank for your answer. I forgot to say i know i can use LoadLibrary and GetProcAddress, its a more dinamical way to do the job at runtime. But i was pretending something more "direct" and easy, for example en GOASM you just write CALL "[KERNEL32.DLL:248]" and there you go. Mmm maybe some macros can simplifiy this job. Anyway, by now, i will stick with LoadLibrary.  :D

Frank Kotler:
What little information I found suggests that Alink won't do this, but GoLink will... but you knew that!

I found a reference to a program that is supposed to do it after the fact:

http://www.masm32.com/board/index.php?topic=381.0

I wonder if that would help?

From what I found, it doesn't improve performance all that much anyway...

Best,
Frank

wyvern666:
Yap, i see now that importing by ordinal require extra work in NASM, MASM and some others assemblers. By the way, and off-topic, i am curious about some little thing in Mathi's code  ???:

--- Code: ---extern GetProcAddress
import GetProcAddress kernel32.dll  ; ???

--- End code ---

I never used "IMPORT" in NASM... Usually i just use EXTERN and then i add neccesary .lib files in ALINK command line. But from what im trying right now with this code it seems like when you declare "EXTERN & IMPORT" you dont need to link with .lib files, so you just used "alink -c -oPE -subsys gui HelloOrd.obj" and thats all. Im wrong?.

Navigation

[0] Message Index

[#] Next page

Go to full version