C # How to take hardware sign

xiaoxiao2021-03-06  109

Using system;

Using system.Runtime.InteropServices;

Using system.management;

Namespace Hardware

{

///

/// Hardware_mac abstract description.

///

Public Class HardwareInfo

{

// Take the machine name

Public string gethostname ()

{

Return system.net.dns.gethostname ();

}

// Take the CPU number

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

{

""; "

}

} // End Method

// Take the first hard disk number

Public String getharddiskid ()

{

Try

{

ManagementObjectSearcher Searcher = New ManagementObjectSearcher ("Select * from win32_physicalmedia);

String strharddiskid = null;

Foreach (ManagementObject Mo in search.get ())

{

StrhardDiskid = Mo ["serialnumber"]. TOSTRING (). TRIM ();

Break;

}

Return Strharddiskid;

}

Catch

{

""; "

}

} // end

Public Enum NCBCONST

{

Ncbnamsz = 16, / * Absolute Length of a Net Name * /

Max_lana = 254, / * lana's in inn 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_timeout;

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 (lant_enum));

Marshal.Freehglobal (ncb.ncb_buffer);

IF (uretcode! = (short) ncbconst.nrc_goodret

""; "

For (int i = 0; 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

""; "

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');

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-99433.html

New Post(0)