Skip to content

Commit

Permalink
HwInfo.SetupApi/DeviceInfo: fix CR_NO_MORE_LOG_CONF error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mixaill committed May 12, 2021
1 parent 6500516 commit f3b18c3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 41 deletions.
91 changes: 56 additions & 35 deletions Mixaill.HwInfo.SetupApi/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,69 +45,90 @@ internal DeviceInfo(DeviceInfoSet devInfoSet, SP_DEVINFO_DATA devInfo)
private List<DeviceResourceMemory> getMemoryResources(SP_DEVINFO_DATA devInfo)
{
var result = new List<DeviceResourceMemory>();

foreach (var res in getResources<MEM_RESOURCE>(devInfo))

var memoryRes = getResources<MEM_RESOURCE>(devInfo, Constants.ALLOC_LOG_CONF);
if(memoryRes.Count == 0)
{
memoryRes = getResources<MEM_RESOURCE>(devInfo, Constants.BOOT_LOG_CONF);
}

var largeMemoryRes = getResources<Mem_Large_Resource_s>(devInfo, Constants.ALLOC_LOG_CONF);
if (largeMemoryRes.Count == 0)
{
largeMemoryRes = getResources<Mem_Large_Resource_s>(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<Mem_Large_Resource_s>(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));
}

return result;
}

private unsafe List<T> getResources<T>(SP_DEVINFO_DATA devInfo) where T: struct
private unsafe List<T> getResources<T>(SP_DEVINFO_DATA devInfo, uint logConfFlags) where T: struct
{
var result = new List<T>();
var resources = new List<T>();

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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions Mixaill.HwInfo.SetupApi/Mixaill.HwInfo.SetupApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
<Version>0.3.1</Version>
<Version>0.3.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/Mixaill/Mixaill.HwInfo</PackageProjectUrl>
<RepositoryUrl>https://github.com/Mixaill/Mixaill.HwInfo.git</RepositoryUrl>
<AssemblyVersion>0.3.1.0</AssemblyVersion>
<FileVersion>0.3.1.0</FileVersion>
<AssemblyVersion>0.3.2.0</AssemblyVersion>
<FileVersion>0.3.2.0</FileVersion>
<Authors>Mikhail Paulyshka</Authors>
<Copyright>2021, Mikhail Paulyshka</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
1 change: 1 addition & 0 deletions Mixaill.HwInfo.SetupApi/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ SP_DEVINFO_DATA

// DevInst/Defines
ALLOC_LOG_CONF
BOOT_LOG_CONF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<StartupObject>Mixaill.HwInfo.SetupApi.Demo.Program</StartupObject>
<Version>0.3.1</Version>
<Version>0.3.2</Version>
<Authors>Mikhail Paulyshka</Authors>
<Company>Mikhail Paulyshka</Company>
<Copyright>2021, Mikhail Paulyshka</Copyright>
Expand All @@ -13,8 +13,8 @@
<RepositoryUrl>https://github.com/Mixaill/Mixaill.HwInfo.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>0.3.1.0</AssemblyVersion>
<FileVersion>0.3.1.0</FileVersion>
<AssemblyVersion>0.3.2.0</AssemblyVersion>
<FileVersion>0.3.2.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit f3b18c3

Please sign in to comment.