使用函数指针调用C ++函数 [英] Calling a C++ function using function pointer

查看:82
本文介绍了使用函数指针调用C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个接收函数指针和XML字符串的函数,该字符串具有该函数的定义和每个参数的说法:



I have a function that receive a function pointer and an XML string which has the defination of the function and the argument per say:

void CallFunction( void * pFuncion, std::string xmlFuncionDef)
{

}


在上面的函数中,xmlFunctionDef 包含由pFunction指向的函数的定义.例如,数字参数,每个参数的类型以及参数:


Inside the above function, xmlFunctionDef contains the defination of the function pointed by pFunction. For example, number paramter, type of each parameter, and the arguments:

<Function ParamCount="3" ReturnType="int">
  <Params>
    <Param type="int" DefaultValue="None" PassBy="Value"/>
    <Param type="double" DefaultValue="None" PassBy="Referenc"/>
    <Param type="char*" DefaultValue="None" PassBy="Pointer"/>
  </Params>

  <Arguments>
   <Arg Value="3" />
   <Arg Value="29.5" />
   <Arg Value="Hello World" />
  </Arguments>
</Function>



现在如何调用此函数?我应该为此使用_asm吗?任何帮助都非常有帮助.

/Mizan



Now how can I call this function? Should I use _asm for this? Any help would be highly appriciative.

/Mizan

推荐答案

首先,您需要函数原型:
First you need the function prototype:
typedef std::string (*xmlFuncionDef)(void*);


然后您的函数看起来就一样:


Then your function looks alike:

void CallFunction( void * pFuncion,xmlFuncionDef fn)
{
 //...
}


然后,您可以按以下方式调用该函数:


Then you can call the function as follows:

void CallFunction( void * pFuncion,xmlFuncionDef fn)
{
  std::string result = (*fn)(pFuncion);
  // ...
}


您的函数调用:


The call of your function:

std::string MyxmlFuncionDef(void* p)
{
  std::string r;
  // get the result
  return r;
}
void main()
{
  void* p = 0;
  // initialize the data pointer ''p''
  CallFunction(p,&MyxmlFuncionDef);
}


祝你好运.


Good luck.


Mizan Rahman,
我记得我的一个朋友正在完全解决同一问题.那个时候我们使用asm代码通过vc编译器调用函数.尽管它对于vc来说很好用,但它不是可移植的.在搜索之后,我们找到了一个名为 libffi .这对我们来说效果很好.请查看该库.下载该库,查看.\ testsuite \ libffi.call 文件夹.

顺便说一句,以下是我们尝试过的asm代码.
Hi Mizan Rahman,
I remembered one of my friend is working exactly on the same problem.That time we used asm code to call functions with vc compiler.Though it is working fine for vc, it is not portable.After searching we found a library called libffi.This worked well for us.Take a look into that library.download the library,check the examples in .\testsuite\libffi.call folder.

BTW,the following is asm code we tried.
int fun(int a, int b, int c)
{
    int x;
    x = a + b + c;
    return 0;
}
int main()
{
    int x = 9;
    int y = 10;
    int z = 11;
    _asm
    {
        mov         eax,dword ptr [z]
        push        eax
        mov         eax,dword ptr [y]
        push        eax
        mov         eax,dword ptr [x]
        push        eax
        call        fun     //call fun(x,y,z);
        add         esp,12 //12 size of total arguments in byte
    }
    return 0;
}



如有任何疑问,请给予答复.

-venkatanarayana



If you have any doubts give reply.

-venkatanarayana


在ANSI C中,在函数指针和数据指针之间进行强制转换是不合法的.
函数指针带有它传达了整个函数签名,因此将函数指针作为void*传递是绝望的,编译器将无法重构适当的堆栈框架.

您最好传递一个函数索引(您的程序定义了固定数量的相关函数)并编写显式调用,并传递相应的已解析参数值.
In ANSI C, it is not legal to cast between a function pointer and a data pointer.

A function pointer conveys the whole function signature with it, so passing the function pointer as a void* is just hopeless, the compiler won''t be able to reconstruct an appropriate stack frame.

You''d better pass a function index (your program defines a fixed number of relevant functions) and write explicit calls, passing the corresponding parsed argument values.


这篇关于使用函数指针调用C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆