-
August 21st, 2001, 09:50 PM
#1
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
-
August 22nd, 2001, 02:52 AM
#2
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
-
April 24th, 2003, 10:06 AM
#3
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;
}
-
April 24th, 2003, 11:02 AM
#4
Little correction
If you select a computer with the code supplied SHGetPathFromIDList (pidl,szDir) will return FALSE.
So, to get the computer name selected you will have to look at pszDisplayName parameter of BROWSEINFO structure. So:
char* pszRetVal = new char [MAX_PATH+1];
memset(pszRetVal, 0, MAX_PATH);
[...]
bi.pszDisplayName = pszRetVal;
pidl = SHBrowseForFolder(&bi);
if (pidl)
{
if (SHGetPathFromIDList(pidl,szDir))
{
delete pszRetVal;
return ((CString)szDir);
}
else
{
CString sRet = bi.pszDisplayName;
delete pszRetVal;
return sRet;
}
[...]
-
April 24th, 2003, 12:28 PM
#5
SHGetSpecialFolderLocation(NULL,CSIDL_NETWORK,&pidl) will work for Windows 95 / 98
SHGetFolderLocation only worked with WindowsMe or higher.
-
April 24th, 2003, 01:54 PM
#6
If you need more control of what is shown I think you can use WNetOpenEnum and related functions. You can probably find sample uses of it if you search for WNetOpenEnum.
-
April 24th, 2003, 02:00 PM
#7
You are absolutely right, but in this case you will have to provide your own UI
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|