SetWindowText显示来自InternetReadFile()的无法识别的代码

时间:2011-02-06 17:42:32

标签: c utf-8 utf-16 winapi

我想将使用InternetReadFile下载的utf-8编码网页源代码转换为windows可识别的utf-16。

这是代码段:

HINTERNET hConnect,hSession;
char *szBuffer = malloc(WEBSOURCE_SIZE);
wchar_t *wszBuffer = (wchar_t *)malloc(WEBSOURCE_SIZE);
TCHAR szStr[100];
DWORD dwSize=0;
DWORD dwDownloaded;
if(szBuffer == NULL)
{
    MessageBox(hwndGetBtn,TEXT("error"),TEXT("Error"),MB_ICONHAND);
}
GetWindowText(hwndUrlEdt,szStr,sizeof(szStr)/sizeof(TCHAR));
hSession = InternetOpen(TEXT("testWinINet"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
hConnect = InternetOpenUrl(hSession,szStr,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
ZeroMemory(szBuffer,WEBSOURCE_SIZE);
while (InternetReadFile(hConnect,szBuffer,WEBSOURCE_SIZE,&dwDownloaded))
{
    if (0==dwDownloaded) break;
    szBuffer[dwDownloaded]=0;
}
len=MultiByteToWideChar(CP_UTF8,0,szBuffer,-1,NULL,0);
MultiByteToWideChar(CP_UTF8,0,szBuffer,-1,wszBuffer,sizeof(wszBuffer));

SetWindowText(hwndShowEdt,wszBuffer);
当网页采用utf-8编码时,

hwndShowEdt编辑框始终显示无法识别的代码。

所以有人能找到我的代码的错误吗?

1 个答案:

答案 0 :(得分:0)

网页的字符集只能通过HTML meta 标记识别:

<meta http-equiv="Content-Type" content="text/html;charset=gb2312">