2

I am trying to use this code:

public string GetCPUId()
{
    string cpuInfo = String.Empty;
    string temp = String.Empty;
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc)
    {
        if (cpuInfo == String.Empty)
        {
            cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
        }
    }
    return cpuInfo;
}

To get a hw uid on a XP virtual machine (virtualbox), but I am only getting a messagebox that says:

Object reference not set to an instance of an object.

Is it because it's a virtual machine or what?

heresma
  • 273
  • 1
  • 4
  • 14

3 Answers3

7

Yes, it is because you are running a virtual machine. mo.Properties["ProcessorId"] will return null. See the answers here.

Community
  • 1
  • 1
SwDevMan81
  • 47,539
  • 21
  • 146
  • 180
  • Note that the (currently) accepted answer on that question suggests a method that, as far as I know, would give you the _volume_ serial number (which is easily changed,) not the _drive_ serial number (which is not easily changed.) – reirab Dec 17 '15 at 22:57
3

I've just found a faster solution here : http://www.dotnetspark.com/kb/24-get-processor-id-using-c-sharp.aspx

it works faster than yours.and IT WORKS IN MY VIRTUAL WINDOWS(using VMware Workstation 7.0.0 with WINDOWS XP installed virtually) as both codes use the same library yours should work as well! try including dll file in project output it MIGHT Help.

Omid S.
  • 701
  • 7
  • 15
0

That should work just fine on a VM. The CPU ID presented by the virtual CPU may or may not match the physical CPU, though.

nobody
  • 19,421
  • 17
  • 55
  • 76