您的位置:首页 > 编程语言 > C#

c#调用API显示内部局域网内的主机

2014-03-28 01:47 363 查看
//写这个程序,为了是显示局域网内的主机,然后通过用户点击选中的机器,检查是否该主机上安装有SQLSERVER,是为一个配置程序所写的

//这里提供CODE,给有需要的朋友

//设置DLL必须使用该命名空间

using System.Runtime.InteropServices;

  #region 动态链接库定义

  [DllImport("mpr.dll",CharSet=CharSet.Auto)] private static extern int WNetEnumResource(IntPtr hEnum, ref int lpcCount,IntPtr lpBuffer, ref int lpBufferSize );

  [DllImport("mpr.dll",CharSet=CharSet.Auto)] private static extern int WNetOpenEnum(RESOURCE_SCOPE dwScope,RESOURCE_TYPE dwType,RESOURCE_USAGE dwUsage,[MarshalAs(UnmanagedType.AsAny)][In] Object lpNetResource,out IntPtr lphEnum);

  [DllImport("mpr.dll",CharSet=CharSet.Auto)] private static extern int WNetCloseEnum( IntPtr hEnum );

  #endregion

  #region 定义枚举常量

  public enum RESOURCE_SCOPE

  {

   RESOURCE_CONNECTED = 0x00000001,

   RESOURCE_GLOBALNET = 0x00000002,

   RESOURCE_REMEMBERED = 0x00000003,

   RESOURCE_RECENT= 0x00000004,

   RESOURCE_CONTEXT= 0x00000005

  }

  public enum RESOURCE_TYPE

  {

   RESOURCETYPE_ANY= 0x00000000,

   RESOURCETYPE_DISK= 0x00000001,

   RESOURCETYPE_PRINT = 0x00000002,

   RESOURCETYPE_RESERVED = 0x00000008,

  }

  public enum RESOURCE_USAGE

  {

   RESOURCEUSAGE_CONNECTABLE =0x00000001,

   RESOURCEUSAGE_CONTAINER=0x00000002,

   RESOURCEUSAGE_NOLOCALDEVICE =0x00000004,

   RESOURCEUSAGE_SIBLING=0x00000008,

   RESOURCEUSAGE_ATTACHED=0x00000010,

   RESOURCEUSAGE_ALL =(RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),

  }

  public enum RESOURCE_DISPLAYTYPE

  {

   RESOURCEDISPLAYTYPE_GENERIC= 0x00000000,

   RESOURCEDISPLAYTYPE_DOMAIN= 0x00000001,

   RESOURCEDISPLAYTYPE_SERVER= 0x00000002,

   RESOURCEDISPLAYTYPE_SHARE= 0x00000003,

   RESOURCEDISPLAYTYPE_FILE = 0x00000004,

   RESOURCEDISPLAYTYPE_GROUP= 0x00000005,

   RESOURCEDISPLAYTYPE_NETWORK= 0x00000006,

   RESOURCEDISPLAYTYPE_ROOT = 0x00000007,

   RESOURCEDISPLAYTYPE_SHAREADMIN = 0x00000008,

   RESOURCEDISPLAYTYPE_DIRECTORY = 0x00000009,

   RESOURCEDISPLAYTYPE_TREE = 0x0000000A,

   RESOURCEDISPLAYTYPE_NDSCONTAINER = 0x0000000B

  }

  public struct NETRESOURCE

  {

   public RESOURCE_SCOPE dwScope;

   public RESOURCE_TYPE dwType;

   public RESOURCE_DISPLAYTYPE dwDisplayType;

   public RESOURCE_USAGE dwUsage;

   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpLocalName;

   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpRemoteName;

   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpComment;

   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpProvider;

  }

  public enum NERR

  {

   NERR_Success = 0,/* Success */

   ERROR_MORE_DATA = 234, // dderror

   ERROR_NO_BROWSER_SERVERS_FOUND = 6118,

   ERROR_INVALID_LEVEL = 124,

   ERROR_ACCESS_DENIED = 5,

   ERROR_INVALID_PARAMETER = 87,

   ERROR_NOT_ENOUGH_MEMORY = 8,

   ERROR_NETWORK_BUSY = 54,

   ERROR_BAD_NETPATH = 53,

   ERROR_NO_NETWORK = 1222,

   ERROR_INVALID_HANDLE_STATE = 1609,

   ERROR_EXTENDED_ERROR= 1208

  }

  #endregion

  #region 开始列举网络函数

  public TreeNode pNode1;

  public TreeNode pNode2;

  public TreeNode pNode3;

  private void WNETOE(Object o)

  {

   int iRet;

   IntPtr ptrHandle = new IntPtr();

   try

   {

    iRet =WNetOpenEnum(RESOURCE_SCOPE.RESOURCE_GLOBALNET,RESOURCE_TYPE.RESOURCETYPE_ANY,RESOURCE_USAGE.RESOURCEUSAGE_ALL,o,out ptrHandle );

    if( iRet != 0 )return;

    int entries;

    int buffer = 16384;

    IntPtr ptrBuffer = Marshal.AllocHGlobal(buffer);

    NETRESOURCE nr;

   

    for(;;)

    {

     entries = -1;

     buffer = 16384;

     iRet =WNetEnumResource(ptrHandle,ref entries,ptrBuffer,ref buffer);

     if((iRet != 0) || (entries<1))break;

     Int32 ptr = ptrBuffer.ToInt32();

     for(int i=0;i<entries;i++ )

     {

      nr = (NETRESOURCE)Marshal.PtrToStructure( new IntPtr(ptr), typeof(NETRESOURCE) );

      if(RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == (nr.dwUsage & RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))

      {

      

       ptr += Marshal.SizeOf( nr );

       if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_NETWORK)

       {

        pNode1 = tView.Nodes.Add(nr.lpRemoteName);//TreeView

        pNode1.ImageIndex = 0;

        pNode1.SelectedImageIndex = 0;

       }

       else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_DOMAIN)

       {

        pNode2 = pNode1.Nodes.Add(nr.lpRemoteName);

        pNode2.ImageIndex = 1;

        pNode2.SelectedImageIndex =1;

       }

       else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SERVER)

       {

        string sPCName = nr.lpRemoteName;

        pNode3 = pNode2.Nodes.Add(sPCName.Substring(2,sPCName.Length-2));\\此处为去处机器名称前的两个"\\"字符

        pNode3.ImageIndex = 2;

        pNode3.SelectedImageIndex = 2;

       }

       WNETOE(nr);

      }

     

     }

    }

    Marshal.FreeHGlobal( ptrBuffer );

    iRet =WNetCloseEnum( ptrHandle );

   }

   catch(Exception ex)

   {

    MessageBox.Show("查找本地网络错误:"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);

   }

  }

  #endregion

  //窗体LOAD事件中,使用函数

  private void frmDBConnConfig_Load(object sender, System.EventArgs e)

  {

   WNETOE(null);

  }
http://0cngrk5k.blog.com http://pd242td4.blog.com http://35176vyz.blog.com http://1m567a.blog.com http://1gxy9xrp.blog.com http://03e174.blog.com http://zr71k16.blog.com http://3d4xvqo.blog.com http://i4v73ag.blog.com http://7vnbbs40.blog.com http://52912s.blog.com http://26jt25q.blog.com http://ubo0e8.blog.com http://gc8yqx.blog.com http://68ux4ja.blog.com http://6vb4l7.blog.com http://5x92lg54.blog.com http://s7bdg5id.blog.com http://0lyg0p0v.blog.com http://974rr0q.blog.com http://x927h25s.blog.com http://np357v6r.blog.com http://32604v8.blog.com http://lq8dwj.blog.com http://6kim104.blog.com http://owa502.blog.com http://0qs4rs.blog.com http://61e7tn0.blog.com http://04ejl7.blog.com http://wqfr3h0.blog.com http://2a9923l2.blog.com http://273dn73.blog.com http://64382f2.blog.com http://eslip1.blog.com http://o2m7sm49.blog.com http://6q8w1g.blog.com http://096ive6.blog.com http://199f53.blog.com http://pf037f21.blog.com http://4jcvs5.blog.com http://0dma969.blog.com http://z3acbn5.blog.com http://06c48k3.blog.com http://x78ps3.blog.com http://408x8x4v.blog.com http://5ni9f75h.blog.com http://jit9bj13.blog.com http://92x6641n.blog.com http://4826zl.blog.com http://y0g2l23.blog.com http://p61ge0.blog.com http://t8167o2.blog.com http://38893m.blog.com http://0nk4w1.blog.com http://88018o3p.blog.com http://23gpsw.blog.com http://907u9im.blog.com http://45794wv.blog.com http://vi32m6v9.blog.com http://m80tc7.blog.com http://b18s3v.blog.com http://d9ifa08.blog.com http://nh83677.blog.com http://n7pd8jk.blog.com http://4a7516n8.blog.com http://5x6gaa.blog.com http://wrbj70.blog.com http://30hxi1.blog.com http://u9nchc.blog.com http://y57cm4.blog.com http://j8wkeonp.blog.com http://475rxefd.blog.com http://dk1l6n5n.blog.com http://55ge5zd.blog.com http://nopawfrm.blog.com http://28k1cv18.blog.com http://6qv8s995.blog.com http://0j2m4v3.blog.com http://9eg760.blog.com http://6f08072.blog.com http://5528w0m.blog.com http://s22qzgv9.blog.com http://d8z522.blog.com http://74elv7e.blog.com http://i4ylr1t6.blog.com http://401zfx2n.blog.com http://tm1k625r.blog.com http://zv27j7.blog.com http://7n1559pt.blog.com http://652wl91z.blog.com http://iwp5mfz9.blog.com http://82k543.blog.com http://q6xu7y3.blog.com http://s196194v.blog.com http://7e792ns.blog.com http://p14kiryn.blog.com http://wm1814v.blog.com http://97veqj21.blog.com http://3a8ud7.blog.com http://3gs6w1.blog.com http://bj9zse.blog.com http://891e3gd.blog.com http://35635i.blog.com http://vr2x348p.blog.com http://9u0z2bq.blog.com http://ly9943.blog.com http://4m6803xa.blog.com http://7m8u25.blog.com http://358oei.blog.com http://3o238v7k.blog.com http://9dwp1w9k.blog.com http://jelopb2.blog.com http://2awy8qld.blog.com http://49t376t.blog.com http://hap71y68.blog.com http://7w3uj81.blog.com http://96p8603y.blog.com http://q5u7zq.blog.com http://u0pn92j7.blog.com http://viy5nk1z.blog.com http://ea0gtt81.blog.com http://q68ld2.blog.com http://866g00c.blog.com http://18k5v8.blog.com http://lo63fc9.blog.com http://26b7wz21.blog.com http://9cs10er.blog.com http://xqa4mmq4.blog c052
.com
http://gfz374.blog.com http://yeb8ll.blog.com http://nb99s9.blog.com http://isrgwt72.blog.com http://kwy94o.blog.com http://cg2yf8up.blog.com http://p6fm3g83.blog.com http://3i80435u.blog.com http://328d7b44.blog.com http://n6esq391.blog.com http://d303ha.blog.com http://w11erj2n.blog.com http://796e61jm.blog.com http://179luy.blog.com http://c5uu9z1.blog.com http://13goa4qg.blog.com http://e21183.blog.com http://getdax.blog.com http://d17iiv.blog.com http://8q9fs9.blog.com http://w7o275.blog.com http://w7bbz3.blog.com http://3zloh376.blog.com http://kjn2w55x.blog.com http://z20o0co.blog.com http://f1cj16j.blog.com http://a869l08.blog.com http://phdr53.blog.com http://u86e4r.blog.com http://uags2h3.blog.com http://8u1lbd.blog.com http://16i3s4r.blog.com http://27dh241j.blog.com http://9r9r282.blog.com http://j3174p63.blog.com http://93bfbpev.blog.com http://vp5ads1i.blog.com http://kzy32crm.blog.com http://1qd4s2.blog.com http://m73rs0.blog.com http://q2639kz.blog.com http://b6p1wa35.blog.com http://864xek.blog.com http://j1u8bm5.blog.com http://23ss7gs.blog.com http://g64ing.blog.com http://9xznkr.blog.com http://4q269j.blog.com http://rgatixh.blog.com http://w17202.blog.com http://uc44ll9p.blog.com http://okuq188f.blog.com http://q0yj732u.blog.com http://0s264811.blog.com http://21enjyr.blog.com http://3d365p.blog.com http://exf409s.blog.com http://54twev0c.blog.com http://44p2663.blog.com http://25tv5bd.blog.com http://m90749.blog.com http://962xch5j.blog.com http://763m3m1v.blog.com http://v39e416b.blog.com http://5qz2pf.blog.com http://k4f1237s.blog.com http://yh8y17.blog.com http://46b0ql1.blog.com http://n1ufo075.blog.com http://srw8818.blog.com http://yeej53.blog.com http://st698l.blog.com http://k0afu1te.blog.com http://hb7185.blog.com http://qi4300hr.blog.com http://s276w8fl.blog.com http://g6esqt.blog.com http://w0vfgc8.blog.com http://5gb32nn.blog.com http://bbh8e3.blog.com http://3moogx7.blog.com http://vbk0o7.blog.com http://4o054564.blog.com http://68bix110.blog.com http://457914q.blog.com http://9dg5std0.blog.com http://9313m0.blog.com http://22o10c.blog.com http://15ywp7.blog.com http://lh7x4fw.blog.com http://y4oo61.blog.com http://k4638ypk.blog.com http://qac570.blog.com http://82n2s4o1.blog.com http://07b1m9.blog.com http://n6z883.blog.com http://scrwut9l.blog.com http://nw62w9s.blog.com http://m4f7r0.blog.com http://6p2s767b.blog.com http://7jy47c7d.blog.com http://t3bc54.blog.com http://78gtovfl.blog.com http://88jmw4mz.blog.com http://1quivwf.blog.com http://lazjh0wv.blog.com http://1n8p826.blog.com http://b8hr9s5f.blog.com http://fledwdv.blog.com http://32l67bco.blog.com http://ew8r4w9j.blog.com http://6bn993h.blog.com http://sv84b3t0.blog.com http://f6275he.blog.com http://lw19ret.blog.com http://8vr8b8.blog.com http://1r652uk.blog.com http://952ueo5p.blog.com http://992u95n.blog.com http://fg3z743a.blog.com http://a8g6i56.blog.com http://6d6zycq.blog.com http://7il9b8s.blog.com http://o63bfpg.blog.com http://lkm7ul48.blog.com http://g9377f0.blog.com http://t65398.blog.com http://82s90r7.blog.com http://ajqou71.blog.com http://b5403x3.blog.com http://rh3yl9.blog.com http://ekx8363.blog.com http://z67k3pdv.blog.com http://vi7h6w8.blog.com http://5kg75l9.blog.com http://6u22qed.blog.com http://i8p6i75w.blog.com http://2o24wd.blog.com http://h141rg6.blog.com http://l7ufj0.blog.com http://g9xde84.blog.com http://8dy21q03.blog.com http://lwpgl74.blog.com http://13he5210.blog.com http://qi7701.blog.com http://gf083683.blog.com http://ave7288.blog.com http://xt25v5no.blog.com http://etf996h1.blog.com http://7ny363u6.blog.com http://vb58kg04.blog.com http://05giafc6.blog.com http://j3av919c.blog.com http://o8u01b.blog.com http://1wb37292.blog.com http://8zze9627.blog.com http://5b76r88.blog.com http://239bwp.blog.com http://9h92jfz5.blog.com http://13e934.blog.com http://742t7yfd.blog.com http://2yb2k2hm.blog.com http://87a06c22.blog.com http://49x5qh.blog.com http://ylb701nw.blog.com http://vcd2h99.blog.com http://s22ed0q.blog.com http://ub5m40.blog.com http://xnv071.blog.com http://t5a06p1t.blog.com http://5amncml.blog.com http://65n0pb.blog.com http://3vn1340.blog.com http://u3f4980.blog.com http://xzt5695x.blog.com http://n9b794.blog.com http://e8h8683.blog.com http://qfp3q1f.blog.com http://2kmhw4fm.blog.com http://5ewhe2q.blog.com http://6x48uc.blog.com http://502dv2.blog.com http://ig9e126.blog.com http://8oi7p0.blog.com http://71wrsp.blog.com http://18y820.blog.com http://jo1rzcbp.blog.com http://m6wxdpsr.blog.com http://385g5f.blog.com http://21co2849.blog.com http://45e8f710.blog.com http://d5774541.blog.com http://flk16y.blog.com http://25gi0l.blog.com http://yzojh6.blog.com http://7mrm5e.blog.com http://945kj18.blog.com http://558081o.blog.com http://74tm80g.blog.com http://2bfdnn0.blog.com http://toubj007.blog.com http://b4u85rs4.blog.com http://c3a855.blog.com http://coheaf3.blog.com http://vumv62w9.blog.com http://529h740v.blog.com http://j622g5l7.blog.com http://3w5zo7.blog.com http://dy3063e.blog.com http://u9918nf.blog.com http://w7as60d7.blog.com http://d3gpb1.blog.com http://8c831u1p.blog.com http://0c48gs9.blog.com http://u5823c.blog.com http://59w37jz.blog.com http://c4275m8.blog.com http://284255l.blog.com http://24yw86.blog.com http://rfsi6e1u.blog.com http://370id8.blog.com http://11483y6l.blog.com http://003maqp5.blog.com http://2r8942.blog.com http://zm941h49.blog.com http://rq7k06.blog.com http://11483y6.blog.com http://35iu59t9.blog.com http://p70ieod.blog.com http://85g63o.blog.com http://q1iqmf9.blog.com http://1889gf.blog.com http://d2c70r.blog.com http://dku1595k.blog.com http://xh9lth.blog.com http://4w7271.blog.com http://1lg894.blog.com http://3hj061t.blog.com http://cfda1i.blog.com http://8lt38r.blog.com http://idas305.blog.com http://i0gkf953.blog.com http://9r7jy0n1.blog.com http://7dp0794.blog.com http://ih448sy.blog.com http://z67u6s8w.blog.com http://sd8egx.blog.com http://pq463l.blog.com http://7s8512.blog.com http://10a68a.blog.com http://jd4713x.blog.com http://gg393p42.blog.com http://952k01.blog.com http://g057uc2n.blog.com http://a0nnj0w.blog.com http://6945w4.blog.com http://vx4u901i.blog.com http://xz1lqji.blog.com http://0n172d3.blog.com http://60xpyw46.blog.com http://8gk5586w.blog.com http://07ews5c0.blog.com http://twcv92j6.blog.com http://1b43tw8.blog.com http://85h992o.blog.com http://1c64qk6.blog.com http://qkcmp6h0.blog.com http://whe8ycg2.blog.com http://i43zwhr.blog.com http://0x9p1y.blog.com http://v222ekx5.blog.com http://yx53ala6.blog.com http://p19ahnmm.blog.com http://1t101oh.blog.com http://m61j8u.blog.com http://djctwga5.blog.com http://02dz04yv.blog.com http://d0q37p.blog.com http://8ixo2id.blog.com http://4q9694.blog.com http://33ehas.blog.com http://53hj01.blog.com http://56xr3gv.blog.com http://a7r8029.blog.com http://mpxq984.blog.com http://r38g6je7.blog.com http://3sddusu.blog.com http://2p93m919.blog.com http://09gt20e.blog.com http://0c47cj.blog.com http://f20j0d4.blog.com http://d9mf8h7x.blog.com http://9rtx1m8.blog.com http://7fu2v52.blog.com http://380gc05.blog.com http://6x3xey.blog.com http://36w3h471.blog.com http://88tq61.blog.com http://x8ltnz.blog.com http://mvh8a77.blog.com http://0tt84jt.blog.com http://3zo986qf.blog.com http://y7y6gksu.blog.com http://9pv6n553.blog.com http://3u0j8036.blog.com http://qu2x27.blog.com http://q5ed94iy.blog.com http://wjbl3z88.blog.com http://oga2h9sx.blog.com http://0cp35yyy.blog.com http://86zq650.blog.com http://o02o8d.blog.com http://j4mddc.blog.com http://wxdu700.blog.com http://awztui6.blog.com http://k2z9222e.blog.com http://zl8it7ik.blog.com http://50iayalh.blog.com http://g89a3n.blog.com http://v19wdd.blog.com http://c8hlm7o8.blog.com http://uu5249x.blog.com http://675188vl.blog.com http://ehed0dg8.blog.com http://9uonw9.blog.com http://51968b.blog.com http://og9y65.blog.com http://4r6711e8.blog.com http://891nyz.blog.com http://n2n4qc.blog.com http://453fl9.blog.com http://3p78gk.blog.com http://5f16cs7p.blog.com http://017p3n1.blog.com http://54low3f.blog.com http://i9vs1li7.blog.com http://ww95hixl.blog.com http://7l3bo0fw.blog.com http://bt1ci1.blog.com http://6891pke5.blog.com http://6u93413r.blog.com http://7z5p45.blog.com http://b24g4o39.blog.com http://ahtn0d.blog.com http://jk5g17ez.blog.com http://z0881or4.blog.com http://05y69h.blog.com http://zk2up0.blog.com http://bpxtyjy5.blog.com http://c7d2oqfg.blog.com http://0w4hin.blog.com http://6ig34b78.blog.com http://u802bw2.blog.com http://v0sfj0xt.blog.com http://o49656.blog.com http://468xgn.blog.com http://0by1sp59.blog.com http://6mqsu72.blog.com http://r8y002v.blog.com http://6qx5rdog.blog.com http://3t28d3o.blog.com http://r8o3bhs.blog.com http://8svdy168.blog.com http://mrws8ga5.blog.com http://d14426.blog.com http://b80jh3.blog.com http://76ou156.blog.com http://a3ie7dtf.blog.com http://4y4554.blog.com http://o0l8v5b8.blog.com http://neo71u2.blog.com http://8h79o7.blog.com
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: