C# ProcessorID

索引サイト

【note】System.Management は参照設定が必要




using System.Runtime.InteropServices;
using System.Management;

namespace WpfApplication1
{
///


/// MainWindow.xaml の相互作用ロジック
///


public partial class MainWindow : Window
{
[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFOMATION ptmpsi);
[StructLayout(LayoutKind.Sequential)]

public struct SYSTEM_INFOMATION
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
SYSTEM_INFOMATION w_sysInf = new SYSTEM_INFOMATION();
GetSystemInfo(ref w_sysInf);

dwOemId.Text = w_sysInf.dwOemId.ToString();
dwPageSize.Text = w_sysInf.dwPageSize.ToString();
lpMinimumApplicationAddress.Text = w_sysInf.lpMinimumApplicationAddress.ToString();
lpMaximumApplicationAddress.Text = w_sysInf.lpMaximumApplicationAddress.ToString();
dwActiveProcessorMask.Text = w_sysInf.dwActiveProcessorMask.ToString();
dwNumberOfProcessors.Text = w_sysInf.dwNumberOfProcessors.ToString();
dwProcessorType.Text = w_sysInf.dwProcessorType.ToString();
dwAllocationGranularity.Text = w_sysInf.dwAllocationGranularity.ToString();
dwProcessorLevel.Text = w_sysInf.dwProcessorLevel.ToString();
dwProcessorRevision.Text = w_sysInf.dwProcessorRevision.ToString();

string w_CpuID = GetCpuID();
Processorid.Text = w_CpuID;
}
private string GetCpuID()
{
ManagementObjectSearcher w_Searcher = null;
ManagementObjectCollection w_Collection = null;
string p_CpuId = null;    
try
{
w_Searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
w_Collection = w_Searcher.Get();

foreach (ManagementObject w_Object in w_Collection)
{
if (w_Object["ProcessorId"] != null)
{
p_CpuId = w_Object["ProcessorId"].ToString();
}
}
}
catch (Exception errorNo)
{
MessageBox.Show(errorNo.Message, "Object Search Error");
}
w_Searcher.Dispose();
return p_CpuId;
}
}
}



AX