Table of Contents

Introduction

Call functions via pointers on Windows and Linux
Matt Lewis
matthewwalkerlewis@yahoo.com
http://www14.brinkster.com/matthewlewis/projects.html

Licence: This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software.


Table of Contents

Topic

Description

IndexAlphabetical list of all items.
CallbacksAllowing your functions to be called by pointer
Calling ConventionsInformation about the different calling conventions supported
Calling functionsCalling other functions by pointer

Table of Contents

Callbacks

Allowing your functions to be called by pointer


  • func call_back_cdecl( integer rid, integer args )   
  • func call_back_stdcall( integer rid, integer args )   Allowing your functions to be called by pointer
  • func call_back_thiscall( integer rid, integer params )   

    Table of Contents

    Calling Conventions

    Information about the different calling conventions supported


    There are 3 different calling conventions supported by fptr.e:

  • stdcall (standard calling convention)
  • cdecl (C language calling convention)
  • thiscall (MSVC++ default convention for C++ member functions)

    All conventions push the parameters onto the stack in reverse order:
    func( 1, 2, 3 )
      -- asm pseudocode
      -- push 3
      -- push 2
      -- push 1
      -- call func
    
    Both stdcall and thiscall require the called function to clean up the stack (move the stack pointer above the arguments--whenever you push something onto the stack the stack pointer is decreased by 4, not increased), meaning that they should be able to know (at compile time) how many arguments are being passed. It is possible (in C/C++) to have functions that take variable arguments. Since the called function can't know how many arguments it will be passed, it cannot clean the stack, so the calling function must clean the stack.

    The thiscall convention also requires that the this pointer be passed in the ecx register. This is a compiler specific convention, however, and is probably only used by MSVC++. Most compilers (and the COM standard) simply pass the this pointer as the first argument.


    Table of Contents

    Calling functions

    Calling other functions by pointer


  • func call_cdecl( atom fptr, sequence params )   Calls a function using cdecl
  • func call_stdcall( atom fptr, sequence params )   Calls a function using stdcall
  • func call_thiscall( atom fptr, atom this, sequence params )   

    Table of Contents

    [func]
    call_back_cdecl
    ( integer rid, integer args )

    Category: Callbacks

  • rid is the routine_id to the function for which you'd like to generate a call_back.
  • args is the number of arguments that the function has

    If you have a function which requires more than 9 arguments, call_back_cdecl() will set up a callback for a function taking 9 arguments. The 9th argument will actually be a pointer to the actual 9th through the last arguments.
    ex:
    

    function my_function( atom a1, atom a2, atom a3, atom a4, atom a5, atom a6, atom a7, atom a8, atom ptr ) atom a9, a10 a9 = peek4u(ptr) a10 = peek4u(ptr+4) -- do stuff... return 0 end function my_callback = call_back_cdecl( routine_id("my_function"), 10 )

    See Also: call_back_stdcall, call_back_thiscall


    Table of Contents

    [func]
    call_back_stdcall
    ( integer rid, integer args )

    Allowing your functions to be called by pointer

    Category: Callbacks

  • rid is the routine_id to the function for which you'd like to generate a call_back.
  • args is the number of arguments that the function has

    If you have a function with 9 or fewer arguments, this routine will return a normal Euphoria callback. If you have a function which requires more than 9 arguments, call_back_stdcall() will set up a callback for a function taking 9 arguments. The 9th argument will actually be a pointer to the actual 9th through the last arguments.
    ex:
    

    function my_function( atom a1, atom a2, atom a3, atom a4, atom a5, atom a6, atom a7, atom a8, atom ptr ) atom a9, a10 a9 = peek4u(ptr) a10 = peek4u(ptr+4) -- do stuff... return 0 end function my_callback = call_back_stdcall( routine_id("my_function"), 10 )

    See Also: call_back_cdecl, call_back_thiscall


    Table of Contents

    [func]
    call_back_thiscall
    ( integer rid, integer params )

    Category: Callbacks

    Returns a callback address for the routine with id rid, and params parameters, not including the this pointer. The this pointer will be the first argument passed. Up to 8 parameters can be passed by value. If there are more than 8 parameters, the ninth parameter will be a pointer to the 8th through last parameters. Suppose you had a C++ function prototype:

    int theClass::theMember( int x1, int x2, , int x3, int x4, int x5, int x6, int x7, int x8, int x9 )
    

    -- then your Euphoria function should look like: function theClass_theMember( atom this, atom x1, atom x2, atom x3, atom x4, atom x5, atom x6, atom x7, atom ptr) atom x8, x9 x8 = peek4s( ptr ) x9 = peek4s( ptr + 4 ) -- do stuff end function theClass_theMember_callback = call_back_thiscall( routine_id("theClass_theMember"), 9 )

    See Also: call_back_cdecl, call_back_stdcall


    Table of Contents

    [func]
    call_cdecl
    ( atom fptr, sequence params )

    Calls a function using cdecl

    Returns: Return value of fptr

    Category: Calling functions

    Calls a function using stdcall Use this to call a function using the stdcall calling convention when you have a pointer to the function. This is the most common calling convention in Linux. You can get the pointer to a function in a .dll or .so using define_c_var() instead of define_c_func(). You should discard the return value if you call a routine that does not return a value.

    See Also: call_stdcall, call_thiscall


    Table of Contents

    [func]
    call_stdcall
    ( atom fptr, sequence params )

    Calls a function using stdcall

    Returns: Return value of fptr

    Category: Calling functions

    Calls a function using stdcall Use this to call a function using the stdcall calling convention when you have a pointer to the function. This is the most common convention in Windows. You can get the pointer to a function in a .dll or .so using define_c_var() instead of define_c_func(). You should discard the return value if you call a routine that does not return a value.

    See Also: call_cdecl, call_thiscall


    Table of Contents

    [func]
    call_thiscall
    ( atom fptr, atom this, sequence params )

    Category: Calling functions

  • fptr: pointer to the function to call
  • this: the 'this' pointer
  • params: parameters for the function

    See Also: call_cdecl, call_stdcall


    Table of Contents

    []
    Miscellaneous Notes

    Category: Miscellaneous Notes


    Index

    Callbacks
    Calling Conventions
    Calling functions
    call_back_cdecl [func]
    call_back_stdcall [func] Allowing your functions to be called by pointer
    call_back_thiscall [func]
    call_cdecl [func] Calls a function using cdecl
    call_stdcall [func] Calls a function using stdcall
    call_thiscall [func]
    Miscellaneous Notes []