#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
/*
* 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;
// If input is null then just return the same.
if (NULL == pszA)
{
*ppszW = NULL;
return NOERROR;
}
// Determine number of wide characters to be allocated for the
// Unicode string.
cCharacters = strlen(pszA)+1;
// Use of the OLE allocator is required if the resultant Unicode
// string will be passed to another COM component and if that
// component will free it. Otherwise you can use your own allocator.
*ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
if (NULL == *ppszW)
return E_OUTOFMEMORY;
// Covert to 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;
// If input is null then just return the same.
if (pszW == NULL)
{
*ppszA = NULL;
return NOERROR;
}
cCharacters = wcslen(pszW)+1;
// Determine number of bytes to be allocated for ANSI string. An
// ANSI string can have at most 2 bytes per character (for Double
// Byte Character Strings.)
cbAnsi = cCharacters*2;
// Use of the OLE allocator is not required because the resultant
// ANSI string will never be passed to another COM component. You
// can use your own allocator.
*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
if (NULL == *ppszA)
return E_OUTOFMEMORY;
// Convert to 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;
}
HWND GetConsoleHwnd (wchar_t *newName)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
#ifdef UNICODE
wchar_t pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
wchar_t pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
#else
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
#endif // !UNICODE
// Fetch current window title.
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
#ifdef UNICODE
wsprintf(pszNewWindowTitle,TEXT("%d/%d"),
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
#else
wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
#endif // !UNICODE
SetConsoleTitle (newName);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
//SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
int main (void)
{
char str[80] = "Hello, World\n";
wchar_t *wstr;
int err;
err = AnsiToUnicode(str, &wstr);
GetConsoleHwnd (wstr);
if (wstr) {
CoTaskMemFree (wstr);
wstr = NULL;
}
printf ("Enter any key for terminating...\n");
_getch ();
return 0;
}
character 코드를 unicode로 변경 후, 콘솔창의 타이틀이름을 변경하는 예제이다.
--- 결과 ---
'Programming > C,C++' 카테고리의 다른 글
ansi2uni / uni2ansi 예제 in Linux, Win32 (0) | 2017.01.09 |
---|---|
Unicode에 관련하여[펌] (0) | 2017.01.09 |
Multithreading Applications in Win32 (0) | 2017.01.05 |
multithread in windows (0) | 2017.01.04 |
scanf 주의사항 (0) | 2017.01.04 |