类型的值不能用于初始化类型的实体

时间:2018-02-12 17:17:55

标签: c++ c stdcall

我有一个功能:

long __stdcall call_DLL(long n, byte s0, byte s1, long(__stdcall *CallBack)(long m, byte s0, byte s1)){
//trying to copy the address of CallBack to another pointer
long *x = &CallBack;
}

我收到错误:

a value of type "long(__stdcall *CallBack)(long m, byte s0, byte 
s1)"cannot be used to initialize an entity of type "long *"

任何人都知道我该怎么做?

2 个答案:

答案 0 :(得分:1)

如果确实想要保存回调以便以后使用它,你可以这样做:

long (* __stdcall x)(long, byte, byte) = CallBack;

或在C ++中,您也可以使用auto来简化:

auto x = CallBack;

在任何一种情况下,请稍后再使用

long ret = x(n, s0, s1);

否则,如果您只想调用CallBack,请执行类似

的操作
long x = CallBack(n, s0, s1);

答案 1 :(得分:0)

不是使用不兼容的long *初始化函数指针,而是使用兼容类型对其进行初始化。 this answer for C language

@Algirdas Preidžius

long __stdcall call_DLL(long n, byte s0, byte s1,
    long (__stdcall *CallBack)(long m, byte s0, byte s1)) {

  // long *x = &CallBack;
  long (__stdcall *x)(long, byte, byte) = CallBack;

  // sample usage of `x`
  return x(n, s0, s1);
}