String Converter

Native CLI string inter-conversions.


Home

#pragma once

#using <mscorlib.dll>

#include <VCCLR.h>
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;

class CLR2NativeConvert
{
private: System::IntPtr _stringPtr;
public: CLR2NativeConvert(String^ clrString) : _stringPtr(nullptr)
{
_stringPtr = Marshal::StringToHGlobalAnsi(clrString);
}

public: std::string ToStdString()
{
const char* chars = static_cast<const char*>(_stringPtr.ToPointer());
return std::string(chars);
}

public: std::wstring ToStdWString()
{
const char* chars = static_cast<const char*>(_stringPtr.ToPointer());
std::string str(chars);
return std::wstring(str.begin(), str.end());
}

public: ~CLR2NativeConvert()
{
if (IntPtr::Zero != _stringPtr)
{
Marshal::FreeHGlobal(_stringPtr);
}
}
};

ref class StringConverter
{
#pragma region CLR To Native String Conversions

public: static std::string ToStdString(String^ clrString)
{
CLR2NativeConvert cnc(clrString);
return cnc.ToStdString();
}

public: static std::wstring ToStdWString(String^ clrString)
{
CLR2NativeConvert cnc(clrString);
return cnc.ToStdWString();
}

public: static void ConvertStringToLPCWSTR(System::String^ clrString,
unsigned int length,
LPCWSTR lpcwString)
{
pin_ptr<const wchar_t> wch = PtrToStringChars(clrString);
// Convert to a char*
size_t origsize = wcslen(wch) + 1;
size_t convertedChars = 0;
char* nstring = new char[length + 1];
wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
// Convert to a wchar_t*
wcscpy_s((wchar_t*)lpcwString, length + 1, wch);
delete[] nstring;
}

public: static void ConvertStringToLPCSTR(System::String^ clrString,
unsigned int length,
LPCSTR lpcString)
{
pin_ptr<const wchar_t> wch = PtrToStringChars(clrString);
// Convert to char*
size_t origsize = wcslen(wch) + 1;
size_t convertedChars = 0;
char* nstring = new char[length+1];
wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
strcpy_s((char*)lpcString, length + 1, nstring);
delete nstring;
}

#pragma endregion

#pragma region Native To CLR String Conversions

public: static String^ ToString(const std::wstring& nativeStr)
{
return gcnew String(nativeStr.c_str());
}

public: static String^ ToString(const std::string& nativeStr)
{
return gcnew String(nativeStr.c_str());
}

#pragma endregion

#pragma region Native To Native String Conversions

public: std::string ToStdString(const wchar_t* wpsz)
{
std::wstring wstr(wpsz);
return ToStdString(wstr);
}

public: std::string ToStdString(const std::wstring& wstr)
{
return std::string(wstr.begin(), wstr.end());
}

public: static void ConvertStdStringToLPCWSTR(std::string& stdString, LPCWSTR lpcwString)
{
int slength = (int)stdString.length() + 1;
int len = MultiByteToWideChar(CP_ACP, 0, stdString.c_str(), slength, 0, 0);

wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, stdString.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
lpcwString = r.c_str();
}

#pragma endregion
};