Calling WNetAddConnection2 with empty local name

前提是你 提交于 2019-12-24 02:24:10

问题


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).


回答1:


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



来源:https://stackoverflow.com/questions/2296918/calling-wnetaddconnection2-with-empty-local-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!