使用C ++ / CLI包装非托管C ++代码:传递资源

时间:2016-01-10 22:33:15

标签: c# c++ vector resources

关于在C#项目中使用C ++非托管代码的问题。我从以前的应用程序中获得了非常多(源代码)的C ++代码,无需UI。现在我想给我的代码提供一个UI,并且我使用C#作为UI和C ++ DLL作为"计算库"。现在,我使用一些本机C ++类的包装类来在C ++ / CLI中创建托管代码。我遇到的困难是我的代码使用了很多(静态)数据作为输入。这些数据特别是从我的工程工作中得出的表格和坐标。我希望这些数据无法从外部访问,并且在本机C ++应用程序时我不想使用外部.txt文件从中读取数据。所以我在C ++代码中创建了一个资源管理器类,我将这些数据作为字符串存储,然后我创建了一些std :: vector< >存储我的列ecc并将数据传递给我的对象。 随之而来的是我想要的所有"计算类"和#34;资源经理"为了保持一切,如果可能在C ++代码中找到我不想改变我以前的工作的问题,我的问题/问题是:

1。)存在一种更好的方法来嵌入这些数据并创建这个" Resouce Manager"内部代码或我的方式是好的?,也预见到我必须在C#应用程序中使用它,我希望它在C ++代码中维护它。

2.。)如果我的方法是正确的,当我尝试使用C ++ / CLI创建托管代码时,不要编译,因为我使用了std :: vector<取代。所以我不知道如何用C ++ / CLI编译这些数据。

3.)我遇到的另一个问题是我的一些课程经常使用std:vector< >所以我不知道如何处理它。

提前谢谢你。 对不起我的英文。如果需要进一步说明,请询问。

非常感谢Walter对你的重播,对我很久的回答感到抱歉。 我做了一个例子。

这是我的C ++标题(vector.h)

#include <iostream>
#include "vector"
using namespace std;

class Vector2CLI
{
public:
    Vector2CLI();
public:
    static const string m_stringVector2CLI;
    void SplitString(const string& resourcedata, vector<double>& vectorresult);
    vector<double> m_vectorVector2CLI;
};

这是我的C ++源代码(vector.cpp)

#include <iostream> 
#include <fstream> 
#include <sstream>

#include "vector.h"


Vector2CLI::Vector2CLI()
{

    SplitString(m_stringVector2CLI, m_vectorVector2CLI);
    double ciao = 1;

}


void
Vector2CLI::SplitString(const string& resourcedata, vector<double>& vectorresult)
{
    istringstream iss(resourcedata);
    //vector<double> temp_vector;

    while (iss)
    {
        string item;
        iss >> item;
        vectorresult.push_back(atof(item.c_str()));
    }

}

const string
Vector2CLI::m_stringVector2CLI =
"0 "
"1 "
"2 "
"3 "
"4 "
"5 "
"6 "
"7 "
"8 "
"9";

现在我制作了一个C ++ CLI代码,在我的标题中我已经包含了我的C ++项目的标题:

#include "c:\users\...\vector.h"
#include "c:\users\...\vector.cpp"

using namespace System;

namespace VCcCLI_DLL {

    public ref class Class1
    {
        // TODO: Add your methods for this class here.
    };
}

它给了我这个错误:

- Error 1   error C2011: 'Vector2CLI' : 'class' type redefinition   c:\users\...\vector.h   7   1   VCc++CLI_DLL

- Error 2   error C2027: use of undefined type 'Vector2CLI' c:\users\...\vector.cpp 10  1   VCc++CLI_DLL

- Error 3   error C2059: syntax error : ')' c:\users\...\vector.cpp 10  1   VCc++CLI_DLL
- Error 4   error C2143: syntax error : missing ';' before '{'  c:\users\...\vector.cpp 11  1   VCc++CLI_DLL

- Error 5   error C2447: '{' : missing function header (old-style formal list?) c:\users\...\vector.cpp 11  1   VCc++CLI_DLL

- Error 6   error C2027: use of undefined type 'Vector2CLI' c:\users\...\vector.cpp 20  1   VCc++CLI_DLL

- Error 7   error C2027: use of undefined type 'Vector2CLI' c:\users\...\vector.cpp 35  1   VCc++CLI_DLL

1 个答案:

答案 0 :(得分:0)

1。)按照你描述的方式做它应该没问题。另一种可能的方法是使用字符串文字将数据嵌入C ++代码中。

static const char* data = "0,1;42,42;";

您也可以这种方式存储二进制数据。

static const unsigned char* binaryData = "\x01\x02\x03\x04";

如果您的数据非常大,您还可以在将其转换为字符串文字之前对其进行压缩,然后在需要时将其解压缩到内存中。

当我们谈论Windows时,您当然也可以将您的数据嵌入到资源中。

2.。)在我的公司,我们使用C ++ / CLI创建来自C#的桥梁 - &gt; C ++和C ++&gt; C#。在某些情况下我们也使用std :: vector,并且没有任何问题。你能提供一个无法编译的代码示例吗?

相关问题