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,怎么解決?
用這個WinExec("NET USE * /DELETE /Y",SW_HIDE); 斷開
WNetAddConnection2之后OK
