News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

invoking the function when its address is received from winapi

Started by shaikkareem, October 11, 2013, 07:40:07 PM

Previous topic - Next topic

shaikkareem

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,,,,,,,,,,,,,,,,,,,, 

dedndave

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

GoneFishing

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: 

dedndave

i thought i had posted it elsewhere, but couldn't find the thread   :P

dedndave

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


dedndave

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

ragdog

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

Vortex

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

Gunther

You have to know the facts before you can distort them.

ragdog


jj2007

The main reason for using invoke is that it checks the correct number of parameters:

include \masm32\MasmBasic\MasmBasic.inc        ; download
        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

dedndave

there are times when i prefer the other method
     mov     lpfnMultiMode,ModeAproc

although, that's not usually the case with externally loaded functions

shaikkareem

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......





dedndave

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