0

I want to get id processor in .NET with WMI but when I'm using the get() method from the ManagementObjectSearcher, I'm getting an out of memory exception ...

If you want to take a look from the code see below :

   ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "select * from Win32_Processor");

            foreach (ManagementObject share in searcher.Get())
                foreach (PropertyData PC in share.Properties)
                    if (PC.Name.Equals("ProcessorId"))
                        return (string)PC.Value;

            return null;

This code works on others computers but not on mine ...

I'm using windows 7.

What is the problem ?

I tried to restart WMI service and that not resolve my problem :(

Tata2
  • 893
  • 2
  • 8
  • 14

1 Answers1

2

There are several reasons which could cause out of memory exception.

  1. possible memory leak in WMI, source: http://brooke.blogs.sqlsentry.net/2010/02/win32service-memory-leak.html
  2. check whether you have permission(s), that would explain why does your code work on some computers and why doesn't on yours.
  3. run your code as Administrator (for debugging start VS as Administrator)
  4. Here is an other code snippet, try this one as well... who knows

Sample:

public static String GetCPUId()
{
    String processorID = "";

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(
        "Select * FROM WIN32_Processor");

    ManagementObjectCollection mObject = searcher.Get();

    foreach (ManagementObject obj in mObject)
    {
        processorID = obj["ProcessorId"].ToString();
    }

    return processorID;
}

Source: WIN32_Processor::Is ProcessorId Unique for all computers

Community
  • 1
  • 1
Andras Sebo
  • 1,120
  • 8
  • 19
  • Big thanks to you ! I tried to launch it in administrator and it works fine :) But on one another computer, I have tried to launch it on an user account without administrator role and it works too ... Why on mine, it doesn't work without administrator permissions ? – Tata2 Jun 24 '13 at 12:14
  • Check whether UAC (http://en.wikipedia.org/wiki/User_Account_Control) is 'ON' on your computer. – Andras Sebo Jun 24 '13 at 13:08
  • I've encountered this problem before, and it's down to user permissions. There's some account permissioning that you have to do in order to get certain parts of WMI to work correctly with a user account. – Sean Feb 02 '14 at 15:15