Timur Yalymov
Timur Yalymov

Reputation: 319

WINAPI Network Discovery without SMBv1

I need to get a list of available shared folders on the local network, the way they appear in the "Network" tab in File Explorer. Earlier, I used combination of NetServerEnum/NetShareEnum functions to obtain it, but they are using SMBv1 protocol, which is now disabled by default in windows, so now i'm getting error 1231 from NetServerEnum. But File Explorer still cat obtain this list. I tried use Process Monitor to determine, which API it use, but failed. So, is there any way to get list of available shared folders in local network without using API, that requires SMBv1?

Upvotes: 1

Views: 390

Answers (1)

Drake Wu
Drake Wu

Reputation: 7190

You can use windows shell api and use FOLDERID_NetworkFolder to get the KNOWNFOLDERID of "network".

The following sample can get folders, nonfolders, and hidden items in the "network" folder.

#include <windows.h>
#include <Shobjidl.h>
#include <Shlobj.h>
#include <iostream>
void wmain(int argc, TCHAR* lpszArgv[])
{
    IShellItem* pShellItem;
    IEnumShellItems* pShellEnum = NULL;
    HRESULT hr = S_OK;
    hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("CoInitialize error, %x\n", hr);
        return;
    }

    hr = SHGetKnownFolderItem(FOLDERID_NetworkFolder, KF_FLAG_DEFAULT, NULL, IID_PPV_ARGS(&pShellItem));
    if (FAILED(hr))
    {
        printf("SHGetKnownFolderItem error, %x\n", hr);
        return;
    }

    hr = pShellItem->BindToHandler(nullptr, BHID_EnumItems, IID_PPV_ARGS(&pShellEnum));
    if (FAILED(hr))
    {
        printf("BindToHandler error, %x\n", hr);
        return;
    }

    do {
        IShellItem* pItem;
        LPWSTR szName = NULL;

        hr = pShellEnum->Next(1, &pItem, nullptr);
        if (hr == S_OK && pItem)
        {
            HRESULT hres = pItem->GetDisplayName(SIGDN_NORMALDISPLAY, &szName);
            std::wcout << szName << std::endl;
            CoTaskMemFree(szName);
        }
    } while (hr == S_OK);

    CoUninitialize();

}

Upvotes: 1

Related Questions