Search code examples
c++winapiremote-accessremote-server

How to Enumerate Volumes on Remote Computer?


I am running three computers in my home network, all within the same workgroup. Now I want to find out on my computer which volumes are present/mounted on the other computers im my network, and I want to do it in my C++/C# code.

Method NetServerEnum works fine and delivers the computer names in my network but now I am stuck and cannot identify a function which lists the volumes of such named 'remote' computer. I have already tried function NetServerDiskEnum but receive ERROR_ACCESS_DENIED. Is there another function that does the trick? (Any File dialog can do it but I need to include that into my code - and without an operator intervention.

Does anybody have a hint for me?

Update (2021-04-08) Thanks to Alan, he gave me a lot to try out. I decided to proceed with using WNetEnumResource which does not require any elevated privileges. Here is my code:

#include <windows.h>

int main ( int nArgc, char** lppszArgv )
{
    DWORD ErrCode;
    HANDLE hEnum;
    DWORD dwCount;
    DWORD BfSize = 0x1000;

    ErrCode = WNetOpenEnum ( RESOURCE_CONTEXT, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, NULL, &hEnum );
    if ( ErrCode == NO_ERROR )
    {
        BYTE ucBuffer[0x1000];
        NETRESOURCE* lpNr = (NETRESOURCE*)ucBuffer;

        do
        {
            dwCount = 1;
            ErrCode = WNetEnumResource ( hEnum, &dwCount, ucBuffer, (LPDWORD)&BfSize );
        }
        while ( ErrCode == NO_ERROR && dwCount == 1 );

        ErrCode = WNetCloseEnum ( hEnum );
    }

    return 0;
}

That code gives me a perfect list of all computers in my home network. Now I try to figure out how to obtain the names of volumes and/or directories that current user has access to. It might take me a while, I come back with more results.


Solution

  • I have already tried function NetServerDiskEnum but receive ERROR_ACCESS_DENIED.

    This is because only members of the Administrators or Server Operators local group can successfully execute the NetServerDiskEnum function on a remote computer.

    Refer: NetServerDiskEnum Remarks

    Instead, No special group membership is required to successfully execute the NetServerEnum function.

    Is there another function that does the trick?

    Considering that you are running these computers on your own home network, you can simply set the domain user with administrator privileges.

    Refer: How to set administrator rights to domain user?

    If you don't want to set administrator privileges, WMI is another option. Of course, the implementation method will be more complicated than calling NetServerDiskEnum.

    Using Get-Volume to get the specified Volume object, or all Volume objects if no filter is provided.

    Similar case: How to get disk capacity and free space of remote computer

    WMI can not only rely on PowerShell to implement their methods, but also C++. If you are interested, it is a good start to learn from this example.

    Back to the topic, members of the Administrators group are allowed to remotely connect to the computer by default. But you can grant DCOM remote startup and activation permissions for certain users and groups.

    Steps: Setting DCOM Security to Allow a User to Access a Computer Remotely