Re: Calling a function by hand

From: Avatar Zondertau (avatarzondertau_at_hotmail.com)
Date: 12/08/04

  • Next message: Turbo Man: "Call system unit procedure in BASM"
    Date: 8 Dec 2004 09:22:30 -0800
    
    

    > I need to call a function at runtime but the number of arguments and
    > their types, and the pointer to the function are not known until
    > runtime.
    >
    > So, I need to call a function and pass the parameters "by hand"
    > rather than the compiler doing it at compile time.
    >
    > I was going to create a procedure that accepts the function pointer
    > and a variant open array, like the function Format('', []) does.
    > Then I was going to call the function and pass the parameters.
    >
    > I know there are different calling conventions, stdcall, cdecl,
    > etc... I assume that I will be able to call the function correctly
    > if I know its calling convention and its parameters. The variant
    > array will tell me what the parameter types are.
    >
    > The end resulting procedure I imagine is
    > procedure CallFunction(AFunction: Pointer; Args: array of const);
    >
    > I want this to be able to call functions in C, C++, and Delphi. I
    > may also want to call an object method in Delphi or C++.
    >
    > The possibility of getting return results, and var params might be
    > necessary.
    >
    >
    > What advice can you give me?

    Not to make it more complicated than it already is by using a variant
    open array.

    I would suggest using an open array of pointers instead. Put the
    parameters in the array by:

    - Adding a pointer to it (by reference parameters)

    - Typecasting to a pointer, adding the result (32-bit parameters)

    - Zero extend/sign extend (depending on type) to 32-bits, then typecast
    to pointer, add the result (8-bit and 16-bit parameters)

    - Split in multiple 32-bit blocks, typecast those to pointers, then add
    those (more than 32-bit parameters)

    This way you can just put the array on the stack and you don't have to
    care about the parameter types anymore. If you really want to use a
    variant open array-supporting function, just make a wrapper to the
    array of pointer function. This can be done in Pascal.

    I also suggest you only use stdcall, ince it's widely supported and
    probably the easiest calling convention.

    Return values are stored in AL, AX, EAX or EDX:EAX (depending on result
    size). If you don't touch EAX and EDX after calling the function you
    can simply ignore them and return an Int64. Cast this return value to
    the proper type after making the call.

    Keep in mind that return values can sometimes be passed as an
    additional by reference parameter. This is for example the case when
    64-bit+ structures are returned.

    Calling methods just means adding an extra parameter - Self - to the
    parameter list.


  • Next message: Turbo Man: "Call system unit procedure in BASM"

    Relevant Pages

    • Re: char **argv & char *argv[]
      ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
      (comp.lang.c)
    • Re: why cannot assign to function call
      ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
      (comp.lang.python)
    • Re: multi dimensional arrays as one dimension array
      ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
      (comp.lang.c)
    • Re: Evaluating unary *
      ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
      (comp.std.c)
    • Re: "Mastering C Pointers"....
      ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
      (comp.lang.c)