diff --git a/Mixaill.HwInfo.SetupApi/DeviceInfo.cs b/Mixaill.HwInfo.SetupApi/DeviceInfo.cs index f31d4fd..c9b024d 100644 --- a/Mixaill.HwInfo.SetupApi/DeviceInfo.cs +++ b/Mixaill.HwInfo.SetupApi/DeviceInfo.cs @@ -45,13 +45,24 @@ internal DeviceInfo(DeviceInfoSet devInfoSet, SP_DEVINFO_DATA devInfo) private List getMemoryResources(SP_DEVINFO_DATA devInfo) { var result = new List(); - - foreach (var res in getResources(devInfo)) + + var memoryRes = getResources(devInfo, Constants.ALLOC_LOG_CONF); + if(memoryRes.Count == 0) + { + memoryRes = getResources(devInfo, Constants.BOOT_LOG_CONF); + } + + var largeMemoryRes = getResources(devInfo, Constants.ALLOC_LOG_CONF); + if (largeMemoryRes.Count == 0) + { + largeMemoryRes = getResources(devInfo, Constants.BOOT_LOG_CONF); + } + + foreach (var res in memoryRes) { result.Add(new DeviceResourceMemory(DeviceResourceType.Mem, res.MEM_Header.MD_Alloc_Base, res.MEM_Header.MD_Alloc_End)); } - - foreach (var res in getResources(devInfo)) + foreach (var res in largeMemoryRes) { result.Add(new DeviceResourceMemory(DeviceResourceType.MemLarge, res.MEM_LARGE_Header.MLD_Alloc_Base, res.MEM_LARGE_Header.MLD_Alloc_End)); } @@ -59,55 +70,65 @@ private List getMemoryResources(SP_DEVINFO_DATA devInfo) return result; } - private unsafe List getResources(SP_DEVINFO_DATA devInfo) where T: struct + private unsafe List getResources(SP_DEVINFO_DATA devInfo, uint logConfFlags) where T: struct { - var result = new List(); + var resources = new List(); UIntPtr logicalConfiguration; UIntPtr resourceDescriptor; var resourceType = typeof(T) == typeof(MEM_RESOURCE) ? DeviceResourceType.Mem : DeviceResourceType.MemLarge; - if (PInvoke.CM_Get_First_Log_Conf(&logicalConfiguration, devInfo.DevInst, Constants.ALLOC_LOG_CONF) != CONFIGRET.CR_SUCCESS) - { - throw new Win32Exception("Failed to get first logical configuration"); - } - - var cmResult = PInvoke.CM_Get_Next_Res_Des(out resourceDescriptor, logicalConfiguration, (uint)resourceType, null, 0); - while (resourceDescriptor != null) + var result = PInvoke.CM_Get_First_Log_Conf(&logicalConfiguration, devInfo.DevInst, logConfFlags); + if (result == CONFIGRET.CR_SUCCESS) { - uint dataSize = 0; - if(PInvoke.CM_Get_Res_Des_Data_Size(out dataSize, resourceDescriptor, 0) != CONFIGRET.CR_SUCCESS) - { - break; - } - - - T res; - fixed (void* p = new byte[dataSize]) + var cmResult = PInvoke.CM_Get_Next_Res_Des(out resourceDescriptor, logicalConfiguration, (uint)resourceType, null, 0); + while (resourceDescriptor != null) { - if (PInvoke.CM_Get_Res_Des_Data(resourceDescriptor, p, dataSize, 0) != CONFIGRET.CR_SUCCESS) + uint dataSize = 0; + if (PInvoke.CM_Get_Res_Des_Data_Size(out dataSize, resourceDescriptor, 0) != CONFIGRET.CR_SUCCESS) { break; } - res = (T)Marshal.PtrToStructure((IntPtr)p, typeof(T)); - } - result.Add(res); + T res; + fixed (void* p = new byte[dataSize]) + { + if (PInvoke.CM_Get_Res_Des_Data(resourceDescriptor, p, dataSize, 0) != CONFIGRET.CR_SUCCESS) + { + break; + } - UIntPtr nextResourceDescriptor; - if(PInvoke.CM_Get_Next_Res_Des(out nextResourceDescriptor, resourceDescriptor, (uint)resourceType, null, 0) != CONFIGRET.CR_SUCCESS) - { - break; + res = (T)Marshal.PtrToStructure((IntPtr)p, typeof(T)); + } + resources.Add(res); + + UIntPtr nextResourceDescriptor; + result = PInvoke.CM_Get_Next_Res_Des(out nextResourceDescriptor, resourceDescriptor, (uint)resourceType, null, 0); + if (result == CONFIGRET.CR_SUCCESS) + { + PInvoke.CM_Free_Res_Des_Handle(resourceDescriptor); + resourceDescriptor = nextResourceDescriptor; + + } + else if (result == CONFIGRET.CR_NO_MORE_RES_DES) + { + break; + } + else + { + throw new Win32Exception("failed to get resource descriptor"); + } } PInvoke.CM_Free_Res_Des_Handle(resourceDescriptor); - resourceDescriptor = nextResourceDescriptor; + PInvoke.CM_Free_Log_Conf(logicalConfiguration, 0); + } + else if (result != CONFIGRET.CR_NO_MORE_LOG_CONF) + { + throw new Win32Exception("failed to get logical configuration"); } - PInvoke.CM_Free_Res_Des_Handle(resourceDescriptor); - PInvoke.CM_Free_Log_Conf(logicalConfiguration, 0); - - return result; + return resources; } } diff --git a/Mixaill.HwInfo.SetupApi/Mixaill.HwInfo.SetupApi.csproj b/Mixaill.HwInfo.SetupApi/Mixaill.HwInfo.SetupApi.csproj index b47e32b..e483fcb 100644 --- a/Mixaill.HwInfo.SetupApi/Mixaill.HwInfo.SetupApi.csproj +++ b/Mixaill.HwInfo.SetupApi/Mixaill.HwInfo.SetupApi.csproj @@ -3,12 +3,12 @@ netstandard2.0 8 - 0.3.1 + 0.3.2 true https://github.com/Mixaill/Mixaill.HwInfo https://github.com/Mixaill/Mixaill.HwInfo.git - 0.3.1.0 - 0.3.1.0 + 0.3.2.0 + 0.3.2.0 Mikhail Paulyshka 2021, Mikhail Paulyshka MIT diff --git a/Mixaill.HwInfo.SetupApi/NativeMethods.txt b/Mixaill.HwInfo.SetupApi/NativeMethods.txt index aaa892d..5a265bf 100644 --- a/Mixaill.HwInfo.SetupApi/NativeMethods.txt +++ b/Mixaill.HwInfo.SetupApi/NativeMethods.txt @@ -20,3 +20,4 @@ SP_DEVINFO_DATA // DevInst/Defines ALLOC_LOG_CONF +BOOT_LOG_CONF diff --git a/Mixaill.HwInfoSetupApi.Demo/Mixaill.HwInfo.SetupApi.Demo.csproj b/Mixaill.HwInfoSetupApi.Demo/Mixaill.HwInfo.SetupApi.Demo.csproj index 1c1f88d..87f69d8 100644 --- a/Mixaill.HwInfoSetupApi.Demo/Mixaill.HwInfo.SetupApi.Demo.csproj +++ b/Mixaill.HwInfoSetupApi.Demo/Mixaill.HwInfo.SetupApi.Demo.csproj @@ -4,7 +4,7 @@ Exe net5.0 Mixaill.HwInfo.SetupApi.Demo.Program - 0.3.1 + 0.3.2 Mikhail Paulyshka Mikhail Paulyshka 2021, Mikhail Paulyshka @@ -13,8 +13,8 @@ https://github.com/Mixaill/Mixaill.HwInfo.git git true - 0.3.1.0 - 0.3.1.0 + 0.3.2.0 + 0.3.2.0