2012-02-15 134 views
0

我正在研究一個關於紅外跟蹤的項目,我正在使用FreeTrack(www.free-track.net)軟件。 FreeTrack提供了一個sdk文件夾,下載C,Matlab和delphi代碼,可用它們將FreeTrack的數據與其中一種語言的書寫程序連接起來。訪問衝突讀取位置0xc3618000

我開始使用Matlab,但是當matlab出現問題時,我繼續使用Mvisual中的C++。這也給出了一個問題,這在Matlab中看起來是一樣的。

這裏是東西: 當我想從freetrack客戶端的DLL讀取數據,我用:

//declare imported function pointers 
importGetData getData; 
getData = (importGetData)GetProcAddress(hinstLib, "FTGetData"); 

if (getData(pData)) 
printf("Yaw: %f\n", data.yaw); 

第一次它的工作原理,但數據是0第二次它它,Mvisual給出錯誤:

Unhandled exception at 0xc3618000 in FreeTrack.exe: 0xC0000005: Access violation 
reading location 0xc3618000. 

getData的地址爲0xc3618000。

這隻發生在FreeTrack運行時。當它不是時,沒有數據從getData返回。

任何想法可能是什麼?

原始代碼:

#include <windows.h> 
#include <stdio.h> 
#include <conio.h> 

typedef struct 
{ 
    float yaw; 
    float pitch; 
    float roll; 
    float x; 
    float y; 
    float z; 
    int dataID; 
}FreeTrackData; 

// DLL function signatures 
// These match those given in FTTypes.pas 
// WINAPI is macro for __stdcall defined somewhere in the depths of windows.h 
typedef bool (WINAPI *importGetData)(FreeTrackData * data); 
typedef char *(WINAPI *importGetDllVersion)(void); 
typedef void (WINAPI *importReportID)(int name); 
typedef char *(WINAPI *importProvider)(void); 


int main(int argc, char **argv) 
{ 
     /*while(1){ 
     printf("hello"); 
     }*/ 
     //declare imported function pointers 
     importGetData getData; 
     importGetDllVersion getDllVersion; 
     importReportID reportID; 
     importProvider provider; 

     // create variables for exchanging data with the dll 
     FreeTrackData data; 
     FreeTrackData *pData; 
     pData = &data; 
     char *pDllVersion; 
     int name = 453; 
     char *pProvider; 

     //while(1){}; 
     // Load DLL file 
     HINSTANCE hinstLib = LoadLibrary("FreeTrackClient.dll"); 
     if (hinstLib == NULL) { 
       printf("ERROR: unable to load DLL\n"); 
       //return 1; 
       while(1){}; 
     } 
     else 
     { 
      printf("dll loaded\n"); 
     } 
     //while(1){}; 
     // Get function pointers 
     getData = (importGetData)GetProcAddress(hinstLib, "FTGetData"); 
     getDllVersion = (importGetDllVersion)GetProcAddress(hinstLib, "FTGetDllVersion"); 
     //reportID = (importReportID)GetProcAddress(hinstLib, "FTReportID"); 
     reportID = (importReportID)GetProcAddress(hinstLib, "FTReportName"); 
     provider = (importProvider)GetProcAddress(hinstLib, "FTProvider"); 

     // Check they are valid 
     if (getData == NULL) { 
       printf("ERROR: unable to find 'FTGetData' function\n"); 
       FreeLibrary(hinstLib); 
       //return 1; 
     } 
     if (getDllVersion == NULL){ 
       printf("ERROR: unable to find 'FTGetDllVersion' function\n"); 
       FreeLibrary(hinstLib); 
       //return 1; 
     } 
     if (reportID == NULL){ 
       printf("ERROR: unable to find 'FTReportID' function\n"); 
       FreeLibrary(hinstLib); 
       //return 1; 
     } 
     if (reportID == NULL){ 
       printf("ERROR: unable to find 'FTProvider' function\n"); 
       FreeLibrary(hinstLib); 
       //return 1; 
     } 

     // Print the address of each function 
     printf("FTGetData is at address: 0x%x\n",getData); 
     printf("FTGetDllVersion is at address: 0x%x\n",getDllVersion); 
     printf("FTReportID is at address: 0x%x\n",reportID); 
     printf("FTProvider is at address: 0x%x\n",provider); 

     // Call each function and display result 
     pDllVersion = getDllVersion(); 
     printf("Dll Version: %s\n", pDllVersion); 

     pProvider = provider(); 
     printf("Provider: %s\n", pProvider); 

     reportID(name); //not sure what this does - I guess it tells the dll that I am using it. 

     system("pause"); //wait till keyboard is pressed before entering main loop 
     while(kbhit() != 1) 
     { 
      //system("cls"); //clear screen 
      if (getData(pData)) 
      { 
       printf("Provider: %s\n", pProvider); 
       printf("Record ID: %d\n" , data.dataID); 
       printf("Yaw: %5.2f\n" , data.yaw); 
       printf("Pitch: %5.2f\n" , data.pitch); 
       printf("Roll: %5.2f\n" , data.roll); 
       printf("X: %5.2f\n" , data.x); 
       printf("Y: %5.2f\n" , data.y); 
       printf("Z: %5.2f\n" , data.z); 
      } 
      else 
      { 
       printf("Nothing returned from getData\n"); 
       break; 
      } 
     } 

     // Unload DLL file 
     FreeLibrary(hinstLib); 
     return 0; 
} 
+0

什麼是'pData'? – valdo 2012-02-15 10:50:09

+0

typedef struct {float yaw;浮動間距;浮動輥; float x;浮動y; float z; int dataID; } FreeTrackData; FreeTrackData數據; FreeTrackData * pData; – Ezraneut 2012-02-15 10:55:25

+0

我不認爲我與打印有關,因爲當我在「if 9getData(pdata))」處插入一個斷點時,並且我進入/結束它,那麼問題就會發生 – Ezraneut 2012-02-15 10:57:37

回答

0

你告訴printfyaw是C風格的字符串,但它是一個浮動!

+0

是的,這是一個錯誤,它應該是%f – Ezraneut 2012-02-15 10:58:15

+0

然後可能是錯誤的是,你忘了初始化'pData'類似'pData = &data;''。所以你傳遞了一個未初始化變量的值getData。 – 2012-02-15 10:59:06

+0

我剛剛創建數據並* pdata – Ezraneut 2012-02-15 11:02:36

0

我認爲這可能是由FreeTrackData的結構定義引起的。 我發現從this post更新一個,並希望這會有所幫助。