• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Return a unique HWID string with WMI

Hyperion

Founder
Threw this together this morning for a quick example of pulling some hardware serials via WMI
and creating a unique HWID string to use for a login system or just getting users to
generate their unique ID for you to use in a database. Thanks to Hammer for the
Substring hint. The last 10 characters of each serial is used.

For the RAM, I used the part number ID which is also unique. Only because
for some reason I only get a null value from my SerialNumber (0x0000000).

String format is: ram-cpu-motherboard-harddrive

A screenie of it in action:
xI4IdLT.png


Used References:
Code:
System
System.Management

Here is the code showing each ID return separately:
Code:
public class HWID
{
    static string hwid_str;
    static string ram;
    static string ramid;
    static string cpu;
    static string mobo;
    static string moboid;
    static string cpuid;
    static string hdd = null;
    static string hddid;
    static string hddstr;

    static void Main(string[] args)
    {
        ramID();
        cpuID();
        moboID();
        hddID();
        hwid_str = ramid + "-" + cpuid + "-" + moboid + "-" + hddstr;
        Console.WriteLine("Unique HWID: " + hwid_str);
        Console.ReadKey();
    }

    static void cpuID()
    {
        ManagementObjectCollection mbsList = null;
        ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
        mbsList = mbs.Get();

        foreach (ManagementObject mo in mbsList)
        {
            cpu = mo["ProcessorID"].ToString();
            cpuid = cpu.Substring(cpu.Length - 10);
        }
    }

    static void moboID()
    {
        ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
        ManagementObjectCollection moc = mos.Get();

        foreach (ManagementObject mo in moc)
        {
            mobo = (string)mo["SerialNumber"];
            moboid = mobo.Substring(mobo.Length - 10);
        }
    }

    static string hddID()
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

        foreach (ManagementObject wmi_HD in searcher.Get())
        {
            if (wmi_HD["SerialNumber"] != null)
                hddid = (string)wmi_HD["SerialNumber"].ToString();

            hddstr = hddid.Substring(hddid.Length - 14);
                
        }
        return string.Empty;
    }

    static void ramID()
    {
        ManagementObjectSearcher mor = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
        ManagementObjectCollection mok = mor.Get();

        foreach (ManagementObject morm in mok)
        {
            ram = (string)morm["PartNumber"];
            ramid = ram.Substring(ram.Length - 10);
        }
    }
}

Enjoy
 

Parranoia

Insane Member
I don't have much knowledge in this sector, but something like getting the id of the RAM, where you can have multiple sticks of it, how does this work? Does it just pick the last one it finds or what? Same concept can go for processors and storage drives. The topic just made me curious :)
 

Hyperion

Founder
I don't have much knowledge in this sector, but something like getting the id of the RAM, where you can have multiple sticks of it, how does this work? Does it just pick the last one it finds or what? Same concept can go for processors and storage drives. The topic just made me curious :)

Well, far as the serial HWID goes, it's the same as the part number. If you have multiple sticks and not the same brand, it's going to select the first ram slot.
I only used the PartNumber in this example because I couldn't figure out why my SerialNumber was coming up as 0x000000. The HDD is different because of the types
like local, physical and what not. I would like to design a better login system than what I had released in the other thread, will get to it one day...
 

Hamar

BETA Tester
I don't have much knowledge in this sector, but something like getting the id of the RAM, where you can have multiple sticks of it, how does this work? Does it just pick the last one it finds or what? Same concept can go for processors and storage drives. The topic just made me curious :)

in this snippet it'll pick the last one, however you could just setup an array of strings or list/dictionary to store all the result id's for instance:

Code:
        static Dictionary<string, string> example_ram = new Dictionary<string, string>(); 

        static void ramID()
        {
            ManagementObjectSearcher mor = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
            ManagementObjectCollection mok = mor.Get();

            foreach (ManagementObject morm in mok)
            {
                string ram = (string)morm["PartNumber"];
                string ramid = ram.Substring(ram.Length - 10);

                example_ram.Add(ram, ramid);
            }
        }

        static string GenerateUniqueRamKey()
        {
            string result = string.Empty;

            foreach (var item in example_ram)
            {
                result += item.Key + item.Value;
            }

            return result;
        }
 

Hyperion

Founder
in this snippet it'll pick the last one, however you could just setup an array of strings or list/dictionary to store all the result id's for instance:

Code:
        static Dictionary<string, string> example_ram = new Dictionary<string, string>(); 

        static void ramID()
        {
            ManagementObjectSearcher mor = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
            ManagementObjectCollection mok = mor.Get();

            foreach (ManagementObject morm in mok)
            {
                string ram = (string)morm["PartNumber"];
                string ramid = ram.Substring(ram.Length - 10);

                example_ram.Add(ram, ramid);
            }
        }

        static string GenerateUniqueRamKey()
        {
            string result = string.Empty;

            foreach (var item in example_ram)
            {
                result += item.Key + item.Value;
            }

            return result;
        }

That's just going to return every slot's part number, kinda defeats the purpose of an HWID string
 

Parranoia

Insane Member
in this snippet it'll pick the last one, however you could just setup an array of strings or list/dictionary to store all the result id's for instance:

Code:
        static Dictionary<string, string> example_ram = new Dictionary<string, string>(); 

        static void ramID()
        {
            ManagementObjectSearcher mor = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
            ManagementObjectCollection mok = mor.Get();

            foreach (ManagementObject morm in mok)
            {
                string ram = (string)morm["PartNumber"];
                string ramid = ram.Substring(ram.Length - 10);

                example_ram.Add(ram, ramid);
            }
        }

        static string GenerateUniqueRamKey()
        {
            string result = string.Empty;

            foreach (var item in example_ram)
            {
                result += item.Key + item.Value;
            }

            return result;
        }

Thanks. That makes more sense now.
 
Top