std::string Maker

Utility class for converting System::String to std::string, and more uses.

[Home]

#pragma once

#include<string>

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Diagnostics;

class StdStringMaker
{
private: std::string nativeString;

public: StdStringMaker(System::String^ str)
	{
	    Debug::Assert(str != nullptr); 
	    IntPtr ip = Marshal::StringToHGlobalAnsi(str);
	    this->nativeString = static_cast(ip.ToPointer());
	    Marshal::FreeHGlobal(ip);
	}

public: operator std::string() const
	{
	    return this->nativeString;
	}

//public: operator CComBSTR() const
//{
//    return this->nativeString.c_str();
//}

public: std::string ToString() const
	{
	    return this->nativeString;
	}
};