Skip to content

Instantly share code, notes, and snippets.

@JinnLynn
Last active April 2, 2019 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JinnLynn/f10f30a535220f42185a9afeef25f679 to your computer and use it in GitHub Desktop.
Save JinnLynn/f10f30a535220f42185a9afeef25f679 to your computer and use it in GitHub Desktop.
C# String std::string
using System;
#define s2S(s) gcnew String(s.c_str())
//REF: https://msdn.microsoft.com/en-us/library/1b4az623.aspx
string S2s(String ^s) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
string ret = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
return ret;
}
wstring S2ws(String ^s) {
using namespace Runtime::InteropServices;
const wchar_t* chars =
(const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
wstring ret = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment