2012-08-14 64 views
0

我认为这是一个微不足道的问题,但我永远无法得到它的工作!初始化函数C库外的.lib指针的外部指针

我已经通过.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 
} 

我这样做吗???当过函数调用库中的方法,它抛出我下面的错误 原因:DecoderTestApp.exe在0x00536e88

未处理的异常:0000005:访问冲突读取位置0x7ffef045。

+0

该库是静态的,而不是.dll – 2012-08-14 11:26:32

+0

它是否无法使用指针或在函数内调用函数? – Rohan 2012-08-14 11:28:27

+0

它在函数调用时失败,而不是函数内部我已经在函数内部放置了一个断点,并且它没有达到那个点,我在函数调用 – 2012-08-14 11:32:46

回答

0

一般为处理函数指针,你这样做以下列方式:

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); 

的lib里面在适当的地方调用这个函数,通过传递适当的参数

+0

我得到和以前一样的错误... – 2012-08-14 12:14:45

+0

嘿! .. 找到了 ! ..我在另一个文件中调用函数,并没有在该文件中给出相同的原型,因此它将返回值作为“int”而不是“void”! :) ..谢谢你,你的方法非常有帮助! :) – 2012-08-14 13:16:28

0

这似乎没问题,但我怀疑你读入的缓冲区太小了 这就是为什么你要访问冲突。

void UpdateNALBuff(char *Buffer,unsigned int *readbytes) 
{ 
    //put values in Buffer but no more than *readbytes passed in, 
    //finally update *readbytes with actual number of bytes read. 
} 
+0

没有变化..我仍然得到相同的错误 – 2012-08-14 11:25:23

+0

@nos是啊,但它是好的编码风格? – 2012-08-14 12:45:46

0

致电PpBuffFunc得到它! :)

的问题是,我打电话在另一个文件中的函数指针不为同一原型提供,

因此,它正在采取“返回类型”为INT,并作为函数是回报键入void, 错误被抛出! ..这是一个愚蠢的错误! 感谢每一位! :)