Hi.
I've never used GetProcAddress before. Can someone show me something
like a "Hello World!" MessageBox, with LoadLibrary,GetProcAddress, FreeLibrary?

Thanx in advance! :alright:
Posted on 2001-07-19 20:32:14 by bazik
Here.
Posted on 2001-07-19 21:00:22 by comrade
Bazik,

Just have a look in the MACRO file that Prostart produces as the macros use this method to call the dll that has the display function in it.

Its simple stuff to do and its very useful when you don't want a DLL loaded all the time in an app.

Regards,

hutch@pbq.com.au

EXAMPLE



ShowReturn MACRO hWindow, value
LOCAL lbl
LOCAL LibName
LOCAL ProcName
jmp lbl
LibName db "Dbshow.dll",0
ProcName db "ShowReturnValue",0
lbl:
pushad
push value
push hWindow
invoke LoadLibrary,ADDR LibName
invoke GetProcAddress,eax,ADDR ProcName
call eax
popad
ENDM
Posted on 2001-07-19 21:50:31 by hutch--
Oh yes, pretty wonderful macro, hutch :). First of all, you have no
error checks whatsoever. I guess a check after GetProcAddress
would suffer - but you really DO need a check there. Second, why
do you use the old way of push parms/call eax? Scali recently told
me about the TYPE keyword in masm -- know your system ;).
Posted on 2001-07-20 00:29:59 by f0dder
Comrade,
Many thanks! That helped my a lot! BTW, the "push eax" after LoadLibrary could be deleted :)

EDITED:
P.S.: Or is the "push eax" after LoadLibrary for the FreeLibrary?

f0dder,

...
Second, why do you use the old way of push parms/call eax?
...

Please tell us your way :grin:
Posted on 2001-07-20 03:04:17 by bazik
It's not MY way, it's scali who told me about it :). The idea is to do
a TYPE statement. If you have, for example, the address of
MessageBox in EAX, you could do:



INVOKE (TYPE MessageBox) PTR eax, NULL, ADDR text, ADDR caption, MB_OK


-- I haven't tried it yet, but I take scali's word that it works. Definitely
looks prettier than manual pushing.
Posted on 2001-07-20 05:33:42 by f0dder
bAZiK,

I push eax for FreeLibrary prematurely so I do not have to store the library handle into a variable.
Posted on 2001-07-20 12:53:03 by comrade
f0dder,

A basic lesson in coding, if you want it small and fast, DON'T make mistakes. That simple macro is from the Prostart code wizard and it ALWAYS has the correct DLL available.

I personally have no problems with the push/call syntax, MASM can do both with no problems but Bazik asked for an example, a simple one is a lot easier to understand.

A little second hand knowledge is dangerous, you must prototype the function being called from the DLL to use INVOKE,


ShowReturnValue typedef PROTO STDCALL :DWORD,:DWORD
ShowReturn equ <(type ShowReturnValue) PTR eax>

Then call the function in code,
.data
LibName db "Dbshow.dll",0
ProcName db "ShowReturnValue",0
.code

invoke LoadLibrary,ADDR LibName
mov hLib, eax
invoke GetProcAddress,hLib,ADDR ProcName
invoke ShowReturn,hWin,54321


Next question, where is the FreeLibrary() call ? The DLL exits after it is run so the DLL count is decremented without having to call it. :)

Perhaps if you bothered to read your MASM reference instead of trying to inflict C on people who wrote it while you were in napkins, you would not develop this cargo cult mentality. :tongue:

Regards,

hutch@pbq.com.au
Posted on 2001-07-21 07:15:42 by hutch--
Here is the simplified variation, it works fine but is probably not worth
the extra typing, push/call is simply faster to get going.


PROTOTYPE
~~~~~~~~~
ShowReturnValue typedef PROTO STDCALL :DWORD,:DWORD
ShowReturn equ <(type ShowReturnValue) PTR eax>

MACRO
~~~~~
LoadProcAddress MACRO quoted_text1,quoted_text2
LOCAL library_name
LOCAL proc_name
.data
library_name db quoted_text1,0
proc_name db quoted_text2,0
.code
invoke LoadLibrary,ADDR library_name
invoke GetProcAddress,eax,ADDR proc_name
ENDM

CODE
~~~~
LoadProcAddress "Dbshow.dll","ShowReturnValue"
invoke ShowReturn,hWin,12345

Regards,

hutch@pbq.com.au
Posted on 2001-07-21 07:56:38 by hutch--

A basic lesson in coding, if you want it small and fast, DON'T make mistakes.
That simple macro is from the Prostart code wizard and it ALWAYS has the correct
DLL available.


Sure, avoid errors. But it's not only your errors that could lead to LoadLibrary
failing. Never depend on windows succeeding if it CAN fail. And since bAZiK was
asking for an example that he'll probably use for generic things, mentioning
error checking is a pretty good idea.



I personally have no problems with the push/call syntax, MASM can do both with no
problems but Bazik asked for an example, a simple one is a lot easier to understand.


True, true. I just think the invoke syntax is prettier and easier to read, and it
requires less typing. It's of course only useful if you need to call (sorry, invoke :))
the function more than a few times in your program.



A little second hand knowledge is dangerous, you must prototype the function being called
from the DLL to use INVOKE,


I think that is pretty obvious from the "TYPE MessageBox" -- if you haven't defined the
MessageBox type, where would the prototype come from?



Next question, where is the FreeLibrary() call ? The DLL exits after it is run so the DLL
count is decremented without having to call it.


Does ShowReturnValue do a FreeLibrary on the DLL it's in? This is the only way I can
see how your statement would make sense :). Is this possible to do? I'm not saying it's
not, it just feels a bit like... pulling the rug away underneath your own feet. Neat
if it works, though.


By the way, if the macro is used multiple times, wouldn't it result in the library
name and procedure name multiple times in the executable? And don't you think it
would be better to make the macro save only eax, and the ShowReturnValue preserve
the ones it uses itself? Just a friendly comment, you know :).
Posted on 2001-07-21 09:00:18 by f0dder