CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 1999
    Posts
    36

    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


  2. #2
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658

    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

  3. #3
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    Lightbulb 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;
    }

  4. #4
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    Exclamation 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;
    }

    [...]

  5. #5
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    Exclamation

    SHGetSpecialFolderLocation(NULL,CSIDL_NETWORK,&pidl) will work for Windows 95 / 98
    SHGetFolderLocation only worked with WindowsMe or higher.

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    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
  •  





Click Here to Expand Forum to Full Width

Featured