Not able to Print the folder path in EditBox- MFC
I am using the following function ..instead of CFolderDialog.. to get the folder path...check my code below....am getting a run time error when i try toprint the folder path name in a edit box..
void CSelf开发者_运维技巧ExtractorUIDlg::OnBnClickedButton1() {
CDialog dlg;
HWND hwnd = NULL; LPCTSTR szCurrent = (LPCTSTR)malloc(25*sizeof(TCHAR)); szCurrent = NULL; LPTSTR szPath = (LPTSTR)malloc(25*sizeof(TCHAR)); BOOL check = BrowseForFolder(hwnd,szCurrent,szPath); if( check == TRUE) { dlg.SetDlgItemTextW(IDC_EDIT1,szPath); }
}
BOOL BrowseForFolder(HWND hwnd, LPCTSTR szCurrent, LPTSTR szPath) { BROWSEINFO bi = { 0 }; LPITEMIDLIST pidl; TCHAR szDisplay[256]; BOOL retval;
//CoInitialize();
bi.hwndOwner = hwnd; bi.pszDisplayName = szDisplay; bi.lpszTitle = TEXT("Please choose a folder."); bi.ulFlags
= BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; bi.lpfn = BrowseCallbackProc; bi.lParam = (LPARAM) szCurrent;pidl = SHBrowseForFolder(&bi);
if (NULL != pidl) { retval = SHGetPathFromIDList(pidl, szPath); CoTaskMemFree(pidl); } else { retval = FALSE; }
if (!retval) { szPath[0] = TEXT('\0'); }
CoUninitialize(); return retval;
} static int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData) { // If the BFFM_INITIALIZED message is received // set the path to the start path.
switch (uMsg) { case BFFM_INITIALIZED: { if (NULL != lpData) { SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData); } } }return 0; // The function
25 characters seems a little short for a full path length, I'd use MAX_PATH.
精彩评论