using System;
using System. Runtime.interopservices;
using System. Management;
namespace Hardware
{
///<summary>
Summary description of the
///Hardware_mac.
///</summary>
public class Hardwareinfo
{
//Take machine name
public String gethostname ()
{
return System.Net.Dns.GetHostName ();
}
//Fetch 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
{
return "";
}
}//end Method
//Take the first hard drive 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 ' 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 ');
}
}
}