c - 在C中初始化库外.lib的函数指针

标签 c function pointers

我认为这是一个微不足道的问题,但我永远无法让它发挥作用!

我通过 .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/

相关文章:

c - eclipse中的静态库链接

c - SIGABRT 和损坏的双链表

javascript - JavaScript 中的 str.fun()/str.fun/fun(str) 有什么区别?

C++ - 指针问题

在某些情况下,您可以使用受限指针来访问同一对象吗?

c - 指向结构数组的指针

c++ - 是否有一个 gcc 选项来假设所有 extern "C"函数不能传播异常?

c++ - 英特尔 IPP 卷积已弃用——是否有不同的 IPP 2D 卷积方法?

c++ - 有符号/无符号比较给出意想不到的结果

python - python中通过函数列表列表