visual c++中支持unicode char的从TCHAR到char*的转换

visual Conversion from TCHAR to char* with unicode char support in c++

本文关键字:char 转换 TCHAR 的从 支持 unicode visual c++      更新时间:2023-10-16

如何用unicode字符将TCHAR转换为char*。我使用以下代码

//BROWSE FOLDER - Opens a browse folder dialog.
char* browse_folder(void)
{
char *selected_path = NULL;
TCHAR path[MAX_PATH];
BROWSEINFO bi = { 0 };
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl != 0)
{
    // get the name of the folder and put it in path
    SHGetPathFromIDList(pidl, path);
    //Set the current directory to path
    SetCurrentDirectory(path);
    // free memory used
    IMalloc* imalloc = 0;
    if (SUCCEEDED(SHGetMalloc(&imalloc)))
    {
        imalloc->Free(pidl);
        imalloc->Release();
    }
    //selected_path = ;
    USES_CONVERSION;
    selected_path = T2A(path);
}
}

我在visual studio项目设置中启用了unicode。我使用此函数来浏览文件夹,当我将其转换为char* unicode字符被替换为(?????)符号时,我在路径(TCHAR)中获得确切的值。

由于您的项目支持unicode字符,您应该使用WideCharToMultiByte()将其转换为char*。