C#如何取硬件标志

原创 2004年09月08日 13:41:00
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');
}

}
}


进销存软件之OO设计--中间层处理(一)

进销存软件之OO设计――中间层业务逻辑(一)  一 前言:本文是作者个人的软件作品/产品 iSale商业进销存系统的部分设计思路,软件还在最后完善中,文中摘录这个软件中的部分设计思路及实现方法与网友共...
  • cocoboy79
  • cocoboy79
  • 2004-02-17 18:50:00
  • 1429

C#如何取硬件标志()

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

C#如何取硬件标志,取机器名,取CPU编号,第一块硬盘编号 源代码

  • 2009年04月23日 13:34
  • 2.53MB
  • 下载

C#如何取硬件标志

using System; using System.Runtime.InteropServices; using System.Management; namespace Hardware { //...
  • 21aspnet
  • 21aspnet
  • 2007-03-24 16:30:00
  • 2994

C#如何取硬件标志.rar

  • 2008年10月17日 21:54
  • 4.66MB
  • 下载

C#中如何取硬件标志

From:http://blog.csdn.net/drago/archive/2004/09/11/101506.aspxusing System;using System.Runtime.Inte...
  • mwaterfall
  • mwaterfall
  • 2004-09-11 20:03:00
  • 615

c#各种应用技巧实现(打包集)

  • 2009年09月26日 21:32
  • 3.1MB
  • 下载

如何通过C#获取硬件信息。

在Windows下有个叫wmi的东西可以获取。微软还为此做了个wmi代码生成小工具。 但是这个东西着实不好用。获取到的温度都是不会刷新的。我推荐使用Open Hardware Monitor 这个开...
  • a88141302
  • a88141302
  • 2015-02-27 15:47:44
  • 1538

asm基础——汇编指令之CPU的标识位操作指令

汇编中CPU标识位相关的指令。
  • jiangwei0512
  • jiangwei0512
  • 2016-01-29 22:27:37
  • 981

标志枚举的使用

标志枚举的使用大多是在标记多重状态,比如说文件的属性:只读,可写,隐藏,系统文件等相关属性,都对应相应的标志位,如果在C#中想实现自己的标志枚举,也是可以的,下文是亲身试验的一段代码,希望对您有所帮助...
  • myinc
  • myinc
  • 2017-01-21 21:35:08
  • 749
收藏助手
不良信息举报
您举报文章:C#如何取硬件标志
举报原因:
原因补充:

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