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 namespace System::Runtime::InteropServices
System::String* STLToManaged(std::string stl) {
//the c_str() function gets a char array from the STL string,
//but the PtrToStringAnsi function wants a int array, so it gets casted
return Marshal::PtrToStringAnsi((int*) stl.c_str());
}

int _tmain()
{
/*Parsing and Comparison Example*/
//First, here is an example of simple string parsing
Console::WriteLine("Parsing and Comparison Example:");

//here is a string to parse
String *str = S"one two,three:four;five";

//this is a list of delimiters that can separate words
Char delim[] = {' ', ',', ':', ';'};

//using the Split() method, we can obtain an array of strings that
//are separated by the given delimeters
String *strArray[] = str->Split(delim);

//check that the first element contains "one"
//if you wish to compare alphabetically, use Compare() (see MSDN)
if (strArray[0]->Equals(S"one"))
Console::WriteLine("\"one\" == \"one\"");

//output all members of the array
//we should see:
// one
// two
// three
// four
// five
for (int i = 0; i <>get_Count(); i++)
Console::WriteLine(strArray[i]);

/*StringBuilder Example*/
//Here is an example of using StringBuilder as a changeable alternative
//to the immutable String class
//Here we assume:
// using namespace System::Text;
Console::WriteLine();
Console::WriteLine("StringBuilder Example:");

//declare a new StringBuilder
StringBuilder *strBuild = new StringBuilder();

//use the Append() method to construct a string of the words that we parsed
strBuild->Append(strArray[0]);
for (int i = 1; i <>get_Count(); i++) {
strBuild->Append(" ");
strBuild->Append(strArray[i]);
}

//convert the StringBuilder to an immutable String and output
String *final = strBuild->ToString();
Console::WriteLine(final);

/*Conversion Example*/
//Here we see how to use the functions declared above to convert between
//the managed System::String class and the unmanaged STL std::string class
Console::WriteLine();
Console::WriteLine("Conversion Example:");

//the variables we will be using
String *managedString;
string stlString;

//converts a managed String to an STL string, using cout to output
managedString = S"Managed -> STL";
stlString = ManagedToSTL(managedString);
cout <<>

//converts an STL string to a managed String, using Console::WriteLine to output
stlString = "STL -> Managed";
managedString = STLToManaged(stlString);
Console::WriteLine(managedString);

return 0;
}


Comments

Popular posts from this blog

Outlook : "operation failed, object could not be found. " when trying to close a PST.

How to transfer app and data from old iPhone to new iPhone for model IOS17 and IOS18 above.

Tips on how to use IBM iNotes (web client) on emailing or scheduling - Like copy tables from Excel or checking for user id (windows logon id) through email address