字符转换为WriteFile的Unicode

问题描述

我正在使用InternetReadFile下载数据,该数据只能存储到ansi缓冲区,即char *

我需要解析从InternetReadFile获得的数据(该数据可以具有俄语符号),并将其放入csv文件中。

我读了this answer,但没有帮助。在此之前,当我尝试只写wchar_t时,在打开csv文件的记事本中以及在其他工作表应用程序中出现了工件。使用答案中的代码,记事本可以正确显示它,但应用程序仍然无法显示

我现在在做什么:

  char *name; // some ansi data from InternetReadFile,which is perfectly displayed by MessageBoxA
  wchar_t *nameW[100];
  AnsiToUnicode(name,nameW);
  int len = WideCharToMultiByte(CP_UTF8,*nameW,wcslen(*nameW),NULL,NULL);
  if (len>0)
        WideCharToMultiByte(CP_UTF8,&name[0],len,NULL);

  HANDLE f = CreateFile(L"data.csv",GENERIC_WRITE,CREATE_ALWAYS,FILE_ATTRIBUTE_norMAL,NULL);
  DWORD wr;
  WriteFile(f,name,strlen(name),&wr,0);
  CloseHandle(f);

AnsiToUnicode from Microsoft.com

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,pszA,cCharacters,*ppszW,cCharacters))
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszW);
        *ppszW = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }

    return NOERROR;
}

在这种情况下如何正确保存unicode?​​ p>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)