包含头文件
#ifdef WIN32
#include <shlobj.h>
#endif
代码
bool func()
{
BROWSEINFO bi;
bi.hwndOwner = NULL;
bi.pidlRoot = CSIDL_DESKTOP;
bi.pszDisplayName = NULL;
bi.lpszTitle = NULL;
bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = NULL;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL)
{
return false;
}
TCHAR strFolder[MAX_PATH];
SHGetPathFromIDList(pidl, strFolder);
std::string sFolder = UnicodeToANSI(strFolder);
return false;
}
std::string UnicodeToANSI(const std::wstring& str)
{
char* pElementText;
int iTextLen;
iTextLen = ::WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL);
pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1));
::WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,pElementText,iTextLen,NULL,NULL);
std::string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}