Managed 문자열 -> Unmanaged 문자열 변환
Programming/C++/CLI / 2010. 5. 26. 17:14
//-- 인코딩 지정 변환 예
System::Text::Encoding^ m_encoding;
m_encoding = gcnew System::Text::UTF8Encoding();
m_stat, sql::SQLException* : legacy native c++ pointer
int execute(String^ sql)
{
try {
assert(m_stat);
if (!m_encoding) {
return m_stat->execute(getAnsiString(sql)); //.net -> native
} else {
array<unsigned char>^ bytes = m_encoding->GetBytes(sql);
pin_ptr<unsigned char> s = &bytes[0];
return m_stat->execute((const char*)s); //.net -> native
}
} catch (sql::SQLException *ex) {
throw gcnew SQLException(ex, sql);
}
}
//-- utf8 로 저장된 문자열 배열을, .net 유니코드 문자열 배열로 변환
array<String^>^ convStrArray(const std::vector<std::string> &arrStr) //native -> .net
{
size_t cnt = arrStr.size();
UTF8Encoding^ encode = gcnew UTF8Encoding(true,true);
array<String^>^ arr = gcnew array<String^>(cnt);
for (size_t i = 0; i < cnt; i++)
arr[i] = gcnew String(arrStr[i].c_str(), 0, arrStr[i].size(), encode);
return arr;
}
//-- strconv.h, vs2005/8
#pragma once
#include <vcclr.h>
#include <string>
using namespace System;
using namespace System::Runtime::InteropServices;
inline
std::string getAnsiString(String^ s)
{
IntPtr ip = Marshal::StringToHGlobalAnsi(s);
const char* str = static_cast<const char*>(ip.ToPointer());
std::string ansi(str);
Marshal::FreeHGlobal( ip );
return ansi;
}
inline
std::wstring getUniString(String^ s)
{
pin_ptr<const wchar_t> str = PtrToStringChars(s);
std::wstring uni(str);
return uni;
반응형
'Programming > C++/CLI' 카테고리의 다른 글
System::String -> char* OR wchar_t* (0) | 2010.05.26 |
---|---|
CLI - 초무식, 초단순 암기용 저장글 (0) | 2010.05.26 |
C++/CLI 예제 (0) | 2010.05.25 |
참고 사이트 (0) | 2010.05.25 |
CLI의 기본 타입 (0) | 2010.05.25 |