[转] WNetAddConnection2 返回1219错误分析 --- 转自: http://hi.baidu.com/ssnhitfkgd/item/0a026e1ed0afe408d1d66d3b


WNetAddConnection2 返回1219错误分析

 

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.

一般情况下似乎并不会导致这个错误,有这个错误时用net use可以看到如下结果:

Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           \\10.99.99.3\FILES        Microsoft Windows Network
The command completed successfully.

注意这是一个连接,但是Local为空。测试表明,如果Local不为空,例如是Z:,则不会有问题,哪怕此前建立Z的时候用的是另一个用户名。用net use删除这个连接之后也不会再有这个问题。(net use /help可以看到所有用法)

注意有时候Status是Disconnect,这种情况一样。

重现这个现象的方法很简单,可用如下代码:

NETRESOURCE nr = {0};

nr.dwType = RESOURCETYPE_DISK;

nr.lpLocalName = L"";

nr.lpRemoteName = L\\\\10.99.99.3\\files;

WNetAddConnection2(&nr,L"passwd1",L"user1",CONNECT_UPDATE_PROFILE);

由此,建立了一个local为空的连接。

然后再用如下代码:

nr.lpLocalName = L"Z:";

WNetAddConnection2(&nr,L"passwd2",L"user2",CONNECT_UPDATE_PROFILE);

则返回1219错误。

如果前面nr.lpLocalName = L"" 改成nr.lpLocalName = L"Y:";则可以看到2次WNetAddConnection2调用均成功,且均建立了映射。

处理这个问题的方法也很简单:如果返回1219错误,则枚举本地连接,发现有到这个服务器的空连接则调用WNetCancelConnection2取消这个连接再次调用即可。

注意WNetCancelConnection2的第一个参数,如果这个连接有LocalName,则必须是LocalName,如果这个连接LocalName为空,则是RemoteName。枚举时需要判断一下。

另,RemoteName有2种形式,一种是基于IP地址的,例如\\10.99.99.3\files,另一种是基于计算机名的,例如\\netcore\files。这两种形式似乎是独立的,也即如果先用\\10.99.99.3\files 建立了一个LocalName为空的连接,此时再用另一个用户名密码建立到10.99.99.3的连接会返回1219,但如果此时用另一个用户名密码建立一个到netcore的连接则成功。

 

 

 

 

//设置及删除文件夹共享代码

View Code
using   System;   
  using   System.Management;   
    
  class   CreateShare   
  {   
  public   static   void   Main(string[]   args)   
  {   
  ManagementClass   _class   =   new   ManagementClass(new   ManagementPath("Win32_Share"));   
    
  object[]   obj   =   {"C://Temp","我的共享",0,10,"Dot   Net   实现的共享",""};   
    
  _class.InvokeMethod("create",obj);   
  }   
  }   
  
  //////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  
  
  //设置共享的方法 
using   System; 
using   System.Management; 
using   System.Windows.Forms;

namespace   WMISample 
{ 
        public   class   CallWMIMethod 
        { 
                public   static   void   Main() 
                { 
                        try 
                        { 
                                ManagementClass   classInstance   =   
                                        new   ManagementClass( "root//CIMV2 ",   "Win32_Share ",   null); 

                                //   Obtain   in-parameters   for   the   method 
                                ManagementBaseObject   inParams   =   
                                        classInstance.GetMethodParameters( "Create "); 

                                //   Add   the   input   parameters. 
                                inParams[ "Name "]   =     "share   name "; 
                                inParams[ "Path "]   =     "D://新建文件夹 "; 
                                inParams[ "Type "]   =     0; 

                                //   Execute   the   method   and   obtain   the   return   values. 
                                ManagementBaseObject   outParams   =   
                                        classInstance.InvokeMethod( "Create ",   inParams,   null); 

                                //   List   outParams 
                                Console.WriteLine( "Out   parameters: "); 
                                Console.WriteLine( "ReturnValue:   "   +   outParams[ "ReturnValue "]); 
                        } 
                        catch(ManagementException   err) 
                        { 
                                MessageBox.Show( "An   error   occurred   while   trying   to   execute   the   WMI   method:   "   +   err.Message); 
                        } 
                } 
        } 
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

删除共享的方法: 
using   System; 
using   System.Management; 
using   System.Windows.Forms; 

namespace   WMISample 
{ 
        public   class   CallWMIMethod 
        { 
                public   static   void   Main() 
                { 
                        try 
                        { 
                                ManagementObject   classInstance   =   
                                        new   ManagementObject( "root//CIMV2 ",   
                                        "Win32_Share.Name= 'Temp ' ", 
                                        null); 

                                //   no   method   in-parameters   to   define 


                                //   Execute   the   method   and   obtain   the   return   values. 
                                ManagementBaseObject   outParams   =   
                                        classInstance.InvokeMethod( "Delete ",   null,   null); 

                                //   List   outParams 
                                Console.WriteLine( "Out   parameters: "); 
                                Console.WriteLine( "ReturnValue:   "   +   outParams[ "ReturnValue "]); 
                        } 
                        catch(ManagementException   err) 
                        { 
                                MessageBox.Show( "An   error   occurred   while   trying   to   execute   the   WMI   method:   "   +   err.Message); 
                        } 
                } 
        } 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//设置共享的方法
using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementClass classInstance = 
                    new ManagementClass("root//CIMV2", "Win32_Share", null);

                // Obtain in-parameters for the method
                ManagementBaseObject inParams = 
                    classInstance.GetMethodParameters("Create");

                // Add the input parameters.
                inParams["Name"] =  "share name";
                inParams["Path"] =  "D://新建文件夹";
                inParams["Type"] =  0;

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = 
                    classInstance.InvokeMethod("Create", inParams, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}

 

 

 

//删除文件夹共享连接

调用WNetCancelConnection2, WNetAddConnection2之后 反回值是1219,怎么解决?

8180418 发表于 2011-12-9 14:41

用这个WinExec("NET USE * /DELETE /Y",SW_HIDE); 断开
WNetAddConnection2之后OK


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM