如何在C# WPF .NET Framework 4.8中检测E-core与P-core?
混合处理器核心信息检测的纯C#实现方案(无C++ DLL依赖)
针对你的C# WPF .NET Framework 4.8项目需求,放弃C++ DLL封装方案,直接调用Windows原生API就能解决杀毒误报和便携性问题,以下是完整实现:
一、核心实现代码
通过GetLogicalProcessorInformationEx API直接获取处理器拓扑、核心类型(P/E核)及超线程状态,完全用C#实现:
using System; using System.Runtime.InteropServices; using System.Collections.Generic; public enum LOGICAL_PROCESSOR_RELATIONSHIP { RelationProcessorCore, RelationNumaNode, RelationCache, RelationProcessorPackage, RelationGroup, RelationAll = 0xffff } [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX { public LOGICAL_PROCESSOR_RELATIONSHIP Relationship; public uint Size; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)] public byte[] Data; } [StructLayout(LayoutKind.Sequential)] public struct PROCESSOR_GROUP_INFO { public byte MaximumProcessorCount; public byte ActiveProcessorCount; public ushort Reserved; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public uint[] ActiveProcessorMask; } [StructLayout(LayoutKind.Sequential)] public struct PROCESSOR_CORE_INFO { public uint Flags; public PROCESSOR_GROUP_INFO GroupCount; } public static class ProcessorInfoHelper { [DllImport("kernel32.dll", SetLastError = true)] private static extern bool GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP Relationship, IntPtr Buffer, ref uint ReturnLength); private const uint LTP_PC_SMT = 0x00000001; private const uint LTP_PC_CORE_TYPE = 0x00000002; private const uint LTP_PC_CORE_TYPE_PERFORMANCE = 0x00000000; private const uint LTP_PC_CORE_TYPE_EFFICIENCY = 0x00000001; private const int ERROR_INSUFFICIENT_BUFFER = 122; public static List<CoreInfo> GetAllCoreDetails() { var coreDetails = new List<CoreInfo>(); uint bufferSize = 0; // 获取所需缓冲区大小 GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore, IntPtr.Zero, ref bufferSize); if (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) return coreDetails; IntPtr buffer = Marshal.AllocHGlobal((int)bufferSize); try { if (GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore, buffer, ref bufferSize)) { IntPtr currentPtr = buffer; while (currentPtr < buffer + bufferSize) { var baseInfo = Marshal.PtrToStructure<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(currentPtr); if (baseInfo.Relation != LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore) { currentPtr += baseInfo.Size; continue; } var coreInfo = Marshal.PtrToStructure<PROCESSOR_CORE_INFO>(currentPtr + 8); // 跳过Relationship和Size字段 bool isHyperthreaded = (coreInfo.Flags & LTP_PC_SMT) != 0; bool isPCore = true; // 判断核心类型(P/E核) if ((coreInfo.Flags & LTP_PC_CORE_TYPE) != 0) { uint coreType = (coreInfo.Flags >> 8) & 0xFF; isPCore = coreType == LTP_PC_CORE_TYPE_PERFORMANCE; } // 解析核心编号 int coreId = 0; for (int i = 0; i < coreInfo.GroupCount.ActiveProcessorCount; i++) { uint mask = coreInfo.GroupCount.ActiveProcessorMask[i]; while (mask != 0) { if ((mask & 1) != 0) { coreDetails.Add(new CoreInfo { CoreId = coreId, IsPCore = isPCore, IsHyperthreaded = isHyperthreaded }); } mask >>= 1; coreId++; } } currentPtr += baseInfo.Size; } } } finally { Marshal.FreeHGlobal(buffer); } return coreDetails; } } public class CoreInfo { public int CoreId { get; set; } public bool IsPCore { get; set; } public bool IsHyperthreaded { get; set; } }
二、调用示例(WPF中使用)
// 在ViewModel或代码后台中调用 var coreList = ProcessorInfoHelper.GetAllCoreDetails(); foreach (var core in coreList) { // 输出或绑定到UI Console.WriteLine($"Core ID: {core.CoreId} | P-Core: {core.IsPCore} | 超线程: {core.IsHyperthreaded}"); }
三、方案优势
- 无第三方依赖:仅调用系统自带的
kernel32.dll,彻底避免杀毒软件误报 - 便携性完美:纯C#代码直接编译进EXE,无需额外DLL文件
- 兼容性强:支持Windows 7 SP1及以上系统,完全适配.NET Framework 4.8
四、注意点
- 旧版Windows(如Windows 7)不支持核心类型检测,此时
IsPCore默认返回true,可通过Environment.OSVersion做版本兼容判断 - 核心编号基于处理器掩码连续计数,与系统设备管理器中的编号保持一致
内容的提问来源于stack exchange,提问作者iosiph
相关产品推荐
相关产品推荐

