Getting CPU ID on virtual machine

heresma picture heresma · Feb 24, 2011 · Viewed 9.5k times · Source

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?

Answer

SwDevMan81 picture SwDevMan81 · Feb 24, 2011

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