C#如何取硬件标志
2024-07-21 02:18:35
供稿:网友
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');
}
}
}