Use a marshal context:
msclr::interop::marshal_context context;
string ProgReleaseType = context.marshal_as<std::string>(CurrentSubKey->GetValue("ReleaseType", NULL)->ToString());
Вопрос
I have both of these in my class
#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>
and I have
using namespace msclr::interop;
I am using it to marshal String^ to string. Here is one example.
string ProgReleaseType = marshal_as<std::string>(CurrentSubKey->GetValue("ReleaseType", NULL)->ToString());
When I build, I get this error.
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
When I go into the marshal.h and read through line 237 is says to please use a marshal_context. I thought I was. I don't get what it doesn't like.
Решение 2
Use a marshal context:
msclr::interop::marshal_context context;
string ProgReleaseType = context.marshal_as<std::string>(CurrentSubKey->GetValue("ReleaseType", NULL)->ToString());
Другие советы
I'm unable to reproduce this error as described. That is the error I get if I include msclr\marshal.h
but not msclr\marshal_cppstd.h
. Double-check you're including it, or perhaps include it explicitly as the very first line in your cpp file.
Here's my test application:
#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;
}
Output:
Some-hing
I hope this answer will help you.
Add header
#include <msclr\marshal_cppstd.h>
Here is sample code for String^ to string (standard string)
String^ str ="Sample string";
msclr::interop::marshal_as<std::string>(str)