当我试图将WNetAddConnection2调用到我已经有会话的机器时,我遇到了一个问题。这是预期的,因为您只能使用一组凭据连接到网络资源。我要做的是捕捉这个条件,并自动调用WNetCancelConnection2断开所有现有连接,然后重试WNetAddConnection2调用。当我运行下面的代码时,我会得到以下日志消息:
DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 0
DEBUG - WNetAddConnection2 returned 1219
如果我将dwFlags设置为CONNECT_UPDATE_PROFILE in WNetCancelConnection,则会得到以下日志消息:
DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 2250
DEBUG - WNetAddConnection2 returned 1219
这是我的资料来源,感谢大家的帮助!
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);
}
发布于 2013-09-09 14:08:36
这个问题还存在吗?还是你解决了?
我也发生了同样的失败,因为我打开了要连接到的资源的连接。这些连接在启动时由windows网络域的登录脚本自动打开。因此,我使用“网络使用”来断开它们(所有与目标计算机的连接)。在那之后,一切顺利。
这意味着它不是代码中的一个失败,而是windows网络中的一个问题。顺便说一句:无论如何,您应该使用"net“来检查代码是否成功,而不仅仅是信任调试消息。
以下是错误代码的链接:http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx
发布于 2016-10-27 09:03:05
我也面临着同样的问题,原因是:
正如它所说的,errorCode 1219意味着已经存在到该资源的连接。您可能正在使用WNetCancelConnection2(networkName,0,true)取消连接,但如果任何与该资源有连接的windows资源管理器都不会关闭该连接。因此,您确保如果有任何窗口显示该资源的内容,则手动关闭它们,然后尝试它将工作。无论如何,您都可以使用"net命令“来查看系统中有多少个n/w映射: use is = open命令提示符,它们键入: net,它将显示映射是否已经存在。
这是我编写的示例代码,它适用于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);
}
https://stackoverflow.com/questions/9085586
复制相似问题