2013-03-22 151 views
2

如何將TCHAR轉換爲帶有Unicode字符的字符*。我使用follwong代碼從TCHAR轉換爲字符*在Unicode中支持unicode字符

//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 settings.i我使用這個功能來瀏覽文件夾,我在路徑(TCHAR)得到確切值,當我將其轉換爲char * Unicode字符替換爲(?????)符號。

+0

如果你想要char數據,爲什麼你不使用windows API函數的ansi對象(例如SHGetPathFromIDListA,SetCurrentDirectoryA ...)? – 2013-03-22 11:16:20

+0

你爲什麼要混合使用TCHAR和char?不要以爲你應該嘗試進行任何轉換。只需用相應的Windows數據類型TCHAR替換char即可。 – duleshi 2013-03-22 14:34:23

+0

我在npapi中使用這段代碼,並且npapi需要字符指針將此值傳遞給javascript。 – ragu 2013-03-23 14:39:23

回答

0

由於您的項目支持unicode字符,因此您應該使用WideCharToMultiByte()將其轉換爲char *。嘗試使用它。