日期:2011-04-04  浏览次数:20381 次

 

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