4

I need to get any info to identify a machine and what I was using so far was the following:

    internal static string GetProcessorId()
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            using (System.Management.ManagementClass theClass = new System.Management.ManagementClass("Win32_Processor"))
            {
                using (System.Management.ManagementObjectCollection theCollectionOfResults = theClass.GetInstances())
                {
                    foreach (System.Management.ManagementObject currentResult in theCollectionOfResults)
                    {
                        sb.Append(currentResult["ProcessorID"].ToString());
                    }
                }
            }
            return sb.ToString();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return "";
        }
    }

But when I run this on Windows XP (from a Virtual Machine), my currentResult["ProcessorID"] is null. I'm not sure if it is because I'm using a virtual machine, but it might be because is XP. Anyway, I'm not very trustful of this code. Is it safe enough? Should I get a computer Id some other way? This is all part of a licensing system I'm developing.

Juan
  • 15,274
  • 23
  • 105
  • 187
  • 1
    Possible duplicate: http://stackoverflow.com/questions/1101772/win32-processoris-processorid-unique-for-all-computers I know for a fact that "ProcessorID" is not supported on VMWare. – Cody Gray - on strike Jan 03 '11 at 07:24
  • @Cody Gray: Do you mean VMWare doesn't emulate the intel CPUID instruction? Or do you mean the hard coded ID that each intel processor supposedly has (that can be turned off in the BIOS...) – JimR Jan 03 '11 at 07:28
  • @JimR: Yes. I mean that as the question describes, the value returned will be `null`. See [this thread](http://forums.virtualbox.org/viewtopic.php?f=2&t=26178) on their forums for more information. – Cody Gray - on strike Jan 03 '11 at 07:33
  • @Cody Gray: I think you mean Virtual Box, not VMWare. Your link supports my thoughts as well. I remember having licensing problems way in the past with some retarded software before VMWare supported the Processor ID... That software works now so that was why I asked. – JimR Jan 03 '11 at 07:37
  • @JimR: You're correct. I meant Virtual Box, rather than VMWare. There are too many virtualization utilities with similar names... Either way, the point is it doesn't always work. – Cody Gray - on strike Jan 03 '11 at 07:40
  • My is my question marked with close? – Juan Jan 03 '11 at 23:27

2 Answers2

8

For a small tool I wrote (Windows Forms .NET 2.0), I needed something similar.

I wrote a helper class that simply uses some common information like HDD serial number through PInvoke of the GetVolumeInformation function.

This is by no way really safe or bullet-proof, but accurate enough to fit my needs.

(If you are interested this is the tool, if I'm allowed to link to)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Out of pure curiosity, why does free software need a way to uniquely identify a computer? I'm not sure I like this type of thing to enforce licensing agreements, but I don't even see the point of it for the software you linked to. – Cody Gray - on strike Jan 03 '11 at 07:27
  • 6
    Good question :-) It is a tool to upload files (like YouSendIt). Initially a user had to log in with his user name and password. Some reviewers disliked this. So I introduced the hardware ID stuff and enabled anonymous usage of the tool. This enables a user to any time in the future switch his history from anonymous to a registered user, if he wants to. Bottom line: Its a mechanism to generate a unique key in the upload history database on the server. – Uwe Keim Jan 03 '11 at 07:30
  • 1
    Thanks for the honest explanation. That makes sense. – Cody Gray - on strike Jan 03 '11 at 07:46
  • Doesn't `GetVolumeInformation` return the _volume_ serial number, rather than the _drive_ serial number? My understanding is that the volume serial number [is pretty easy to change](http://www.wintips.org/how-to-change-hard-disks-volume-serial-number-volume-id/). – reirab Dec 17 '15 at 22:54
3

Even if it worked, it wouldn't do what you want. The ProcessorID gives you an identification of the process product (Pentium X with features Y and Z), not about the specific instance of the CPU. It is described thus:

Processor information that describes the processor features. For an x86 class CPU, the field format depends on the processor support of the CPUID instruction. If the instruction is supported, the property contains 2 (two) DWORD formatted values. The first is an offset of 08h-0Bh, which is the EAX value that a CPUID instruction returns with input EAX set to 1. The second is an offset of 0Ch-0Fh, which is the EDX value that the instruction returns. Only the first two bytes of the property are significant and contain the contents of the DX register at CPU reset—all others are set to 0 (zero), and the contents are in DWORD format.

The IBM PC hardware doesn't have any kind of fake-proof hardware identification.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235