c++ cli - How to convert a handle string to a std::string -


i trying convert handle string normal string. though method using working, when in debugger appears half of string has been chopped off on line creates chars variable. idea why , proper way convert handle string normal string woudl be?

std::string convert(string^ s) {     const char* chars = (const char*)(system::runtime::interopservices::marshal::         stringtohglobalansi(s)).topointer();     string mynewstring = std::string(chars);     return mynewstring; } 

it's debugger that's cutting off display of string. didn't mention how long string you're using, debugger can't display infinite length, has cut off @ point.

to verify this, try printing mynewstring console, or debugger via debug::writeline or outputdebugstring.

however, there significant issue in code: after allocating memory stringtohglobalansi, must free using freehglobal.

if want continue using stringtohglobalansi, i'd fix this:

std::string convert(string^ s) {     intptr ptr = marshal::stringtohglobalansi(s);     string mynewstring = std::string((const char*)ptr.topointer());     marshal::freehglobal(ptr);     return mynewstring; } 

however, it's easier use marshal_as methods. take care of you.

std::string output = marshal_as<std::string>(managedstring); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -