Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C+;返回字符串+/CLI方法返回到非托管C++;这就叫它 我试图找出如何将一个字符串值从C++ /CLI方法返回到调用它的非托管C++。在我的当前实现中,我有一个字符串存储在一个本地字符串String变量中(托管)C++/CLI方法中,我希望该方法返回到调用它的非托管C++程序。如果使用String^变量不是一个好选择,那么什么构造/类型w/更好?注意,我省略了一部分,其中C#方法将字符串值返回给C++/CLI方法,因为这不是问题_C#_C++_Unmanaged_Managed_Bridge - Fatal编程技术网

如何从C+;返回字符串+/CLI方法返回到非托管C++;这就叫它 我试图找出如何将一个字符串值从C++ /CLI方法返回到调用它的非托管C++。在我的当前实现中,我有一个字符串存储在一个本地字符串String变量中(托管)C++/CLI方法中,我希望该方法返回到调用它的非托管C++程序。如果使用String^变量不是一个好选择,那么什么构造/类型w/更好?注意,我省略了一部分,其中C#方法将字符串值返回给C++/CLI方法,因为这不是问题

如何从C+;返回字符串+/CLI方法返回到非托管C++;这就叫它 我试图找出如何将一个字符串值从C++ /CLI方法返回到调用它的非托管C++。在我的当前实现中,我有一个字符串存储在一个本地字符串String变量中(托管)C++/CLI方法中,我希望该方法返回到调用它的非托管C++程序。如果使用String^变量不是一个好选择,那么什么构造/类型w/更好?注意,我省略了一部分,其中C#方法将字符串值返回给C++/CLI方法,因为这不是问题,c#,c++,unmanaged,managed,bridge,C#,C++,Unmanaged,Managed,Bridge,我正在使用VS2017 代码示例-为简单起见,代码已减少 非托管C++ -----------------/P> _declspec(dllexport) void GetMyString(); int main() { GetMyString(); } __declspec(dllexport) String GetMyString() { String ^ sValue = "Return this string"; return (sValue); } (托管

我正在使用VS2017

代码示例-为简单起见,代码已减少

非托管C++ -----------------/P>

_declspec(dllexport) void GetMyString();

int main()
{
    GetMyString();
}
__declspec(dllexport) String GetMyString()
{
    String ^ sValue = "Return this string";
    return (sValue);
}
(托管)C++/CLI-------------------------

_declspec(dllexport) void GetMyString();

int main()
{
    GetMyString();
}
__declspec(dllexport) String GetMyString()
{
    String ^ sValue = "Return this string";
    return (sValue);
}

非常感谢您的帮助。提前感谢。

< p>不能返回<代码>字符串^ < /COD>到C++,因为它将无法识别它。不过,使用InteropServices进行了一些转换。从


<>我最终将一个系统::字符串^转换成托管C++方法中的STD::string,将后者返回给非托管C++调用方。< /P>

托管C++文件摘要:

#include <msclr\marshal_cppstd.h>

__declspec(dllexport) std::string MyManagedCppFn()
{
    System::String^ managed = "test";
    std::string unmanaged2 = msclr::interop::marshal_as<std::string>(managed);
    return unmanaged2;
}
_declspec(dllexport) std::string MyMangedCppFn();

std::string jjj = MyMangedCppFn();    // call Managed C++ fn


信用证从一个Soad溢出到一个StasOp溢出问题,询问如何将系统转换为:String ^到STD::String。< /P>我认为在非托管C++中不能使用<代码>字符串^ < /C>。使用
std::string
std::wstring
。可能您必须在c++/cli中将
String^
转换为std字符串。