Posts

Showing posts from March, 2005

Managed string to Unmanaged string

Taken from http://www.cs.virginia.edu/~cs216/labs/helpdocs/system.string.html

Example Code#include "stdafx.h" #include
#include
using namespace std; #using
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices; //Converts a System::String to a std::string
//This code assumes that you have used the following namespace:
// using namespace System::Runtime::InteropServices;
std::string ManagedToSTL(System::String *managed) {
//get a pointer to an array of ANSI chars
char *chars = (char*) Marshal::StringToHGlobalAnsi(managed).ToPointer(); //assign the array to an STL string
std::string stl = chars; //free the memory used by the array
//since the array is not managed, it will not be claimed by the garbage collector
Marshal::FreeHGlobal(chars); return stl;
} //Converts a std::string to a System::String
//This code assumes that you have used the following namespace:
// using n…