RE:

new topic     » topic index » view thread      » older message » newer message

> -----Original Message-----
> From: George Papadopoulos [mailto:georgp at otenet.gr]
 
> Does anybody knows how to call a dll routine from asm?
> Dll has been opened from Euphoria, so i have an address.
> I know the offset of the routine from PE header of the file.
> I think Address + Offset falls on start of the routine but the only
> thing i'm sure is that i'm full of invalid page faults.

You can do this, but you'll need to link to LoadLibrary and GetProcAddress
(in kernel32.dll) yourself, and use these functions, rather than open_dll
and define_c_func/proc.  Use LoadLibrary to get the handle of the dll, and
then GetProcAddress to get the pointer to the function:

HINSTANCE LoadLibrary(
    LPCTSTR  lpLibFileName 	// address of filename of executable module 
   );

FARPROC GetProcAddress(
    HMODULE  hModule,	// handle to DLL module  
    LPCSTR  lpProcName 	// name of function 
   );

Once you've done this, you can use the code below (from fptr.e in EuCOM) to
call a function by pointer (actually, any function, including Eu
call_backs).  Then you use call_fptr, passing the pointer to the function as
the first parameter, and the arguments in a sequence.  Note that this only
works with routines declared as stdcall.

-- start code
include machine.e
include misc.e
constant
fptr_asm = {
    #60,                    --    0: pusha
    #BB,#00,#00,#00,#00,    --    1: mov ebx, paramcount (2)
    #B9,#00,#00,#00,#00,    --    6: mov ecx, params (7)
                            --    B: start:
    #8B,#01,                --    B: mov eax, [ecx]
    #50,                    --    D: push eax
    #83,#C1,#04,            --    E: add ecx, 4
    #4B,                    --   11: dec ebx
    #75,#F7,                --   12: jnz start
    #FF,#15,#00,#00,#00,#00,--   14: call dword ptr [comfunc] (22)
    #A3,#00,#00,#00,#00,    --   1A: mov [retpointer], eax (27)
    #61,                    --   1F: popa
    #C3},                   --   20: ret


fptr_paramcount = 2,
fptr_params = 7,
fptr_funcptr = 22+0,
fptr_retptr = 27+0,
retval = allocate(4),

fptr_asm_addr = allocate( length( fptr_asm ) + 20 * 4 )

constant
fptr_func = fptr_asm_addr + 33,
fptr_retval = fptr_asm_addr + 37,
fptr_param_ptr = fptr_asm_addr + 41

poke( fptr_asm_addr, fptr_asm )
poke4( fptr_asm_addr + fptr_funcptr, fptr_func )
poke4( fptr_asm_addr + fptr_params, fptr_param_ptr )
poke4( fptr_asm_addr + fptr_retptr, fptr_retval )

global function call_fptr( atom fptr, sequence params )
    atom ret

    -- store the pointer to the function
    poke4( fptr_func, fptr )

    -- reverse the params for stdcall calling convention
    params = reverse(params)

    -- store the params
    poke4( fptr_param_ptr, params )

    -- tell the asm how many params to push
    poke4( fptr_asm_addr + fptr_paramcount, length(params) )

    -- run the asm
    call( fptr_asm_addr )

    -- get the value returned from the function
    ret = peek4u( fptr_retval )

    return ret
end procedure
-- end code

Matt Lewis

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu