The MASM Forum

General => The Campus => Topic started by: Lightman on December 09, 2015, 08:12:34 PM

Title: Calling functions in DLL files.
Post by: Lightman on December 09, 2015, 08:12:34 PM
Hello Everyone,

Following Iczelion's Tutorial 17 shows me how to create and use a DLL file, which is cool. The implementation of which goes something like this...


...
; Load up the Library...
invoke   LoadLibrary, addr myLibrary
mov     hLib, eax

; Get the address of the function I'd like to call
invoke   GetProcAddress, hLib, addr myfunction
mov   FuncAddress, eax

; run it...
call   [FuncAddress]

; Clear up when I'm done.
invoke   FreeLibrary, hLib
 

..which is all good. I complete the tutorial and it all works. So, moving on I get to Tutorial 24. In this case I need to use a DLL file again for Windows Hooks. But this time, when coding the program that calls the DLL file, things are a little different. This time Iczelion has two extra files... a .inc and a .lib.. They are with the other libraries that an assembler program might call. When calling the functions from the DLL file, Iczelion is using invoke rather than call.

The .inc file is a text file containing the function prototypes, but the .lib appears to be compiled. Where did he get that file from? This seems to be a much easier way of handling DLLs I have created, but how is this done?

Title: Re: Calling functions in DLL files.
Post by: TWell on December 09, 2015, 08:48:42 PM
link.exe with -dll generate import-library for you.
Title: Re: Calling functions in DLL files.
Post by: Lightman on December 09, 2015, 09:00:36 PM
Ah, cool. I'm a RadASM user, so my my linker entry looks like this from the Project->Project Options... Menu (DLL Release Settings)...

/SECTION:.bss,S /SUBSYSTEM:WINDOWS /RELEASE /VERSION:4.0 /DLL /DEF:$D /OUT:$O $C $M $R -dll

...and it works. Many thanks.
Title: Re: Calling functions in DLL files.
Post by: TWell on December 09, 2015, 09:12:26 PM
That /DLL is same option as -dll so you can remove last one

EDIT: If someone needs undecorated names in dll , use DEF-file and don't use keyword EXPORT in asm-file
EXPORTS
TestF = _TestF@0
Title: Re: Calling functions in DLL files.
Post by: dedndave on December 09, 2015, 09:50:49 PM
the windows API's are also in DLL's
the best way to create import libraries is to use Erol's tools

http://www.vortex.masmcode.com/ (http://www.vortex.masmcode.com/)

in particular, the Def2Lib program is useful for this
Erol's tool creates smaller import libraries

Dll2Inc is another useful tool
it creates DEF and INC files from a DLL
Title: Re: Calling functions in DLL files.
Post by: six_L on December 10, 2015, 02:06:24 AM
for ex.


proto_PGetRawInputData typedef proto stdcall :HRAWINPUT,:DWORD,:LPVOID,:PUINT,:DWORD
PGetRawInputData typedef ptr proto_PGetRawInputData

.data?
_GetRawInputData PGetRawInputData ?

.code

GetApiAdd proc dllname:DWORD,procname:DWORD
Local hdll:HWND

invoke LoadLibrary,dllname
.if eax!=0
mov hdll,eax
invoke GetProcAddress,hdll,procname
push eax
invoke FreeLibrary,hdll
pop eax
.endif
ret
GetApiAdd endp

invoke GetApiAdd,CTXT("User32.dll"),CTXT("GetRawInputData")
.if eax==0
mov _GetRawInputData,eax
invoke ErrorMessage,CTXT("GetRawInputData")
.else
mov _GetRawInputData,eax
.endif
....
invoke _GetRawInputData,lParam,RID_INPUT,NULL,addr dwSize,sizeof RAWINPUTHEADER