Search code examples
c++wnet

Calling WNetAddConnection2 with empty local name


I have a small program that simply checks if a specified file is located on a specified network drive that is not mapped on the computer.
To check this I temporarily map to the network location, check if the file exists and than unmap the drive. I now figured out that I can call WNetAddConnection2 with an empty local name (MSDN: If the string is empty, or if lpLocalName is NULL, the function makes a connection to the network resource without redirecting a local device.).
Just for showing the code:

NETRESOURCE nr;
nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = NULL; // explicitly set this to NULL
nr.lpRemoteName = "\\\\computer\\c$";
nr.lpProvider = NULL;

DWORD dwResult = WNetAddConnection2(&nr, cstrPassword, cstrUsername, FALSE);
if (dwResult != 0)
{
    return false;
}

CPath cLocation(cstrFileLocation);
return cLocation.FileExists() != FALSE;

So far so good the code works fine. But what I now want to know is if there is any problem with that call of WNetAddConnection2? I cannot call WNetCancelConnection, as I do not have a local name. So do I have some kind of zombies on my computer now?
How can I see all my network connections on my computer? Best would be a short command for the Command Prompt (something like NET USE).


Solution

  • Ok, figured it out. I can call WNetCancelConnection2(nr.lpRemoteName, 0, TRUE); to unmap the drive properly.