Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c++ - 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?

question from:https://stackoverflow.com/questions/66053425/winapi-network-discovery-without-smbv1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", hr);
        return;
    }

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

    hr = pShellItem->BindToHandler(nullptr, BHID_EnumItems, IID_PPV_ARGS(&pShellEnum));
    if (FAILED(hr))
    {
        printf("BindToHandler error, %x
", 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();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...