NASM Forum > Programming with NASM

Import by ordinal

<< < (2/2)

Mathi:

--- Quote ---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?.

--- End quote ---

Thats correct.
We need to make sure that we give the correct Function Name and dll name.
Otherwise we will get runtime errors. (program won't start).

"The procedure entry point <apifunction> could not be located in the dynamic link library <dllname>.   etc.

Regarding the call by Ordinal .
It can be simplified if you are using the nagoa+.inc include file with couple of macros.



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

%include "nagoa+.inc"

%macro GETORDINAL 2
%ifndef GETORDINAL_IMPORT
extern GetProcAddress
import GetProcAddress kernel32.dll

extern LoadLibraryA
import LoadLibraryA kernel32.dll

%define GETORDINAL_IMPORT
%endif
call LoadLibraryA,%1
test eax,eax
jz %%ExitError
call GetProcAddress,eax,%2
%%ExitError:
%endmacro

%macro CALLORD 2-*
GETORDINAL %1,%2
test eax,eax
jz %%ExitError

%assign iparm %0
%if %0 > 1
%rep %0 - 2
%rotate -1 
%assign iparm iparm-1                                
%rotate -1
%ifidni %1,"ADDR_"
%rotate 1
lea eax, [%1]
push eax
%rotate -1
%assign iparm iparm-1
%else
%rotate 1
STDPUSH {%1}     
%endif
%if iparm <=1
%exitrep
%endif
%endrep
%rotate -1
%endif
CALL eax   ;; Caps for actual CALL instruction
%%ExitError:
%endmacro

global ..start

segment .code USE32 class=code

..start:

CALLORD "user32.dll",2039,0,"HelloWorld!","Hello",MB_OK

ret

--- End code ---

wyvern666:
WOW, that is pretty cool. Thank you very much. And then i will try the IMPORT with COFF format too.

wyvern666:
Well, sadly IMPORT only work with "obj" (OMF) format. Any knows why?

Frank Kotler:
I don't know why, but you're correct - "import", as a Nasm keyword, is specific to "-f obj". Unless I'm mistaken, the NASMX package has "import" as a macro. I don't know if it does the same thing...

Best,
Frank

Rob Neff:
NASMX's import macro defines the appropriate external linkages to the shared libraries.  For Windows we take advantage of the automatic resolving of the name table by GoLink which is also distributed with the package.  It will not work with Alink without some effort.  I'm not inclined to provide such effort for a tool such as Alink that's long considered dead.

Navigation

[0] Message Index

[*] Previous page

Go to full version