C#如何取硬件标志

转载 2006年06月01日 09:28:00
原文:C#如何取硬件标志
using System;
using System.Runtime.InteropServices;
using  System.Management;
namespace Hardware
{
/// <summary>
/// Hardware_Mac 的摘要说明。
/// </summary>
public class HardwareInfo
{
  //取机器名
  public string GetHostName()
  {
   return System.Net.Dns.GetHostName();
  }

  //取CPU编号
  public String GetCpuID()
  {
   try
   {
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = mc.GetInstances();
      
    String strCpuID = null ;
    foreach( ManagementObject mo in moc )
    {
     strCpuID = mo.Properties["ProcessorId"].Value.ToString();
     break;
    }
    return strCpuID;
   }
   catch
   {
    return "";
   }

  }//end method

  //取第一块硬盘编号
  public String GetHardDiskID()
  {
   try
   {
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
    String strHardDiskID = null ;
    foreach(ManagementObject mo in searcher.Get())
    {    
     strHardDiskID = mo["SerialNumber"].ToString().Trim();
     break;          
    }
    return strHardDiskID ;
   }
   catch
   {
    return "";
   }
  }//end

  public enum NCBCONST
  {
   NCBNAMSZ   =16,      /* absolute length of a net name         */
   MAX_LANA   =254,      /* lana's in range 0 to MAX_LANA inclusive   */
   NCBENUM      =0x37,      /* NCB ENUMERATE LANA NUMBERS            */
   NRC_GOODRET   =0x00,      /* good return                              */
   NCBRESET    =0x32,      /* NCB RESET                        */
   NCBASTAT    =0x33,      /* NCB ADAPTER STATUS                  */
   NUM_NAMEBUF =30,      /* Number of NAME's BUFFER               */
  }

  [StructLayout(LayoutKind.Sequential)]
   public struct ADAPTER_STATUS
  {
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
   public byte[] adapter_address;
   public byte   rev_major;  
   public byte   reserved0;  
   public byte   adapter_type;  
   public byte   rev_minor;  
   public ushort    duration;  
   public ushort    frmr_recv;  
   public ushort    frmr_xmit;  
   public ushort    iframe_recv_err;  
   public ushort    xmit_aborts;  
   public uint   xmit_success;  
   public uint   recv_success;  
   public ushort    iframe_xmit_err;  
   public ushort    recv_buff_unavail;  
   public ushort    t1_timeouts;  
   public ushort    ti_timeouts;  
   public uint   reserved1;  
   public ushort    free_ncbs;  
   public ushort    max_cfg_ncbs;  
   public ushort    max_ncbs;  
   public ushort    xmit_buf_unavail;  
   public ushort    max_dgram_size;  
   public ushort    pending_sess;  
   public ushort    max_cfg_sess;  
   public ushort    max_sess;  
   public ushort    max_sess_pkt_size;  
   public ushort    name_count;
  }

  [StructLayout(LayoutKind.Sequential)]
   public struct NAME_BUFFER
  {  
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
   public byte[] name;
   public byte name_num;  
   public byte name_flags;  
  }

  [StructLayout(LayoutKind.Sequential)]
   public struct NCB
  {  
   public byte  ncb_command;  
   public byte  ncb_retcode;  
   public byte  ncb_lsn;  
   public byte  ncb_num;  
   public IntPtr ncb_buffer;  
   public ushort ncb_length;  
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
   public byte[]  ncb_callname;  
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
   public byte[]  ncb_name;  
   public byte  ncb_rto;  
   public byte  ncb_sto;  
   public IntPtr ncb_post;  
   public byte  ncb_lana_num;  
   public byte  ncb_cmd_cplt;  
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
   public byte[] ncb_reserve;
   public IntPtr ncb_event;
  }

  [StructLayout(LayoutKind.Sequential)]
   public struct LANA_ENUM
  {  
   public byte length;  
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.MAX_LANA)]
   public byte[] lana;
  }

  [StructLayout(LayoutKind.Auto)]
   public struct ASTAT
  {  
   public ADAPTER_STATUS adapt;
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NUM_NAMEBUF)]
   public NAME_BUFFER[] NameBuff;
  }
  public class Win32API
  {
   [DllImport("NETAPI32.DLL")]
   public static extern char Netbios(ref NCB ncb);
  }
  
  public string GetMacAddress()
  {
   string addr="";
   try
   {
    int cb;
    ASTAT adapter;
    NCB Ncb=new NCB();
    char uRetCode;
    LANA_ENUM lenum;

    Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
    cb = Marshal.SizeOf(typeof(LANA_ENUM));
    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
    Ncb.ncb_length = (ushort)cb;
    uRetCode = Win32API.Netbios(ref Ncb);
    lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
    Marshal.FreeHGlobal(Ncb.ncb_buffer);
    if(uRetCode != (short)NCBCONST.NRC_GOODRET)
     return "";

    for(int i=0; i < lenum.length ;i++)
    {
     Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
     Ncb.ncb_lana_num = lenum.lana[i];
     uRetCode = Win32API.Netbios(ref Ncb);
     if(uRetCode != (short)NCBCONST.NRC_GOODRET)
      return "";

     Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
     Ncb.ncb_lana_num = lenum.lana[i];
     Ncb.ncb_callname[0]=(byte)'*';
     cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER))*(int)NCBCONST.NUM_NAMEBUF;
     Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
     Ncb.ncb_length = (ushort)cb;
     uRetCode = Win32API.Netbios(ref Ncb);
     adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
     Marshal.FreeHGlobal(Ncb.ncb_buffer);

     if (uRetCode == (short)NCBCONST.NRC_GOODRET)
     {
      if(i>0)
       addr += ":";
      addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
       adapter.adapt.adapter_address[0],
       adapter.adapt.adapter_address[1],
       adapter.adapt.adapter_address[2],
       adapter.adapt.adapter_address[3],
       adapter.adapt.adapter_address[4],
       adapter.adapt.adapter_address[5]);
     }
    }
   }
   catch
   {}
   return addr.Replace(' ', '0');
  }

}
}

相关文章推荐

C#如何取硬件标志()

  • 2009年06月28日 15:50
  • 6KB
  • 下载

c#里BindingFlags 筛选标志

下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员: 为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。...

C#中使用Obsolete标志代码过期

  • 2015年07月10日 11:23
  • 31KB
  • 下载

c#去html标志,对字符串保留数字小数点,有中文的日期转化等特殊数据处理

命名空间 using System.Text.RegularExpressions; 1.我们采集到的数据 比如有些日期很不规则 带有中文 比如2013年5月8日     ...

C#正真实现C++中的数值型位域(非位标志)

C++中的位域成员有2种用途,一种是存放位标志的,一种是存放数值的。后一种没有搜到C#实现的例子,把实现的方式共享出来。...

C#系列--枚举类型与位标志

项目中应用:    [Flags]      public enum DayOfWeekMask     {                Sunday = 1,...
  • wtw1028
  • wtw1028
  • 2015年01月14日 10:49
  • 271

食品"QS"标志将取,以后买食品请认准"SC"

新修订的《食品安全法》已于10月1日起施行,作为新《食品安全法》的配套规章,国家食药监总局制定的《食品生产许可管理办法》(以下简称《办法》)也于10月1日起同步实施。对消费者来说最直观的变化是,食品包...
  • twgrace
  • twgrace
  • 2015年10月10日 17:17
  • 809
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#如何取硬件标志
举报原因:
原因补充:

(最多只允许输入30个字)