【问题标题】:WNetAddConnection2 and error 1219 - Automatically disconnect?WNetAddConnection2 和错误 1219 - 自动断开连接?
【发布时间】:2016-10-27 17:03:05
【问题描述】:

当我尝试将 WNetAddConnection2 调用到我已经有会话的机器时遇到了问题。这是意料之中的,因为您只能使用一组凭据连接到网络资源。我要做的是捕捉这种情况并自动调用 WNetCancelConnection2 以断开 所有 现有连接,然后重试 WNetAddConnection2 调用。当我运行以下代码时,我会收到这些日志消息:

DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 0
DEBUG - WNetAddConnection2 returned 1219

如果我在 WNetCancelConnection 中将 dwFlags 设置为 CONNECT_UPDATE_PROFILE,我会收到以下日志消息:

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);
}

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    这个问题仍然存在还是你解决了?
    我遇到了同样的失败,因为我打开了与我想要连接的资源的连接。这些连接在启动时由我们的 Windows 网络域的登录脚本自动打开。所以我使用“net use”来断开它们(与目标计算机的所有连接)。之后效果很好。

    这意味着这不是您的代码故障,而是 Windows 网络的问题。 顺便说一句:无论如何,您都应该使用“net use”来检查您的代码是否成功,而不仅仅是信任调试消息。
    这是错误代码的链接:http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx

    【讨论】:

    • 这是 Windows 网络中的一个已知问题,解决方法是为同一台机器使用替代名称。例如。使用主机名而不是 IP 地址,反之亦然。
    • @MSalters:所以您只是将可能的登录凭据集数量增加了一倍,但您并没有解决问题。
    • “例如”只是一个例子。您可以为单台计算机分配无限数量的 DNS 名称。但是,是的,“解决方法”意味着根本问题没有解决。请注意,这不是WNetAddConnection 最严重的问题。有一个 28 岁的错误,它忽略了 CancelIo
    【解决方案2】:

    我遇到了同样的问题,原因是:

    正如它所说的 errorCode 1219 意味着该资源已经存在连接。 您可能正在使用 WNetCancelConnection2(networkName, 0, true); 取消连接,但如果任何 Windows 资源管理器连接到该资源,这将不会关闭。 因此,请确保是否有任何显示该资源内容的窗口手动关闭它们,然后尝试它会起作用。 无论如何,您总是可以使用“net 命令”来查看系统中有多少个 n/w 映射:用法是=打开命令提示符,他们键入:net use 它会显示映射是否已经存在。

    这是我编写的示例代码,它适用于 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);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 2020-03-23
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多