C # obtain the machine code

Source: Internet
Author: User

Using system. runtime. interopservices;
Using system. Management;
Using system;

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;
}< br> return strcpuid;
}< br> 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's in range 0 to max_lana volume 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'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;
[managed Alas (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;
}< br> public class WIN32API
... {
[dllimport ("netapi32.dll")]
Public static extern char NetBIOS (ref NCB);
}

// Obtain the MAC of the NIC
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 )'*';
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 ('', '0 ');
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.