There's a way to set the default encoding for ansi series apis in windows

99 4

What I've encountered is this:
2d, gdi. ansi, textout, ansi, utf 16le. utf 8 utf 16le,,, win32 ansi api"", getacp codepage., utf 8, utf 8 utf 16le, utf 16le ansi gbk, textout, textout gbk utf 16le.,.

I'd like to tell you that there's a way to convert the ansi series api in the process to 8? such as"setacp". So I can use 8 to textout in the application, which can be shown by textout, and it can be shown by.

4 Answers

102 0

There are two versions of the windows api, and you won't be able to use the unicode version directly. Why do you want to go twice. You go to cost coding, and the system is eventually converted to unicode and called a unicode version of api. For example, textout, you can explicitly call TextOutW, before calling, use MultiByteToWideChar to convert UTF-8 to unicode ( utf-16le ). For example, wrap the textout as gditextout, wrapped as both UTF-8 and utf-16le:

BOOL WINAPI GdiTextOut(
 HDC hdc,
 int nXStart,
 int nYStart,
 PCWSTR lpString,
 int cchString
)
{
 return ::TextOutW(hdc, nXStart, nYStart, lpString, cchString);
}
BOOL WINAPI GdiTextOut(
 HDC hdc,
 int nXStart,
 int nYStart,
 PCSTR lpString,
 int cchString
)
{
 WCHAR wszBuffer[1024] = { 0 };
 DWORD cchBuffer = sizeof(wszBuffer)/sizeof(WCHAR);
 int cchResult = MultiByteToWideChar(CP_UTF8, 0, lpString, -1, wszBuffer, cchBuffer);
 if (cchResult> 0)
 {
 return GdiTextOut(hdc, nXStart, nYStart, wszBuffer, cchResult);
 }
 BOOL bResult = FALSE;
 int cchNeeded = MultiByteToWideChar(CP_UTF8, 0, lpString, -1, NULL, 0);
 if (cchNeeded> 0)
 {
 HANDLE hHeap = GetProcessHeap();
 PWSTR lpBuffer = (PWSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cchNeeded * sizeof(WCHAR));
 if (lpBuffer!= NULL)
 {
 int cchResult = MultiByteToWideChar(CP_UTF8, 0, lpString, -1, lpBuffer, cchNeeded);
 if (cchResult> 0)
 {
 bResult = GdiTextOut(hdc, nXStart, nYStart, lpBuffer, cchResult);
 }
 HeapFree(hHeap, 0, (void *)lpBuffer);
 }
 }
 return bResult;
}
...