Convert char[] to LPCWSTR

前端 未结 5 925
轻奢々
轻奢々 2020-11-30 13:05

Can anyone help me to correct this code:

    char szBuff[64];
    sprintf(szBuff, \"%p\", m_hWnd);
    MessageBox(NULL, szBuff, L\"Test print handler\", MB_O         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 13:07

    For this specific case, the fix is quite simple:

    wchar_t szBuff[64];
    swprintf(szBuff, L"%p", m_hWnd);
    MessageBox(NULL, szBuff, L"Test print handler", MB_OK);
    

    That is, use Unicode strings throughout. In general, when programming on Windows, using wchar_t and UTF-16 is probably the simplest. It depends on how much interaction with other systems you have to do, of course.

    For the general case, if you've got an ASCII (or char *) string, use either WideCharToMultiByte for the general case, or mbstowcs as @Matthew points out for simpler cases (mbstowcs works if the string is in the current C locale).

提交回复
热议问题