MFC need code allow user to browse network for computer and return UNC path to it
Does anyone know how I can present the user in my mfc app with a dialog box and allow them to browse the network for computers and select one. Then return to me the UNC \\ path to the computer they selected?
Many thanks
Peter
Re: MFC need code allow user to browse network for computer and return UNC path to it
Use SHBrowseForFolder with ulFlags = BIF_BROWSEFORCOMPUTER
Rating isn't important...But gurus respect it and keep high
Computer browse should start at "Network Neighborhood".
Interesting hint:
If you ask for browsing computers it makes no sense to start browsing at the SHBrowseForFolder default path, which is <user name>\Desktop. It would be much more elegant to start browsing at "Network Neighborhood".
BUT this is an "special folder", and even though it is a standard component of all systems, its name and location in the namespace can vary.
This makes necessary to use its CSIDL:
(shlobj.h : #define CSIDL_NETWORK 0x0012)
(you can read about it at MSDN "Getting a Folder's ID": http://msdn.microsoft.com/library/de.../folder_id.asp)
The code below uses all this to get a "computer browsing dialog":
int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData);
CString CMyDlg::browseComputers()
{
BROWSEINFO bi;
TCHAR szDir[MAX_PATH];
LPITEMIDLIST pidl;
LPMALLOC pMalloc;
if (SUCCEEDED(SHGetMalloc(&pMalloc)))
{
ZeroMemory(&bi,sizeof(bi));
bi.hwndOwner = NULL;
bi.pszDisplayName = 0;
HRESULT hRes = SHGetFolderLocation (NULL, CSIDL_NETWORK, NULL, 0, &pidl);
if (hRes == S_OK)
{
bi.pidlRoot = pidl;
}
else
{
bi.pidlRoot = 0;
}
bi.ulFlags = BIF_BROWSEFORCOMPUTER; bi.lpfn = BrowseCallbackProc;
pidl = SHBrowseForFolder(&bi);
if (pidl)
{
if (SHGetPathFromIDList(pidl,szDir))
{
return ((CString)szDir); }
pMalloc->Free(pidl);
pMalloc->Release();
}
}
return ("");
}
int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
{
TCHAR szDir[MAX_PATH];
CWnd* win;
switch(uMsg)
{
case BFFM_INITIALIZED:
{
if (GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir))
{
// WParam is TRUE since you are passing a path.
// It would be FALSE if you were passing a pidl.
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
}
break;
}
case BFFM_SELCHANGED:
{
// Set the status window to the currently selected path.
if (SHGetPathFromIDList((LPITEMIDLIST) lp, szDir))
{
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
}
break;
}
default:
break;
}
win = CWnd::FromHandle( hwnd );
CString sMsg;
sMsg.Format(IDS_DONDE_BD);
win->SetWindowText(sMsg);
return 0;
}