分享
 
 
 

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

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

//写这个程序,为了是显示局域网内的主机,然后通过用户点击选中的机器,检查是否该主机上安装有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);

}

作者主页:itbaby.jss.cn

欢迎访问我的主页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有