2012-08-14 25 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, 錯誤被拋出! ..這是一個愚蠢的錯誤! 感謝每一位! :)