Using System;
Using System. Runtime. InteropServices;
Using System. Management;
Namespace Hardware
{
/// <Summary>
/// Summary of Hardware_Mac.
/// </Summary>
Public class HardwareInfo
{
// Obtain the machine name
Public string GetHostName ()
{
Return System. Net. Dns. GetHostName ();
}
// Obtain the CPU ID
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
// Obtain the first hard disk number
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 & acute; s in range 0 to MAX_LANA random sive */
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 & acute; s BUFFER */
}
[StructLayout (LayoutKind. Sequential)]
Public struct ADAPTER_STATUS
{
[Financialas (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
{
[Financialas (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;
[Financialas (UnmanagedType. ByValArray, SizeConst = (int) NCBCONST. NCBNAMSZ)]
Public byte [] ncb_callname;
[Financialas (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_rj_cplt;
[Financialas (UnmanagedType. ByValArray, SizeConst = 10)]
Public byte [] ncb_reserve;
Public IntPtr ncb_event;
}
[StructLayout (LayoutKind. Sequential)]
Public struct LANA_ENUM
{
Public byte length;
[Financialas (UnmanagedType. ByValArray, SizeConst = (int) NCBCONST. MAX_LANA)]
Public byte [] lana;
}
[StructLayout (LayoutKind. Auto)]
Public struct ASTAT
{
Public ADAPTER_STATUS adapt;
[Financialas (UnmanagedType. ByValArray, SizeConst = (int) NCBCONST. NUM_NAMEBUF)]
Public NAME_BUFFER [] NameBuff;
}
Public class Win32API
{
[DllImport ("NETAPI32.DLL")]
Public static extern char NetBIOS (ref NCB );
}
Public String getmacaddress ()
{
String ADDR = "";
Try
{
Int CB;
Astat adapter;
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) & acute; * & acute ;;
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 ("{: 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 (& acute;, & acute; 0 & acute ;);
}
}
}