如何打开系统::字符串^成标准::字符串? [英] How to turn System::String^ into std::string?
问题描述
所以,我的工作在CLR,创建在Visual C ++ .NET的DLL。
So i work in clr, creating .net dll in visual c++.
我超铀等code:
static bool InitFile(System::String^ fileName, System::String^ container)
{
return enc.InitFile(std::string(fileName), std::string(container));
}
有恩codeR是normaly resives的std ::字符串。但这里的编译器(Visual Studio中)给了我,如果我脱光了自std :: string的和C2440这是一般相同的参数C2664错误。 VS告诉我,它只是不能到系统::字符串^转换成标准::字符串。
having encoder that normaly resives std::string. but here the compiler (visual studio) gives me C2664 error if I strip out arguments from std::string and C2440 which is in general the same. VS tells me that it just can not to convert System::String^ into std::string.
所以,我很伤心......我应该怎么做才能把系统::字符串^成标准::字符串?
So I am sad... what shall I do to turn System::String^ into std::string?
更新:
现在你的帮助,我有这样的code
Now with your help I have such code
#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
but when I try to compile it gives me C4996
错误C4996:msclr ::互操作:: error_reporting_helper&LT; _To_Type,_From_Type> :: marshal_as:这种转换是不支持的图书馆或需要这种转换头文件不包括在内。请参考上的文档如何:扩展编组库添加你自己的封法
error C4996: 'msclr::interop::error_reporting_helper<_To_Type,_From_Type>::marshal_as': This conversion is not supported by the library or the header file needed for this conversion is not included. Please refer to the documentation on 'How to: Extend the Marshaling Library' for adding your own marshaling method.
该怎么办?
推荐答案
如果您使用的是VS2008或更新,您可以使用的自动编组加入到C ++ 。例如,您可以从转换系统::字符串^
到的std ::字符串
通过的 marshal_as
:
If you're using VS2008 or newer, you can do this very simply with the automatic marshaling added to C++. For example, you can convert from System::String^
to std::string
via marshal_as
:
System::String^ clrString = "CLR string";
std::string stdString = marshal_as<std::string>(clrString);
这是用于P中的同一个编组/ Invoke的调用。
This is the same marshaling used for P/Invoke calls.
这篇关于如何打开系统::字符串^成标准::字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!