所以我在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