繁体   English   中英

WNetAddConnection2和错误1219 - 自动断开连接?

[英]WNetAddConnection2 and error 1219 - Automatically disconnect?

当我尝试将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);
}

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

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

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

因为它说errorCode 1219意味着该资源已经存在连接。 您可能正在使用WNetCancelConnection2(networkName,0,true);取消连接,但如果任何Windows资源管理器与该资源有连接,则不会关闭。 因此,您确定是否有任何窗口显示该资源的内容,您手动关闭它们,然后尝试它将工作。 无论如何,您总是可以使用“net命令”来查看系统中有多少个n / w映射:usage is = open命令提示符,它们键入: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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM