c中与硬件的交互

时间:2012-06-10 18:14:38

标签: c winapi function pointers

通过保存函数地址的指针调用函数会产生错误 代码:

p=GetProcAddress(h,"installhook");//p is a pointer that holds the address returned from getprocaddress() function
(*p)(); //using this pointer making a call to installhook function

但代码在我(*p)();拨打电话时产生错误,它表示术语不会评估为函数。 如何克服这个问题?有没有其他方法使用指针调用函数?

2 个答案:

答案 0 :(得分:2)

您需要将GetProcAddress的返回值强制转换为正确的函数类型。例如:

typedef void (*FuncPtr)();    //assuming a function like void f()
FuncPtr p;

p = (FuncPtr) GetProcAddress(h, "funcName");
if (p)
    p();
else
    printf("Function not found\n");

答案 1 :(得分:2)

验证p的声明如下:

void (*p)(void);

并且GetProcAddress的返回值类型相同。