private static List <ProcessorInfo> GetProcessorInfo64() { // First we're going to execute GetLogicalProcessorInformation // once to make sure that we determine the size of the data // that it is going to return. // This call should fail with error ERROR_INSUFFICIENT_BUFFER. uint iReturnLength = 0; SystemLogicalProcessorInformatioNx64[] oDummy = null; bool bResult = GetLogicalProcessorInformation(oDummy, ref iReturnLength); if (bResult) { throw Fail("GetLogicalProcessorInformation failed.", "x64"); } // Making sure that the error code that we got back is not // that there is in sufficient space in the buffer. int iError = Marshal.GetLastWin32Error(); if (iError != ErrorInsufficientBuffer) { throw Fail( "Insufficient space in the buffer.", "x64", iError.ToString()); } // Now that we know how much space we should reserve, // we're going to reserve it and call // GetLogicalProcessorInformation again. var iBaseSize = (uint)Marshal.SizeOf( typeof(SystemLogicalProcessorInformatioNx64)); uint iNumberOfElements = iReturnLength / iBaseSize; var oData = new SystemLogicalProcessorInformatioNx64[iNumberOfElements]; uint iAllocatedSize = iNumberOfElements * iBaseSize; if (!GetLogicalProcessorInformation(oData, ref iAllocatedSize)) { throw Fail("GetLogicalProcessorInformation failed", "x64", Marshal.GetLastWin32Error().ToString(CultureInfo.InvariantCulture)); } // Converting the data to a list that we can easily interpret. return(oData.Select(oInfo => new ProcessorInfo(oInfo.Relationship, oInfo.Flags, oInfo.ProcessorMask)).ToList()); }
private static List<ProcessorInfo> GetProcessorInfo64() { // First we're going to execute GetLogicalProcessorInformation // once to make sure that we determine the size of the data // that it is going to return. // This call should fail with error ERROR_INSUFFICIENT_BUFFER. uint iReturnLength = 0; SystemLogicalProcessorInformatioNx64[] oDummy = null; bool bResult = GetLogicalProcessorInformation(oDummy, ref iReturnLength); if (bResult) { throw Fail("GetLogicalProcessorInformation failed.", "x64"); } // Making sure that the error code that we got back is not // that there is in sufficient space in the buffer. int iError = Marshal.GetLastWin32Error(); if (iError != ErrorInsufficientBuffer) { throw Fail( "Insufficient space in the buffer.", "x64", iError.ToString()); } // Now that we know how much space we should reserve, // we're going to reserve it and call // GetLogicalProcessorInformation again. uint iBaseSize = (uint)Marshal.SizeOf( typeof(SystemLogicalProcessorInformatioNx64)); uint iNumberOfElements = iReturnLength / iBaseSize; SystemLogicalProcessorInformatioNx64[] oData = new SystemLogicalProcessorInformatioNx64[iNumberOfElements]; uint iAllocatedSize = iNumberOfElements * iBaseSize; if (!GetLogicalProcessorInformation(oData, ref iAllocatedSize)) { throw Fail("GetLogicalProcessorInformation failed", "x64", Marshal.GetLastWin32Error().ToString()); } // Converting the data to a list that we can easily interpret. List<ProcessorInfo> oList = new List<ProcessorInfo>(); foreach (SystemLogicalProcessorInformatioNx64 oInfo in oData) { oList.Add(new ProcessorInfo( oInfo.Relationship, oInfo.Flags, oInfo.ProcessorMask)); } return oList; }