我认为这是一个微不足道的问题,但我永远无法让它发挥作用!
我通过 .lib 内的函数指针进行了函数调用,并将 .lib 附加到在库外部设置函数指针的应用程序。以下是代码片段
库:
void (*PopulateBuffer)(byte *NALBuffer,unsigned int *readbytes); // The Declaration
PopulateBuffer(NALBuffer,&readbytes); // The function Call
应用:
extern void (*PopulateBuffer)(char *NALBuffer,unsigned int *readbytes);
void UpdateNALBuff(char *Buffer,unsigned int *readbytes)
{
//Buffer content is updated
//readbytes is updated
}
main()
{
PopulateBuffer= &UpdateNALBuff;
LibMain(); // this is the main call in which the function pointer is called
}
我这样做对吗??? 因为当函数调用接近库时,它会抛出以下错误:
Unhandled exception at 0x00536e88 in DecoderTestApp.exe: 0xC0000005: Access violation reading location 0x7ffef045.
最佳答案
一般来说,处理函数指针时,可以按以下方式进行:
typedef void (*PopulateBuffer)(byte *NALBuffer,unsigned int *readbytes); //This should be exposed from the library
在库内创建一个指针变量,用于存储函数指针
PopulateBuffer PpBuffFunc = NULL;
从库中提供一个函数来设置功能
void setPopulateBufferFuncPointer(PopulateBuffer func)
{
PpBuffFunc = func;
}
现在从您的应用程序调用此函数
setPopulateBufferFuncPointer(UpdateNALBuff);
在库内适当的位置,通过传递适当的参数调用PpBuffFunc
关于c - 在C中初始化库外.lib的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11951201/