Generic function

This is a discussion on Generic function within the C++ Programming forums, part of the General Programming Boards category; Dear Experts, i am trying to write one Generic function, where function name i will specify it gets the function ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    Generic function

    Dear Experts,

    i am trying to write one Generic function, where function name i will specify it gets the function pointer and pass as argument to the function.
    my generic function looks like this

    Code:
    MyFunc(void *buff,numberofargs,return_typeofarg,arumentpointer,functionpointer)
    in this way i generalized,
    here void *buff= is the resulting buff which stores the resulting value,
    numberofargs=arguments count
    return_typeofarg=what exactly it has to return to the user.
    arumentpointer=pointer to arguments for a given function pointer
    functionpointer=actual function pinter
    and i am using code
    Code:
    libload(".dll);//load the .dll
    getprocaddr();//to get the function pointer address,
    my question is given a function pointer how to fetch the arguments of a function which function pointer is referring to. please help me.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is YOUR task to know what function to call and how.

    You can then do a typedef of the function itself, and cast the result of the getprocaddr() to the appropriate type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Thank you,
    for your reply,

    lets still simplify is there any method through which i can get the arguments pointer for a given function at runtime , because for my generic function i can only pass one argument which represents the argument list so that when i pass the argument pointer to my generic function so it resolves that pointer and can take the arguments from stack,

    is there any way.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you describe a little bit more about what the problem is that you are actually trying to solve. This seems like another case of 'fixing a puncture by asking how to undo a wheel-nut'.

    I'm pretty sure there is a "good way" to solve your overall problem, but I need to understand better what you are trying to ACTUALLY do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    As i described already also,

    let me still clear the point , i will be passing a function at runtime which is not fixed, so i will get its function pointer through by loading a DLL which i can pass it to a generic function, along with the function pointer i want to pass its argument list how to get its argument list when i know only function pointer or function name,
    for ex:
    i have one function
    Code:
    ex1.c
    call_me(int,float)
    {
    }
    i created a DLL here call.dll
    ex2.c
    My_Generic_Function("outputbuffe,arg_list,fptr)
    {
    //here i want to resolve that arg_list so that i can give it to the actual function
    call_me(int ,float);
    }
    
    int main()
    {
    HMODULE hwnt=libload("call.dll);//load the .dll
     fptr=getprocaddr(hwnt,"call_me");
    My_Generic_Function("outputbuffe,arg_list,fptr);
    }
    so i want to send the arg_list for a given function how i can get it that is my question.

    if this is not clear please tell me what u have understood so that we can continue from that perspective.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, maybe I'm being stupid, but I still don't understand what you actually want to do.

    Give a realm example, with argument values to the function. [Don't worry about the syntax errors, but something, given the syntax was ok, would produce some understandable results]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I do not see how you can call the function without knowing its arguments. What is the purpose of this?

    If I call puts - I call it to output a string and I should provide a string as a paramater.

    If I even do not know what argumants puts expects - why would I bother to call it?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This kind of thing is much easier to accomplish in scripting languages. E.g in Python
    Code:
    def foo(a, b):
        print "summing", a, b
        return a + b
    
    def bar():
        print "returning bar"
        return "bar"
    
    def call_function(fun, *args):
        return fun(*args)
    
    if __name__ == '__main__':
        a = call_function(foo, 2, 3)
        b = call_function(bar)
        print a
        print b
        #incorrect number of arguments
        #call_function(bar, "bar does not accept arguments")
    
       #first argument is not a callable
       #call_function(a, 42)
    summing 2 3
    returning bar
    5
    bar

    Whereas code that is commented out would produce something like:
    Traceback (most recent call last):
    File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 310, in RunScript
    exec codeObject in __main__.__dict__
    File "C:\Python25\skripts\Script1.py", line 18, in <module>
    call_function(bar, "bar does not accept arguments")
    File "C:\Python25\skripts\Script1.py", line 10, in call_function
    return fun(*args)
    TypeError: bar() takes no arguments (1 given)

    Traceback (most recent call last):
    File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 310, in RunScript
    exec codeObject in __main__.__dict__
    File "C:\Python25\skripts\Script1.py", line 18, in <module>
    call_function(a, 42)
    File "C:\Python25\skripts\Script1.py", line 10, in call_function
    return fun(*args)
    TypeError: 'int' object is not callable
    (And of course you could catch exceptions and handle them more appropriately.)

    While C is good for implementing scripting language interpreters, I don't see how it would be possible to determine without introspection whether the arguments are correct, how they should be cast etc. (Perhaps you might have more success in C++ if you have, for example, support for variadic templates...)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    ok let me try,

    ex:
    Code:
    i have one Function
    Fn_2.c
    void Fn_2_Call(float *res,int a)
    {
    //its definition goes here
    }
    
    i have created a Dll of that called test.dll
    know my function
    main.c
    
    SMCallFunc(res,1,13,a, (fptr)fnptr)
    {
    //here some code here
    //at last it calls
    fnprt(res,a);
    
    }
    
    int main()
    {
    int a;
    floar res[10];
    HMODULE hwnd=LoadLibrary("c:\\callfunc\\test.dll"); 
    fnptr=(void*)GetProcAddress(hwnd, "Fn_2_Call");
    SMCallFunc(res,1,13,a, (fptr)fnptr);//this is my generic function
    first arg=resulting function
    1=         number of arg
    13=stack memory in bytes
    a=variable
    fnptr=function pointer
      }

    which runs very fine but one strict restriction is that my generic function will take only these 5 parameters nothing more than this, so the problem comes when i try to write a function which is some thing like this,

    Code:
    void Fn_3_Call(float *res,int a,cha *name,float sal)
    if this is the senario how i should collect these parameters, because here i have fixed the
    function to fun_2_call but it can be anything at runtime fun_3_call,fun_4_call which i can load from there corresponding dlls
    which takes different arguments so how to resolve that problem with argument passing how can i pass the n number of argument into a single variable for my generic function
    b
    hope this may help me from u r side.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,009
    Are you asking for a generic function pointer that can point to any function?

    The only way to do that is to designate a specific function pointer type as generic, and cast to the correct type before dereferencing it. Exactly like void * is used for normal pointers.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Use inline assembler...

    Code:
    repeat loop until i < args.length:
      //do type definition to args[i]
      arg = args[i]
      __asm {
        //push arg according to the calling convention stdcall/...
        push ptrToarg
      }
    
    //call the function
    __asm {
      call funcPtr
    }
    This one is called "naked call". Ocassionaly used in scripting extension that support native calls to DLL.
    Last edited by audinue; 01-30-2009 at 11:45 PM.
    Just GET it OFF out my mind!!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by vin_pll View Post
    ok let me try,

    ex:
    Code:
    i have one Function
    Fn_2.c
    void Fn_2_Call(float *res,int a)
    {
    //its definition goes here
    }
    
    i have created a Dll of that called test.dll
    know my function
    main.c
    
    SMCallFunc(res,1,13,a, (fptr)fnptr)
    {
    //here some code here
    //at last it calls
    fnprt(res,a);
    
    }
    
    int main()
    {
    int a;
    floar res[10];
    HMODULE hwnd=LoadLibrary("c:\\callfunc\\test.dll"); 
    fnptr=(void*)GetProcAddress(hwnd, "Fn_2_Call");
    SMCallFunc(res,1,13,a, (fptr)fnptr);//this is my generic function
    first arg=resulting function
    1=         number of arg
    13=stack memory in bytes
    a=variable
    fnptr=function pointer
      }

    which runs very fine but one strict restriction is that my generic function will take only these 5 parameters nothing more than this, so the problem comes when i try to write a function which is some thing like this,

    Code:
    void Fn_3_Call(float *res,int a,cha *name,float sal)
    if this is the senario how i should collect these parameters, because here i have fixed the
    function to fun_2_call but it can be anything at runtime fun_3_call,fun_4_call which i can load from there corresponding dlls
    which takes different arguments so how to resolve that problem with argument passing how can i pass the n number of argument into a single variable for my generic function
    b
    hope this may help me from u r side.
    If your generic function will always take those 5 parameters (which means you have 4 parameters to pass along to the 5th function pointer, if I'm understanding you) then why not make any and every callback function take those four parameters? (They can ignore them if they don't need them, after all.)

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Thank u for all your replies,

    i just started with some direction,

    is there any way of making stack as generic using void * pointer so that it can accept anything(any data type)

    for ex:
    stack s[10];

    s.push(12);
    s.push("name");
    s.push(12.34);

    similarly the pop should happen with the single stack object is this is possible
    if you have any appropriate links please send me because in net i hav seen
    all generic implementations were in <template> for which i have to create

    stack<int>
    stack<float>
    stack<char>

    which i dont want i just want stack s;//which will any data type in c

    in general i need a heterogeneous stack implementation.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    You can use a generic pointer for pushing items onto the stack but you have to cast it to the proper type before popping them.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Now this appears to be a C++ question, so you might try Boost.Any or Boost.Variant, with something like:

    Code:
    std::stack<boost::any> s;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21