我正在尝试使用c++ / cli代码从C#调用非托管函数。
我有与此类似的代码:
MyFileWrapper MyFileWrapper::ReadMyFile(System::String ^fileName)
{
auto nativeMyFile=MyFile::ReadMyFile((char *)
System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(
fileName).ToPointer());
MyFileWrapper myFileWrapper=gcnew MyFileWrapper();
// code will be added to read information from native object and convert them to wrapper properties.
return myFileWrapper;
}
但我得到的错误
Error 2 error C2440: 'return' : cannot convert from 'MyFileWrapper' to 'MyFileWrapper' MyFileWrapper.cpp
Error 1 error C3673: 'MyFileWrapper' : class does not have a copy-constructor MyFileWrapper.cpp
对于第一个错误,智能提示给我以下错误,该错误具有更好的解释:
IntelliSense: no suitable user-defined conversion from "MyFileWrapper ^" to "MyFileWrapper" exists
有什么问题,我该如何解决?
最佳答案
gcnew MyFileWrapper()
返回MyFileWrapper^
(即对MyFileWrapper
实例的托管引用),而不是MyFileWrapper
。
MyFileWrapper^ MyFileWrapper::ReadMyFile(System::String^ fileName)
{
...
MyFileWrapper^ myFileWrapper = gcnew MyFileWrapper();
...
return myFileWrapper;
}
关于c# - 如何从C++/CLI静态函数返回托管类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17167728/