商业源码热门下载www.html.org.cn
//写这个程序,为了是显示局域网内的主机,然后通过用户点击选中的机器,检查是否该主机上安装有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);
}
新闻热点
疑难解答