Does anyone know how to setup a prototype for a function retrived by GetProcAddress ???
GetProcAddress proto :DWORD,:DWORD
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
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
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
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
Ditto what krumms said, you shouldn't have to set up a prototype, your code could look like this:
Be aware that what i have just suggested is heavily influenced by alcohol ;)
invoke GetProcAddress, ...blah..., ...blah...
.IF eax
call dword ptr [eax]
.ENDIF
Be aware that what i have just suggested is heavily influenced by alcohol ;)
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
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