The C # Call API displays the hosts within the internal LAN

Source: Internet
Author: User
Show

Write this program, in order to show the host within the LAN, and then click on the selected machine to check if the host is installed with SQL Server, is written for a configuration program
This provides code for friends who need it.

The setting DLL must use this namespace
Using System.Runtime.InteropServices;

#region Dynamic link Library definition
[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 Define enumeration constants
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 =,
   error_not_enough_memory = 8,
   error_network_busy = si,
   error_bad_netpath =,
   error_no_network = 1222,
   error _invalid_handle_state = 1609,
   error_extended_error= 1208
  }
  # Endregion


   #region start enumerating network functions
  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)); \ n Here is the two "\" characters before the machine name
Pnode3.imageindex = 2;
Pnode3.selectedimageindex = 2;
}

Wnetoe (NR);
}

}
}
Marshal.freehglobal (Ptrbuffer);
IRet =wnetcloseenum (Ptrhandle);
}
catch (Exception ex)
{
MessageBox.Show ("Find local network error:" +ex.) Message, "Error", Messageboxbuttons.ok,messageboxicon.error);
}
}

#endregion

Form Load event, use a function
private void Frmdbconnconfig_load (object sender, System.EventArgs e)
{
Wnetoe (NULL);
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.