Recently, I helped people develop something about software registration. The other party must prevent a large number of copies and prevent general use of registration codes. So I thought of using hardware information of machines to generate registration codes, so it is involved in obtaining machine hardware information...
In the. NET environment (described in VC #), you need to use a class library (system. Management. dll) to obtain the hardware information of the machine. In Solution Explorer, add system. Management.
We canProgramCompile it into a. dll file for future calls;
In the programCodeUsing system. Management;
The Code is as follows:
1. Obtain the machine Name:
Public String gethostname ()
{
Return System. net. DNS. gethostname ();
}
2. Obtain the CPU ID
Public String getcpuid ()
{
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;
}
3. Obtain the hard drive number
Public String getmainharddiskid ()
{
String STR = "";
Managementclass mchd = new managementclass ("win32_logicaldisk ");
Managementobjectcollection mochd = mchd. getinstances ();
Foreach (managementobject m in mochd)
{
If (M ["DeviceID"]. tostring () = "C :")
{
STR = m ["volumeserialnumber"]. tostring ();
Break;
}
}
Return STR;
}
4. Obtain the bios and MAC address. This is complicated and requires netapi32.dll.
In addition:
5. Obtain the MAC address of the NIC
Using system. Management;
Public String getnetworkadapterid ()
{
String STR = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
// Console. writeline ("MAC address \ t {0}", Mo ["macaddress"]. tostring ());
STR = Mo ["macaddress"]. tostring ();
Mo. Dispose ();
}
}
Return STR;
}
---------------------------------------------
If the system prompts that the namespace management cannot be found,
1. In Solution Explorer, right-click references and choose add reference...
2. The add reference dialog box is displayed, and the. NET tab is selected.
3. Select system. Management and click Select.
4. Click OK ".