Is there a Windows user space function to enumerate connected network shares?

126 Views Asked by At

I'm writing a Win32 application and I need to know what the currently connected network file shares are. For example, \ip-addr\share-name\ or \Device\Mup\ip-addr\share-name\

The solution cannot involve mounting the drives.

I tried WNetOpenEnum, but that gives me null localnames and a remotename of "Web Client Network", not what I need.

I am testing with a Samba share connected.

1

There are 1 best solutions below

0
Ai4rei On
#include <stdio.h>
#include <windows.h>

int main(int nArgc, char** lppszArgv)
{
    HANDLE hEnum;

    if(NO_ERROR==WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, RESOURCEUSAGE_CONNECTABLE, NULL, &hEnum))
    {
        BYTE ucBuffer[0x4000];

        for(;;)
        {
            DWORD dwCount = -1, dwBufferSize = sizeof(ucBuffer);

            if(NO_ERROR==WNetEnumResource(hEnum, &dwCount, ucBuffer, &dwBufferSize))
            {
                DWORD dwIdx;
                NETRESOURCE* lpNr = (NETRESOURCE*)ucBuffer;

                for(dwIdx = 0; dwIdx<dwCount; dwIdx++)
                {
                    NETRESOURCE* lpItem = &lpNr[dwIdx];

                    printf("%u,%u,%u,%u,%s,%s,%s,%s\n",
                        lpItem->dwScope,
                        lpItem->dwType,
                        lpItem->dwDisplayType,
                        lpItem->dwUsage,
                        lpItem->lpLocalName==NULL ? "" : lpItem->lpLocalName,
                        lpItem->lpRemoteName,  // \\SERVER\share
                        lpItem->lpComment==NULL ? "" : lpItem->lpComment,
                        lpItem->lpProvider);
                }

                continue;
            }

            break;
        }

        WNetCloseEnum(hEnum);
    }

    return 0;
}

will return something along the lines of

1,3,3,0,Z:,\\SERVER1\SHARE,,Microsoft Network
1,3,3,0,,\\SERVER2\SHARE1,,Microsoft Network
1,3,3,0,,\\SERVER2\SHARE2,,Microsoft Network

The first line refers to a connected network drive letter, the other two are connected shares without a drive letter.