Network Enumerations (3)

Having seen the 2 previous offerings which front load the enumeration in such a way that on a large or awkward network they can take a long time to return, I took a look at the browse for folder code which was rather cumbersome but with some tinkering reduced it to the following:


LPITEMIDLIST pidlRoot = NULL;
SHGetSpecialFolderLocation(GetSafeHwnd(), CSIDL_NETWORK, &pidlRoot);

CString strDisplayName;
BROWSEINFO bi;
memset(&bi, 0, sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = pidlRoot;
bi.pszDisplayName = strDisplayName.GetBuffer(MAX_PATH + 1);
bi.lpszTitle = "Find computer";
bi.ulFlags = BIF_BROWSEFORCOMPUTER;

LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
strDisplayName.ReleaseBuffer();

if(pidl)
{
    m_machine = strDisplayName;
    UpdateData(FALSE);
}

IMalloc *pMalloc = NULL;
SHGetMalloc(&pMalloc);
pMalloc->Free(pidlRoot);


Create a dialog box with a CEdit control on it. Create a member variable for this CEdit called m_machine. Attach the code above to a button (any other than OK or Cancel).