News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Invoking Procedures Indirectly

Started by Jibz, February 08, 2005, 08:28:26 PM

Previous topic - Next topic

Jibz

I was asked today if it was possible to use invoke to call a function that you get the address of from GetProcAddress .. after a bit of rummaging in the MASM manuals I found something that seems to work, and thought I would post a little example :U.

[attachment deleted by admin]

Vortex

Hi Jibz,

Nice work :U

Here is my method with an invoke macro simulation:

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include invoke.inc

includelib \masm32\lib\kernel32.lib

.data
kernel db "user32.dll",0
func    db "MessageBoxA",0
message db "Calling MessageBox through eax",0
caption db "Indirect Invoke Example",0

.code
start:
invoke LoadLibrary,ADDR kernel
invoke GetProcAddress,eax,ADDR func
_invoke eax,0,ADDR message,ADDR caption,MB_OK  ; macro simulating invoke
invoke ExitProcess,0

END start

[attachment deleted by admin]

hutch--

#2
Jibz,

I have not looked at your example yet but there is a prototype macro in the masm32 macro file called DDPROTO that does just that. Its useful for addresses obtained in a numer of ways, GetProcAddress, vtable addresses and procedure addresses passed to a DLL or similar for a callback. Here is a quick example of how its done using the DDPROTO macro.

LATER : Jibz, nice clear way to demonstrate how the indirect call is coded.  :U


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                        ; create 32 bit code
    .model flat, stdcall        ; 32 bit memory model
    option casemap :none        ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\macros\macros.asm

    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    NewMsgBox DDPROTO (hProc,4) ; prototype a procedure address

    .data?
      hProc dd ?
      hLib  dd ?

    .code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:

main proc

    mov hLib, rv(LoadLibrary,"user32.dll")
    mov hProc, rv(GetProcAddress,hLib,"MessageBoxA")

    invoke NewMsgBox,0,chr$("MessageBoxA API call"),chr$("Invoke call to address"),MB_OK

    exit

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

pbrennick

Hutch,
Fr some reason, that code refuses to compile on my machine.  Fist it complains about the commas in the two lines containing the RV syntax.  If I remove the commas, it then complains that RV cannot be found.

Paul

Ratch

Jibz,

Quote...I found something that seems to work, and thought I would post a little example

Or you can run naked (without the parameter counting and red tape that PROTO includes), by using an INVOKE wannabe. Ratch


            ;    invoke F4PTR PTR eax, 0, addr szMsg1, addr szCapt, MB_OK
                INVOKIT eax, 0, @ szMsg1, @ szCapt, MB_OK
00000022  6A 00                  4      PUSH MB_OK
00000024  68 00000000 R     4      PUSH OFFSET szCapt
00000029  68 00000018 R     4      PUSH OFFSET szMsg1
0000002E  6A 00                 4      PUSH 0
00000030  FF D0                 1      CALL eax

Vortex

Hi Paul,

Did you download the latest version of macros.asm?

pbrennick

#6
Vortex,
Thanks for the heads up, it is working now.  Somehow I missed Hutch's posting.
Paul

hutch--

Sorry about that Paul,

I can always claim senile decay for think that everyone had the current macros but I do have a valid excuse, the Win2k install and a 4 HDD. 2 CD/DVD machine do not sleep together well and it has wasted some days setting it all back up. I installed Office 2000 last weekend and the shell would not start any longer. There is some virtue in large multipartition machines though, apart from the boot partition that I only keep the OS and related installations on, I lost nothing at all. This in part makes up for Win2k badly messing up the partition order that I had to manually set for 18 drives.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

pbrennick

Hutch,
That's okay, it's my fault for not being observant.  BTW:  Offtopic:  I would like to somehow help you with the windows.inc project.  It is always easy to be critical of another person's work, I choose to try to help.  I believe it needs to be finalized once and for all, just as you do.

Paul