传递列表<;字符串^>;来自c++;到C# 不是C++的经验,所以这里需要一些帮助。 我得到的是.NET DLL,我正在编写包装器,以便.NET DLL可以在以后的C++和VB6项目中使用。

传递列表<;字符串^>;来自c++;到C# 不是C++的经验,所以这里需要一些帮助。 我得到的是.NET DLL,我正在编写包装器,以便.NET DLL可以在以后的C++和VB6项目中使用。,c#,c++-cli,C#,C++ Cli,到目前为止,我的代码是: 我想给c班打电话: public class App { public App(int programKey, List<string> filePaths) { //Do something } } 公共类应用程序 { 公共应用程序(int programKey,列表文件路径) { //做点什么 } } 我的C++项目: static int m_programKey; static vector<std::str

到目前为止,我的代码是:

我想给c班打电话:

public class App
{
   public App(int programKey, List<string> filePaths)
   {
       //Do something
   }
}
公共类应用程序
{
公共应用程序(int programKey,列表文件路径)
{
//做点什么
}
}

我的C++项目:

static int m_programKey;
static vector<std::string> m_fileNames;

void __stdcall TicketReportAPI::TrStart(int iProgramKey)
{
    m_programKey = iProgramKey;
};

void __stdcall TicketReportAPI::TrAddFile(const char* cFileName)
{
    string filename(cFileName);
    m_fileNames.push_back(filename);
}


void __stdcall TicketReportAPI::TrOpenDialog()
{

    if(m_fileNames.size()> 0)
    {

        List<String^> list = gcnew List<String^>();

        for(int index = 0; index < m_fileNames.size(); index++)
        {

            std::string Model(m_fileNames[index]);
            String^ sharpString = gcnew String(Model.c_str());

            list.Add(gcnew String(sharpString));
        }


        App^ app = gcnew App(m_programKey, list);

    }
    else
        App^ app = gcnew App(m_programKey);

}
static int m_程序键;
静态向量m_文件名;
void uu stdcall TicketReportAPI::TrStart(int-iProgramKey)
{
m_programKey=iProgramKey;
};
void\uu stdcall TicketReportAPI::TrAddFile(const char*cFileName)
{
字符串文件名(cFileName);
m_文件名。向后推(文件名);
}
void uu stdcall TicketReportAPI::TrOpenDialog()
{
如果(m_fileNames.size()>0)
{
List List=gcnew List();
对于(int index=0;index

如果我试图编译C++项目,我会得到以下错误:

应用程序(int,System::Collections::Generic::List^):无法从“System::Collections::Generic::List”转换为“System::Collections::Generic::List^”

是否可以将一个托管列表从C++传递到.NET C? 如果不是,你们建议我如何将字符串数组传递给我的c#汇编


非常感谢您的帮助,提前感谢。

您缺少一个
^

List<String^>^ list = gcnew List<String^>();
             ^-- right here
List^List=gcnewlist();
^--就在这里
您还需要切换
列表。将
添加到
列表->添加

您使用的是
gcnew
,这是在托管堆上创建内容的方式,结果类型是托管句柄
^
。这大致相当于使用
new
在非托管堆上创建对象,结果类型是指针
*

声明类型为
List
(不带
^
)的局部变量是有效的C++/CLI:它使局部变量使用堆栈语义。没有C#等价于该变量类型,因此大多数.Net库不能完全使用它:例如,没有
^
,就没有复制构造函数来处理变量赋值。所有托管API都希望参数的类型具有
^
,因此大多数情况下,您都希望将其用于局部变量


重要提示:此答案中的所有内容都适用于.Net中的引用类型(在C++中声明为
class
,或在C++/CLI中声明为
ref class
ref struct
)。它不适用于值类型(C++/CLI
值类
值结构
)。值类型(例如
int
float
DateTime
,等等)总是在没有
^

Grrr的情况下声明和传递,我现在要绞尽脑汁一个小时才能让它起作用。刚刚错过了一个字符=D。非常感谢。它现在编译(-:
List<String^>^ list = gcnew List<String^>();
             ^-- right here