建立空连接的代码

using System;
using System.Runtime.InteropServices;

namespace winUser
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class Class1
 {
  public Class1()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  const   int   RESOURCETYPE_ANY=0x00000000;

  /**// 代码片断 2.1 声明
  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  public struct USER_INFO_0
  {
   public String Username;
  }
  [StructLayout(LayoutKind.Sequential)]  
   public   struct   NetResource
  {
   public   int   dwScope;
   public   int   dwType;
   public   int   dwDisplayType;
   public   int   dwUsage;
   public   string   lpLocalName;
   public   string   lpRemoteName;
   public   string   lpComment;
   public   string   lpProvider;
  }
 
  [DllImport("Netapi32.dll")]
  extern static int NetUserEnum([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, int filter, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out int resume_handle);
 
  [DllImport("Netapi32.dll")]
  extern static int NetApiBufferFree(IntPtr Buffer);

  [DllImport("mpr.dll")]
  static   extern   int  WNetAddConnection2([MarshalAs(UnmanagedType.Struct)]   ref   NetResource   lpNetResource,string   lpPassword,string   lpUserName,int   dwFlags);
  
  [DllImport("mpr.dll")]
  static   extern   int WNetCancelConnection2( string lpName,int   dwFlags, bool fForce );
  //WNetCancelConnection2(LPCTSTR lpName, DWORD dwFlags, BOOL fForce)
  /**/ 代码结束

  public string[] GetUsers( string ip )
  {
   ip = "" + ip;
   if ( Connect( ip,"","" ) )
   {
    int EntriesRead;
    int TotalEntries;
    int Resume;
    IntPtr bufPtr;
    NetUserEnum(ip, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
    CancelConnection( ip );
    if(EntriesRead> 0)
    {
     string[] Users = new string[EntriesRead];
     IntPtr iter = bufPtr;
     for(int i=0; i < EntriesRead; i++)
     {
      Users[i] = ((USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0))).Username; 
      iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
      //MessageBox.Show(Users[i].Username);
     }
     NetApiBufferFree(bufPtr);
     return Users;
    }
    else
    {
     return null;
    }
   }
   else
   {
    return null;
   }
   

  }

  public bool Connect( string  ipc,string name,string passwd )
  {
   NetResource   nr=new   NetResource();
   nr.lpLocalName=null;
   nr.lpProvider=null;
   nr.dwType=RESOURCETYPE_ANY;
   nr.lpRemoteName=ipc;
   int   ret=WNetAddConnection2(ref nr,passwd,name,0);
   if ( ret == 0 )
    return true;
   else
    return false;
  }

  public bool CancelConnection( string  ipc )
  {
   int   ret= WNetCancelConnection2( ipc,0,true );
   if ( ret == 0 )
    return true;
   else
    return false;
  }
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值