dll中的vector.h在我的应用中不可用

vector.h in dll not useable in my application

本文关键字:应用 我的 中的 vector dll      更新时间:2023-10-16

我是新来的...我的应用程序有问题。我写了一个DLL,其中使用了一个结构和矢量。在矢量中,我希望将我的结构实例放置。到目前为止,一切都很好。

在我的应用程序中导入的dll ist。现在,我想在应用程序中使用我的DLL的向量,但这不起作用。

我没有任何错误,我想做的是以这种方式在我的main(( - 应用程序中获取信息:

#include "lib.h"
#include <iostream>
    int main()
    {
        char setData[4];
        setData[0] = 0xBE;
        setData[1] = 0x3E;
        setData[2] = 0xB8;
        setData[3] = 0x13;
        getuseablePID(setData);     //this function is in my dll and copies the information of my csv-Data to my vector
        //here i want to know the size of my vector an want to print one element but this doesn't work 
        int a = globals::getInstance()._csvContent.size();
        cout << globals::getInstance()._csvContent.PID;
        return 0;
    }

这是我的dll代码

#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define EXPORT extern "C" __declspec(dllexport)

struct csvDatei_Para {
    string PID;
    string Beschreibung;
    string Byteanzahl;
    string Min;
    string Max;
    string Einheit;
    string Wertetab;
};
struct globals {
    static globals& getInstance()
    {
        static globals instance;
        return instance;
    }
    std::vector<csvDatei_Para> _csvContent;
};

我也尝试过:

EXPORT struct globals {
    static globals& getInstance()
    {
        static globals instance;
        return instance;
    }
    std::vector<csvDatei_Para> _csvContent;
};

有人知道,为什么它不起作用并给我解决方案?

谢谢:(

请测试此代码:

#include <fstream>
#include <string>
#include <vector>
using namespace std;
#ifndef GUARD_DLLHDR_H
#define GUARD_DLLHDR_H
#if defined(BUILD_MY_DLL)
#define DLLSYM __declspec(dllexport)
#else
#define DLLSYM __declspec(dllimport)
#endif
#if defined(__cplusplus)
extern "C" {
#endif
    struct csvDatei_Para
    {
        string PID;
        string Beschreibung;
        string Byteanzahl;
        string Min;
        string Max;
        string Einheit;
        string Wertetab;
    };

    struct globals {
        static globals& getInstance()
        {
            static globals instance;
            return instance;
        }
        std::vector<csvDatei_Para> _csvContent;
    };
    DLLSYM extern globals myGlobals;
#if defined(__cplusplus)
}
#endif
#endif

现在我有了解决方案。

我写了一个函数,该函数返回了全球实例。我可以使用此实例来获取我的结构的变量。

我的.h:

EXPORT struct globals getValues();

在我的.cpp中:

struct globals getValues(){
return globals::getInstance();
}

在我的应用程序中:

getValues()._csvContent.PID;