The MASM Forum

General => The Campus => Topic started by: shaikkareem on October 11, 2013, 07:40:07 PM

Title: invoking the function when its address is received from winapi
Post by: shaikkareem on October 11, 2013, 07:40:07 PM
hi there.............
i'm really confused at the point where,
when i'm calling the win32 api functions LoadLibrary and GetProcAddress they really succeeded and returning the handle well....
when comes to the second function it is returning the address pointer to desired function of desired loaded library now what i've to do to take an advantage of desired function..........that is call\invoking it........can somebody explain me what to do and its mechanism,,,,,,,,,,,,,,,,,,,, 
Title: Re: invoking the function when its address is received from winapi
Post by: dedndave on October 11, 2013, 08:00:24 PM
i use a method that qWord showed us
attached is an example that uses SetConsoleIcon

IDI_MYICON  EQU 100

FuncD       TYPEDEF PROTO :DWORD
PFUNCD      TYPEDEF Ptr FuncD
;
        .DATA
        ALIGN   4

szKernel32       db 'kernel32.dll',0
szSetConsoleIcon db 'SetConsoleIcon',0
;
        .DATA?
        ALIGN   4

hInstance          HINSTANCE ?
lpfnSetConsoleIcon PFUNCD ?
;
        .CODE

    INVOKE  GetModuleHandle,NULL
    mov     hInstance,eax
    INVOKE  GetModuleHandle,offset szKernel32
    INVOKE  GetProcAddress,eax,offset szSetConsoleIcon
    mov     lpfnSetConsoleIcon,eax
;
    INVOKE  LoadIcon,hInstance,IDI_MYICON
    INVOKE  lpfnSetConsoleIcon,eax


notice that Kernel32 is always loaded, so no need for LoadLibrary and FreeLibrary
we can just use GetModuleHandle, instead
Title: Re: invoking the function when its address is received from winapi
Post by: GoneFishing on October 11, 2013, 09:08:16 PM
Thank you , Dave
I've just got an answer for my question "How do I set a custom icon in the console window?"  :t
You're extremely fast in your  replies  - you give an  answer to the question that I have in my mind  :biggrin: 
Title: Re: invoking the function when its address is received from winapi
Post by: dedndave on October 12, 2013, 01:14:03 AM
i thought i had posted it elsewhere, but couldn't find the thread   :P
Title: Re: invoking the function when its address is received from winapi
Post by: dedndave on October 12, 2013, 02:40:03 AM
TYPEDEF defines a new type
a typical use of the TYPEDEF directive might be to define something like HBITMAP
HBITMAP TYPEDEF DWORD
tells the assembler that the HBITMAP type has DWORD size

in this case, we are using it to describe a function PROTO (prototype)
FuncD       TYPEDEF PROTO :DWORD
FuncD is now a new type, that has properties of a function that has 1 DWORD parameter

we use TYPEDEF again to describe a specific Ptr type
PFUNCD      TYPEDEF Ptr FuncD
the assembler now knows that PFUNCD is a pointer to a function of type FuncD

now, when we define the variable
lpfnSetConsoleIcon PFUNCD ?
the assembler knows that lpfnSetConsoleIcon is of PFUNCD type
Title: Re: invoking the function when its address is received from winapi
Post by: shaikkareem on October 12, 2013, 03:16:02 AM
What about ptr
Title: Re: invoking the function when its address is received from winapi
Post by: dedndave on October 12, 2013, 03:17:21 AM
Ptr = pointer ~ "address of"

we use TYPEDEF again to describe a specific Ptr type
PFUNCD      TYPEDEF Ptr FuncD
the assembler now knows that PFUNCD is a pointer to a function of type FuncD

meaning that data items defined with the PFUNCD type are addresses of functions of type FuncD
Title: Re: invoking the function when its address is received from winapi
Post by: ragdog on October 12, 2013, 03:53:58 AM
I think a better solution is a Call or Invoke Macro

It give many macros for it  is an example what i often used

INVOKE  GetProcAddress,eax,offset szMessageBoxA
mov     pMessageBox,eax
pCall pMessageBox,0,addr szText ,0,MB_OK
Title: Re: invoking the function when its address is received from winapi
Post by: Vortex on October 12, 2013, 05:50:25 AM
I agree with ragdog. A macro does the job. Here is a quick example :


include     MacroCall.inc

.data

user32      db 'user32.dll',0
MessageBox  db 'MessageBoxA',0
msg         db 'Macro calling MessageBox',0
capt        db 'Test',0

.data?

hDLL        dd ?

.code

start:

    invoke  LoadLibrary,ADDR user32
    mov     hDLL,eax

    invoke  GetProcAddress,eax,ADDR MessageBox

   _invoke  eax,0,ADDR msg,ADDR capt,MB_OK

    invoke  FreeLibrary,hDLL
    invoke  ExitProcess,0

END start
Title: Re: invoking the function when its address is received from winapi
Post by: Gunther on October 12, 2013, 06:22:57 AM
Good catch, Erol.  :t

Gunther
Title: Re: invoking the function when its address is received from winapi
Post by: ragdog on October 12, 2013, 07:27:28 AM
http://www.asmcommunity.net/forums/topic/?id=6187
Title: Re: invoking the function when its address is received from winapi
Post by: jj2007 on October 12, 2013, 08:49:22 AM
The main reason for using invoke is that it checks the correct number of parameters:

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
        Init
        push rv(LoadLibrary, "User32.dll")
        mov CreateInvoke(zebox, 4*dword), rv(GetProcAddress, eax, "MessageBoxA")

        ; try using a wrong number of parameters...
        invoke zebox, 0, Chr$("Text"), Chr$("Title"), MB_OK

        call FreeLibrary
        Exit
end start
Title: Re: invoking the function when its address is received from winapi
Post by: dedndave on October 12, 2013, 09:18:07 AM
there are times when i prefer the other method
     mov     lpfnMultiMode,ModeAproc

although, that's not usually the case with externally loaded functions
Title: Re: invoking the function when its address is received from winapi
Post by: shaikkareem on October 12, 2013, 02:15:41 PM
Got another here it is......
In the masm32 lib there are not much libs i mean in msdn in some reference they denoting libraries which r not in the masm32 lib. What i have to do to use those libs functions that r not in masm32 libs. The previous answer is ans to this one also r is there is any other mech for that......




Title: Re: invoking the function when its address is received from winapi
Post by: dedndave on October 12, 2013, 03:13:50 PM
one line will get you most of what you need, including kernel32
it also takes care of model, casemap, etc
look inside the file to see what it does

    include    \masm32\include\masm32rt.inc

occasionally, you may have to add others - advapi32, for example
if so, add these lines after that one
    include    \masm32\include\advapi32.inc
    includelib \masm32\lib\advapi32.lib

there are numerous other pairs that you may need, depending on the functions you want to use
Title: Re: invoking the function when its address is received from winapi
Post by: Vortex on October 13, 2013, 05:56:13 AM
Here is another version.
Title: Re: invoking the function when its address is received from winapi
Post by: Vortex on October 14, 2013, 01:38:57 AM
Simple parameter checking. The macro will report now a supply of incorrect number of parameters.

include     MacroCall.inc

MessageBox  equ <pm4>

.data

user32      db 'user32.dll',0
MessageBoxA db 'MessageBoxA',0
msg         db 'Macro calling MessageBox',0
msg2        db 'Another MessageBox test',0
capt        db 'Test',0

.data?

hDLL        dd ?
pFunc       dd ?

.code

start:

    invoke  LoadLibrary,ADDR user32
    mov     hDLL,eax

    invoke  GetProcAddress,eax,ADDR MessageBoxA
    mov     pFunc,eax

;   The suffix pm is activating the number of parameters check

;   pmX : X is the number of parameters.

   _invoke  pm4 eax,0,ADDR msg,ADDR capt,MB_OK

   _invoke  %MessageBox pFunc,0,ADDR msg2,ADDR capt,MB_OK

    invoke  FreeLibrary,hDLL
    invoke  ExitProcess,0

END start