我是一名CLR开发者,在Visual C++中创建.NET DLL。
我尝试使用以下代码:
static bool InitFile(System::String^ fileName, System::String^ container)
{
return enc.InitFile(std::string(fileName), std::string(container));
}
我有一个编码器,通常会接收std :: string。但是在这里,如果我从std :: string中删除参数,则编译器(Visual Studio)会给出C2664错误和C2440错误,这两者实际上是相同的。VS告诉我,它无法将System :: String ^转换为std :: string。
所以我很难过... 我该怎么办才能将System :: String ^转换为std :: string?
更新:
现在,在您的帮助下,我有了这样的代码:
#include <msclr\marshal.h>
#include <stdlib.h>
#include <string.h>
using namespace msclr::interop;
namespace NSSTW
{
public ref class CFEW
{
public:
CFEW() {}
static System::String^ echo(System::String^ stringToReturn)
{
return stringToReturn;
}
static bool InitFile(System::String^ fileName, System::String^ container)
{
std::string sys_fileName = marshal_as<std::string>(fileName);;
std::string sys_container = marshal_as<std::string>(container);;
return enc.InitFile(sys_fileName, sys_container);
}
...
但是当我尝试编译时,出现了C4996错误。
错误C4996:'msclr::interop::error_reporting_helper<_To_Type,_From_Type>::marshal_as':该转换不受库支持,或者需要此转换的头文件未包括在内。请参考“如何扩展编组库”文档,以添加您自己的编组方法。
该怎么办呢?
msclr\marshal.h
,尝试使用msclr\marshal_cppstd.h
。 - Chris Schmich