所以我在clr工作,在visual C++中创建.net dll。如何将System :: String ^转换为std :: string?
我TRU这样的代码:
static bool InitFile(System::String^ fileName, System::String^ container)
{
return enc.InitFile(std::string(fileName), std::string(container));
}
具有编码器,其normaly resives的std :: string。但在这里编译器(Visual Studio)给我C2664错误,如果我从std :: string和通常相同的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 ::互操作:: error_reporting_helper < _To_Type,_From_Type> :: marshal_as':库不支持此转换,或不包含此转换所需的头文件。请参阅“如何:扩展编组库”文档以添加自己的编组方法。
该怎么办?
你已经包含'msclr \ marshal.h'。试试'msclr \ marshal_cppstd.h'。 – 2010-08-21 23:14:58
@Chris Schmich:谢谢 - 现在它编译完美=) – Rella 2010-08-21 23:18:25