gpt4 book ai didi

.net - 将 String^ 编码为字符串。使用 marshal.h 出错

转载 作者:行者123 更新时间:2023-12-02 01:05:01 25 4
gpt4 key购买 nike

我的课上有这两个

#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>

我有

using namespace msclr::interop;

我正在使用它来将 String^ 编码为字符串。这是一个例子。

string ProgReleaseType = marshal_as<std::string>(CurrentSubKey->GetValue("ReleaseType", NULL)->ToString());

当我构建时,出现此错误。

Error   53  error C4996: msclr::interop::error_reporting_helper<_To_Type,_From_Type,false>::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.    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\msclr\marshal.h  237

当我进入 marshal.h 并通读第 237 行时,有人说请使用 marshal_context。我以为我是。我不明白它不喜欢什么。

最佳答案

我无法按照描述重现此错误。这是我在包含 msclr\marshal.h 但不包含 msclr\marshal_cppstd.h 时得到的错误。仔细检查您是否包含它,或者可能将其明确包含在您的 cpp 文件中的第一行。

这是我的测试应用程序:

#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

using namespace msclr::interop;
using namespace System;
using namespace System::Diagnostics;

int main(array<System::String ^> ^args)
{
String^ str = "Something";

std::string stdstr = marshal_as<std::string>(str);

stdstr[4] = '-';

// Convert back to managed for printing.
Debug::WriteLine(marshal_as<String^>(stdstr));

return 0;
}

输出:

Some-hing

关于.net - 将 String^ 编码为字符串。使用 marshal.h 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com