Windows下获取主机共享文件夹列表信息

xingyun86 2021-4-16 1342

Windows下获取主机共享文件夹列表信息

#pragma once
#include <winsock2.h>
#include <lm.h>
#pragma comment(lib, "netapi32.lib")
#pragma comment(lib, "advapi32.lib")
#include <vector>
#include <Utils.h>

class CShareInfoMgr {
public:
    class ShareInfo {
    public:
        std::string name = ("");
        std::string path = ("");
    };
    std::vector<ShareInfo> m_shareInfoList = {};
    bool EnumLocalShareFolders()
    {
        PSHARE_INFO_502 pShareInfo502 = NULL;
        NET_API_STATUS dwRet = 0;
        DWORD dwLevel = 502;
        DWORD dwRefMaxLen = MAX_PREFERRED_LENGTH;
        DWORD dwEntriesRead = 0, dwTotalEntries = 0, dwResumeHandle = 0;

        m_shareInfoList.clear();

        do
        {
            dwRet = NetShareEnum(NULL, dwLevel, (LPBYTE*)&pShareInfo502, dwRefMaxLen, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
            if (pShareInfo502 == nullptr)
            {
                return false;
            }

            if (dwRet == ERROR_SUCCESS || dwRet == ERROR_MORE_DATA)
            {
                PSHARE_INFO_502 pPos = pShareInfo502;

                for (DWORD i = 0; i < dwEntriesRead; i++)
                {
                    ShareInfo shareInfo = {};
                    shareInfo.name = CUtils::W_To_A(pPos->shi502_netname);  //[1]
                    shareInfo.path = CUtils::W_To_A(pPos->shi502_path);          //[2]        

                    m_shareInfoList.emplace_back(shareInfo);

                    ++pPos;
                }
                NetApiBufferFree(pShareInfo502);
            }

        } while (ERROR_MORE_DATA == dwRet);

        return true;
    }
    void Print()
    {
        for (auto it : m_shareInfoList)
        {
            std::cout << it.name << std::endl;
            std::cout << it.path << std::endl;
            std::cout << "==============" << std::endl;
        }
    }
};


×
打赏作者
最新回复 (0)
查看全部
全部楼主
返回