简体   繁体   English

通过通用指针调用 C function

[英]Calling C function by generic pointer

Is there any method to call a function, by just knowing its address in a pointer, lets say a pointer of type "void *(*)(void *)" , and number and type of its parameter?是否有任何方法可以调用 function,只需知道它在指针中的地址,比如说一个类型为"void *(*)(void *)"的指针,以及它的参数的数量和类型?

The function could have any number of parameters! function 可以有任意数量的参数!

No, not in any portable or standard way.不,不是以任何便携式或标准方式。

However, there is a standard way to pass a variable number of arguments between functions.但是,有一种标准方法可以在函数之间传递可变数量的 arguments。 Have a look at vsprintf .看看vsprintf Basically, you need a version of the function that accepts a va_list .基本上,您需要一个接受va_list的 function 版本。

Roughly, no.粗略地说,没有。

You have to know the number of parameters before you can write the call, and therefore the types too.在编写调用之前,您必须知道参数的数量,因此也知道类型。 Therefore, you cannot write one line of code that accurately calls a function that takes 0 parameters or 1 parameter or 2 parameters.因此,你不能写一行代码准确调用一个带0个参数或1个参数或2个参数的function。 You could always provide 2 and trust things will work - but the compiler will (justifiably) give warnings.你总是可以提供 2 并且相信事情会起作用 - 但编译器会(有理由地)发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM