Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why WNetAddConnection2 still returns 1219 after successfully calling WNetCancelConnection2?

I wrote some code to connect with some share on a remote server. If WNetAddConnection2 returns ERROR_SESSION_CREDENTIAL_CONFLICT (1219), I will first cancel the connection by WNetCancelConnection2 (return NO_ERROR). And then reconnect. But WNetAddConnection2 still returns 1219. Why this and how to fix it?

Here's my code

BOOL ADDirectorySearch::IPCConnect(CString strServerName, CString strDomainName, CString strUserName, CString strPassWord)
{
    CString strServerNameWithSlash = _T("\\\\") + strServerName; //actually is \\klbnt
    CString strFullUserName = strDomainName + _T("\\") + strUserName; //is domaintest\administrator
    _bstr_t bstrServerNameWithSlash = strServerNameWithSlash;
    _bstr_t bstrFullUserName = strFullUserName;
    _bstr_t bstrPassWord = strPassWord;
    DWORD dwResult;
    NETRESOURCEW netResource;
    memset(&netResource, 0, sizeof(netResource));
    netResource.dwScope = RESOURCE_GLOBALNET;  
    netResource.dwType = RESOURCETYPE_DISK;
    netResource.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;  
    netResource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
    netResource.lpProvider = L"";
    netResource.lpRemoteName = bstrServerNameWithSlash;//Remote IP like:\\192.168.1.11
    dwResult = WNetAddConnection2W(&netResource, bstrPassWord, bstrFullUserName, CONNECT_INTERACTIVE);
    if (dwResult == ERROR_SESSION_CREDENTIAL_CONFLICT)
    {
        dwResult = WNetCancelConnection2W(bstrServerNameWithSlash, CONNECT_UPDATE_PROFILE, TRUE);
        if (dwResult == NO_ERROR)
        {
            dwResult = WNetAddConnection2W(&netResource, bstrPassWord, bstrFullUserName, CONNECT_INTERACTIVE);
        }
        else
        {
            //MyMessageBox_Error(_T("IPCConnect Error."), _T("Error"));
            return FALSE;
        }
    }
    if (dwResult == NO_ERROR)
    {
        return TRUE;
    }
    else
    {
        //MyMessageBox_Error(_T("IPCConnect Error."), _T("Error"));
        return FALSE;
    }
}

FYI: After typing "net use" in cmd, I got this, I feel there's something with error:

Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK                     \\klbnt\NRDC1001          Microsoft Windows Network
The command completed successfully.
like image 709
hsluoyz Avatar asked Jul 15 '12 15:07

hsluoyz


1 Answers

I was just having this problem now, and basically it seemed that it was due to another process still having file open, even though I specified "true" as the last parameter of WNetCancelConnection2() to force close the connection. Once I shut-down that other process, I was able to use successfully switch between credentials connecting and re-connecting to the same share. This is on Windows 2012 (64-bit), and the share was local (referenced by the machinename).

BUT...it's still a problem if you want to connect to different shares on the same machine. If I try to connect to \\mymachine\share1 as user1 then to \\mymachine\share2 as user2, I get the 1219 error (even if it's in a completely different process). I have to explicitly call WNetCancelConnnection on \\mymachine\share1 before I can connect to share2, which means at the point you connect to a share on a particular machine, you may have to first enumerate existing connections and close each one.

Rather frustrating, and I can't understand the design principle here. It seems the flags to create temporary connections etc. have no effect on this behaviour either. Really what I want to be able to do is say "for this thread, connect to this share on this machine and as this user, such that all attempts to access files on the share are done with that user's credentials". That way what other processes/threads are doing can't cause issues with the current one.

like image 95
Dylan Nicholson Avatar answered Oct 26 '22 10:10

Dylan Nicholson