Does anyone know how to setup a prototype for a function retrived by GetProcAddress ???
Posted on 2001-12-24 17:17:02 by Axial
GetProcAddress proto :DWORD,:DWORD
Posted on 2001-12-24 18:45:12 by The Svin
Hope this helps, this shows how to use
invoke with GetProcAddress functions..
enjoy prs.


.586p
.model flat, stdcall

include windows.inc
include kernel32.inc

includelib kernel32.lib

;use the TYPEDEF directive to make IVP = a invokable function that takes 4 params
IVP TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD

.data

szFunctionName db "MessageBoxA",0
szUser32 db "User32.dll",0
hLib dd ?


.code


start:

;Get our library handle
invoke LoadLibrary, addr szUser32
mov hLib, eax

;get the address for our messagebox fucntion
invoke GetProcAddress, eax, addr szFunctionName

;now tell masm to treat eax as an invokable function,
;useing the ivp as the prototype
assume eax : IVP

;invoke eax :)
;try changing the number of parameters, masm will complain
;about invalid number of arguments
invoke eax, NULL, addr szUser32, addr szFunctionName, MB_OK

;now tell masm that eax is nothing
assume eax : nothing

;free our lib
invoke FreeLibrary, hLib

;were done here get out
invoke ExitProcess, eax
end start
Posted on 2001-12-24 20:29:14 by prs
Of course, you could always use the classic 'call' style instead of 'invoke'. Assuming, of course, that you knew what parameters were required - but you wouldn't be using the function anyway if you didn't :P i.e.:

include windows.inc
include kernel32.inc

includelib kernel32.lib

.data
szLibName DB "library.dll", 0
szPrcName DB "FunctionName", 0

.code

lea eax, szLibName
push eax
call LoadLibrary ; this is the same as:
; 'invoke LoadLibrary, ADDR szLibName'

cmp eax, NULL
je some_error_handling_stuff

lea ebx, szPrcName
push ebx
push eax ; remember eax is the handle of library.dll
call GetProcAddress ; likewise, this is the same as:
; 'invoke GetProcAddress, eax, ebx'

cmp eax, NULL
je more_error_handling_stuff

; here, you could do the following to store the function for later
; usage ... remember, pFunction would be a DWORD/DD
;
; mov pFunction, eax

push 3 ; FunctionName, parameter 3
push 2 ; FunctionName, parameter 2
push 1 ; FunctionName, parameter 1
call eax ; same as 'invoke FunctionName, 1, 2, 3'

; you could, if using the 'pFunction' variable above, do the
; following:
;
; call pFunction
;
; Can you invoke it? :)
;

; ... and the application termination code goes down here or
; whatever
Posted on 2001-12-25 03:22:49 by krumms
Ditto what krumms said, you shouldn't have to set up a prototype, your code could look like this:



invoke GetProcAddress, ...blah..., ...blah...
.IF eax
call dword ptr [eax]
.ENDIF


Be aware that what i have just suggested is heavily influenced by alcohol ;)
Posted on 2001-12-25 06:27:42 by sluggy
Thanks for replies (expecially in that day !)

prs:

I like your method, it showed me a new MASM directive TYPEDEF (same as C++ I suppose), and made me think about something:

I was wondering if a directive like "reinterpret cast" in C++, could exist in Masm ???

If yes, the following code may work


newMessageBox PROTO :DWORD,:DWORD,:DWORD,:DWORD
szMessageBoxA db "MessageBoxA",0
pMessageBox dd ? ;

invoke GetProcAddress, hLib, addr szMessageBoxA
mov pMessageBox,eax

reinterpret_cast pMessageBox, newMessageBox; ?????

invoke pMessageBox,0, addr text, addr cap, 0
[...]



But it's still optional since your code works well.


Axial
Posted on 2001-12-25 15:16:40 by Axial