to Unicode to Ansi

/* 
* AnsiToUnicode converts the ANSI string pszA to a Unicode string 
* and returns the Unicode string through ppszW. Space for the 
* the converted string is allocated by AnsiToUnicode. 
*/ 

HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) 
ULONG cCharacters; 
DWORD dwError; 

// 입력한 값이 NULL 이면 return 합니다. 
if (NULL == pszA) 
*ppszW = NULL; 
return NOERROR; 

// Unicode 로 할당될 문자열의 길이를 구합니다. 
cCharacters =  strlen(pszA)+1; 

// Unicode 문자열을 위해서 CoTaskMemAlloc() 함수를 사용하여 
// 문자열을 할당합니다. 
*ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2); 
if (NULL == *ppszW) 
return E_OUTOFMEMORY; 


// Unicode 로 바꿉니다. 
if (0 == MultiByteToWideChar(
CP_ACP, 
0, 
pszA, 
cCharacters, 
*ppszW, 
cCharacters)) 
dwError = GetLastError(); 
CoTaskMemFree(*ppszW); 
*ppszW = NULL; 
return HRESULT_FROM_WIN32(dwError); 
return NOERROR; 


/* 
* UnicodeToAnsi converts the Unicode string pszW to an ANSI string 
* and returns the ANSI string through ppszA. Space for the 
* the converted string is allocated by UnicodeToAnsi. 
*/ 
HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA) 
ULONG cbAnsi, cCharacters; 
DWORD dwError; 

// 입력 값이 NULL 이면 return 
if (pszW == NULL) 
*ppszA = NULL; 
return NOERROR; 

cCharacters = wcslen(pszW)+1; 
cbAnsi = cCharacters*2; 

*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi); 

if (NULL == *ppszA) 
return E_OUTOFMEMORY; 

// ANSI 문자열로 바꿉니다. 
if (0 == WideCharToMultiByte(
CP_ACP, 
0, 
pszW, 
cCharacters, 
*ppszA, 
cbAnsi, 
NULL, 
NULL)) 
dwError = GetLastError(); 
CoTaskMemFree(*ppszA); 
*ppszA = NULL; 
return HRESULT_FROM_WIN32(dwError); 
return NOERROR; 
}

'NIght.. > C++API+MFC' 카테고리의 다른 글

char 을 WCHAR 로, WCHAR 을 char 로  (0) 2008/10/30
Url 인코딩, 디코딩  (0) 2008/10/30
to Unicode to Ansi  (0) 2008/10/30
ShellExecute 사용예  (0) 2008/10/30
RegEnumValue, Key 사용예  (0) 2008/10/30
RB_GETBANDINFO 사용예  (0) 2008/10/30

Write your message and submit