c++ - 读取位置 0xc3618000 访问冲突

标签 c++ dll crash access-violation

我正在开发一个有关红外跟踪的项目,并且正在使用 FreeTrack (www.free-track.net) 软件。 FreeTrack 提供了一个 sdk 文件夹,其中包含 C、Matlab 和 delphi 代码,可用于将 FreeTrack 中的数据连接到您自己使用其中一种语言编写的程序。

我从 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;
}

最佳答案

您告诉 printf yaw 是一个 C 样式字符串,但它是一个 float !

关于c++ - 读取位置 0xc3618000 访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9291932/

相关文章:

c++ - 执行 int 3 中断会停止 Linux 上的整个进程还是仅停止当前线程?

c++ - C++ 库中的模板类定义

c++ - C++ 上限函数的奇怪结果

c++ - VBA 无法找到我的 DLL,尽管对位置进行了硬编码。

iphone - 使用发布版本有效测试 iPhone 应用程序

c++ - 在文件 C++ 中分组数据

c# - 如何在 C# 中调用 Visual Basic 6.0 方法?

c++ - 是否可以编写 C++ .dll 库来导出在命名空间和类中编写的方法?

ios - _GSRegisterPurpleNamedPortInPrivateNamespace - 应用商店崩溃(附代码)

python - PySide 在线程之间发出 None 时使 Python 崩溃