Make a calculator -- get hardware information

This article will tell you how to get the CPU serial number, motherboard serial number and BIOS number.

Zero. How to obtain hardware information

Yes NET, you must use system Yes, it is NET framework, from which all hardware information is obtained. The method of obtaining hardware information is as follows:

  1. Reference system Management ;
  2. Instantiate the ManagenmentClass class and pass in the WMI class to bind. This class represents a CIM management class in WMI. CIM class represents management information including hardware, software, process, etc;
  3. Get WMI information collection;
  4. Search the corresponding instance and traverse the collection to obtain the specified content;
  5. Get the specified content and jump out of the loop.

1, Code implementation

Let's implement the content described in the previous section in the form of code.
Create the HardwareInformation class under the Method folder

using System;
using System.Collections.Generic;
using System.Linq;
//Reference system Management
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator.Method
{
    public class HardwareInformation
    {
        public static string Get(string path)
        {
            try
            {
                string propertyName = "";
                switch (path)
                {
                    //cpu serial number
                    case "Win32_Processor":
                        propertyName = "ProcessorId";
                        break;
                    //Motherboard serial number
                    case "Win32_BaseBoard":
                    //BIOS number
                    case "Win32_BIOS":
                    //Hard disk serial number
                    case "Win32_PhysiclMedia":
                    case "Win32_DiskDrive":
                        propertyName = "SerialNumber";
                        break;
                    default:
                        break;
                }
                //Instantiate the ManagenmentClass class and pass in the WMI class to bind
                ManagementClass mc = new ManagementClass(path);
                //Get WMI information collection
                ManagementObjectCollection moc = mc.GetInstances();
                string hi = null;
                //Search the corresponding instance and traverse the collection to obtain the specified content
                foreach (ManagementObject mo in moc)
                {
                    //Get the specified content and jump out of the loop
                    hi = mo.Properties[propertyName].Value.ToString();
                    break;
                }
                return hi.Trim();
            }
            catch(Exception e)
            {
                MessageBox.Show($"Error: Failed to get hardware information,{e.Message}");
                return "";
            }
        }
    }
}

In the code, we see that when obtaining the hard disk serial number, Win32_PhysicalMedia and Win32_ DiskDrives are available, so what's the difference between them? Win32_DiskDrive returns the hard disk drive information, which is the physical information of the disk, such as cluster size, total size, block size, PNPID, etc
Win32_PhysicalMedia returns the physical media information on the current computer. Because it returns the "physical media" information, it has high universality and the returned information is more abstract. In actual development, Win32 is recommended_ Physiclmedia to get hardware information

After writing the above code, we write the following code in the Load method of the Registered form:

private void Registered_Load(object sender, EventArgs e)
        {
            //Get hardware information
            string cpu = HardwareInformation.Get("Win32_Processor");
            string baseBoard = HardwareInformation.Get("Win32_BaseBoard");
            string bois = HardwareInformation.Get("Win32_BIOS");
            string diskDrive = HardwareInformation.Get("Win32_PhysiclMedia");
            //Generate machine code
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string inf = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(cpu  + baseBoard + bois + diskDrive)), 4, 8);
            MachineCode_TextBox.Text = inf;
        }

Run the code, and the registration form will display the generated machine code

2, Summary

This article explains the acquisition methods and ideas of CPU serial number, motherboard serial number and BIOS serial number, and realizes them through code.

Tip: because this topic is written for personnel with development experience, and this topic mainly explains software anti cracking, this article will not explain in detail the acquisition of hardware related code.

Tip: download the code in this section, https://gitee.com/miaoshu_studio/calculator.git Select the HardwareInformation branch when pulling

Added by Ash3r on Fri, 14 Jan 2022 07:30:47 +0200