C # The method of acquiring machine code (machine name, CPU number, hard disk number, Nic Mac, etc.) _c# tutorial

Source: Internet
Author: User

The example in this article describes how C # obtains machine code. Share to everyone for your reference, specific as follows:

Using System.Runtime.InteropServices;
Using System.Management;
Using System;
  public class Hardwareinfo {//Fetch 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 = n
      EW 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 */NCBR
  ESET =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, Sizeco
    NST=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), Sizecon st= (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; The public class Win32API {[DllImport ("NETAPI32.
  DLL ")] public static extern char Netbios (ref NCB NCB);
    ///Fetch NIC Mac 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.a DAPTER_ADDRESS[1], adapter.adapt.adapter_address[2], adapter.adapt.adapter_address[3], adapt
        ER.ADAPT.ADAPTER_ADDRESS[4], adapter.adapt.adapter_address[5]); } ' catch {} return addr.
  Replace (', ' 0 ');

 }
}

Read more about C # Interested readers can view the site topics: "C # form Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Usage Skills summary", "C # Operation Excel Skills Summary", "C # Summary of operational skills in XML files, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming

I hope this article will help you with C # programming.

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.