Why WNetAddConnection2 still returns 1219 after successfully calling WNetCancelConnection2?

Viewed 10168

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.
1 Answers
Related