没有经验的c ++,所以请求一些帮助.我得到的是.net dll,我正在编写一个包装器,以便.net dll可以在以后的c ++和vb6项目中使用.
我的代码到目前为止:
c#class我想打电话:
public class App
{
public App(int programKey, List<string> filePaths)
{
//Do something
}
}
Run Code Online (Sandbox Code Playgroud)
我的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);
}
Run Code Online (Sandbox Code Playgroud)
如果我正在尝试编译c ++项目,我会收到以下错误:
App(int,System :: Collections :: Generic :: List ^)':从'System :: Collections :: Generic :: List'到'System :: Collections :: Generic :: List ^'的转换不可能
是否可以将托管List从c ++传递到.net c#?如果没有,你们有什么建议我将字符串数组传递给我的c#程序集?
感谢每一位帮助,提前致谢.
Dav*_*Yaw 13
你错过了一个^.
List<String^>^ list = gcnew List<String^>();
^-- right here
Run Code Online (Sandbox Code Playgroud)
你还需要切换list.Add到list->Add.
您正在使用gcnew,这是您在托管堆上创建内容的方式,结果类型是托管句柄^.这大致相当于使用new在非托管堆上创建对象,结果类型是指针*.
声明类型的局部变量List<String^>(不带^)是有效的C++/CLI:它使局部变量使用堆栈语义.没有C#等同于该变量类型,因此大多数.Net库不能完全使用它:例如,没有复制构造函数来处理没有的变量赋值^.所有托管API都需要具有类型的参数^,因此大多数情况下,您都希望将其用于本地变量.
重要说明:本答案中的所有内容都适用于.Net中的引用类型(在C#中声明class,或在C++/CLI中声明为ref class或ref struct).它不适用于值类型(C#struct,C++/CLI value class或value struct).值类型(如int,float,DateTime,等)总是宣称与没有通过^.