SetWindowText 显示来自 InternetReadFile() 的无法识别的代码
我想将使用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 编辑框始终显示无法识别的代码。
那么有人能发现我的代码的错误吗?
i want to convert an utf-8 encoded web page source code downloaded using InternetReadFile to utf-16 which windows can recognize.
here is the code snippet:
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);
hwndShowEdt editbox always show Unrecognizable Code while the web page is utf-8 encoded.
so can anyone find the mistake of my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
网页的字符集只能通过 HTML meta 标签识别:
the charset of the web page can only be recognized by HTML meta tag :