I'm running into an issue when I try to call WNetAddConnection2 to a machine in which I already have a session to. This is expected, as you can only connect to a network resource with only one set of credentials. What I'm trying to do is catch this condition and automatically call WNetCancelConnection2 to disconnect all existing connections and then retry the WNetAddConnection2 call. When I run the below code I get these log messages:
DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 0
DEBUG - WNetAddConnection2 returned 1219
If I set dwFlags to CONNECT_UPDATE_PROFILE in WNetCancelConnection I get these log messages:
DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 2250
DEBUG - WNetAddConnection2 returned 1219
Here is my source, all help is appreciated!
networkName = @"\\192.168.1.1";
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
int result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);
log.Debug("WNetAddConnection2 returned " + result);
if (result == 1219)
{
log.Debug("Multiple credentials detected, disconnecting all current sessions");
result = WNetCancelConnection2(networkName, 0, true);
log.Debug("WNetCancelConnection2 returned " + result);
result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);
log.Debug("WNetAddConnection2 returned " + result);
}
Solution for Error 1219 The workaround is simple. Instead of using the network name of the target server, use its IP address. That way Windows will interpret the IP address as a separate entry in its network connection list and there won't be an issue.
1219 Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.
The WNetAddConnection2 function makes a connection to a network resource and can redirect a local device to the network resource. The WNetAddConnection2 function supersedes the WNetAddConnection function.
Is this problem still existing or did you solve it?
I had the same failure because I had open connections to the resource that I wanted to connect to. These connections were opened automatically at startup by a logon script of our windows network domain. So I used "net use" to disconnect them (ALL connections to the target computer). After that it worked well.
This means it's not a failure in your code, but a problem in windows networking.
BTW: you should use "net use" anyway to see check your code for success and not only trust the debug messages.
And here's a link to the error codes: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx
I faced the same problem and the reason was:
as it says errorCode 1219 means there are connections already exist to that resource. You might be cancelling connections using WNetCancelConnection2(networkName, 0, true);, but this will not close if any windows explorer having connections to that resource. So you make sure if any windows showing the content of that resource you close them manually and then try it will work. Anyways you can always make use of "net command" to see how many n/w mappings are there in your system : usage is = open command prompt they type : net use It will show whether mapping is already there or not.
This is sample code I wrote and it works on win 8:
#include "stdafx.h"
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "mpr.lib")
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <Winnetwk.h>
#include<iostream>
#include<string>
// Need to link with Netapi32.lib and Mpr.lib
int _tmain(int argc, _TCHAR* argv[]){
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
DWORD cancelRetVal;
// Zero out the NETRESOURCE struct
memset(&nr, 0, sizeof(NETRESOURCE));
// Assign our values to the NETRESOURCE structure.
nr.dwType = RESOURCETYPE_ANY;
nr.dwScope = RESOURCE_GLOBALNET;
nr.lpLocalName =NULL;
nr.lpRemoteName = L"\\\\x.x.x.x\\folder";
nr.lpProvider = NULL;
// Assign a value to the connection options
dwFlags = CONNECT_UPDATE_PROFILE;
cancelRetVal = WNetCancelConnection2(L"\\\\x.x.x.x\\fodler", 0, true);
//usage WNetAddConnection2("location", L"password", L"domain\\username", 0);
dwRetVal = WNetAddConnection2(&nr, L"password", L"domain\\username", 0);
if (dwRetVal == NO_ERROR)
wprintf(L"Connection added to %s\n", nr.lpRemoteName);
else
wprintf(L"WNetAddConnection2 failed with error: %u\n", dwRetVal);
std::string s;
std::getline(std::cin, s);
exit(1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With