Skip to content

Commit e4646cf

Browse files
committed
Add NTOS Registry version fetching to WPBuildInfo
1 parent f46d216 commit e4646cf

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using DiscUtils.Registry;
2+
3+
namespace WPBuildInfo
4+
{
5+
internal class DetectionHandler
6+
{
7+
public static string ExtractVersionInfo2(Stream hiveStream)
8+
{
9+
string BranchName = "";
10+
ulong BuildNumber = 0;
11+
string CompileDate = "";
12+
ulong DeltaVersion = 0;
13+
ulong MajorVersion = 0;
14+
ulong MinorVersion = 0;
15+
16+
using (RegistryHive hive = new(hiveStream))
17+
{
18+
try
19+
{
20+
RegistryKey subkey = hive.Root.OpenSubKey(@"Microsoft\Windows NT\CurrentVersion");
21+
22+
string buildLab = (string)subkey.GetValue("BuildLab");
23+
string buildLabEx = (string)subkey.GetValue("BuildLabEx");
24+
25+
string releaseId = (string)subkey.GetValue("ReleaseId");
26+
27+
int? major = (int?)subkey.GetValue("CurrentMajorVersionNumber");
28+
string build = (string)subkey.GetValue("CurrentBuildNumber");
29+
int? minor = (int?)subkey.GetValue("CurrentMinorVersionNumber");
30+
int? ubr = (int?)subkey.GetValue("UBR");
31+
string branch = null;
32+
33+
subkey = hive.Root.OpenSubKey(
34+
@"Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed");
35+
if (subkey != null)
36+
{
37+
foreach (RegistryKey sub in subkey.SubKeys)
38+
{
39+
if (!sub.Name.Contains(".OS."))
40+
{
41+
continue;
42+
}
43+
44+
branch = sub.GetValue("Branch") as string;
45+
}
46+
}
47+
48+
if (!string.IsNullOrEmpty(buildLab) && buildLab.Count(x => x == '.') == 2)
49+
{
50+
string[] splitLab = buildLab.Split('.');
51+
52+
BranchName = splitLab[1];
53+
CompileDate = splitLab[2];
54+
BuildNumber = ulong.Parse(splitLab[0]);
55+
}
56+
57+
if (!string.IsNullOrEmpty(buildLabEx) && buildLabEx.Count(x => x == '.') == 4)
58+
{
59+
string[] splitLabEx = buildLabEx.Split('.');
60+
61+
BranchName = splitLabEx[3];
62+
CompileDate = splitLabEx[4];
63+
DeltaVersion = ulong.Parse(splitLabEx[1]);
64+
BuildNumber = ulong.Parse(splitLabEx[0]);
65+
}
66+
67+
if (major.HasValue)
68+
{
69+
MajorVersion = (ulong)major.Value;
70+
}
71+
72+
if (minor.HasValue)
73+
{
74+
MinorVersion = (ulong)minor.Value;
75+
}
76+
77+
if (!string.IsNullOrEmpty(build))
78+
{
79+
BuildNumber = ulong.Parse(build);
80+
}
81+
82+
if (ubr.HasValue)
83+
{
84+
DeltaVersion = (ulong)ubr.Value;
85+
}
86+
87+
if (!string.IsNullOrEmpty(branch))
88+
{
89+
BranchName = branch;
90+
}
91+
}
92+
catch
93+
{
94+
}
95+
}
96+
97+
return $"{MajorVersion}.{MinorVersion}.{BuildNumber}.{DeltaVersion} ({BranchName}.{CompileDate})";
98+
}
99+
}
100+
}

src/Applications/WPBuildInfo/Program.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ Windows Phone Build Info Tool
3737
FindKernel(disks);
3838
Logging.Log();
3939

40+
FindRegistry(disks);
41+
Logging.Log();
42+
4043
FindPackages(disks);
4144
Logging.Log();
4245

4346
Console.WriteLine("The operation completed successfully.");
47+
48+
Console.ReadLine();
4449
}
4550

4651
private static void FindBuildInfo(IEnumerable<IDisk> disks)
@@ -109,6 +114,34 @@ private static void FindKernel(IEnumerable<IDisk> disks)
109114
}
110115
}
111116

117+
private static void FindRegistry(IEnumerable<IDisk> disks)
118+
{
119+
foreach (IDisk disk in disks)
120+
{
121+
foreach (IPartition partition in disk.Partitions)
122+
{
123+
if (partition.FileSystem is IFileSystem fileSystem)
124+
{
125+
try
126+
{
127+
if (fileSystem.FileExists(@"Windows\System32\config\SOFTWARE"))
128+
{
129+
using DiscUtils.Streams.SparseStream hiveStream = fileSystem.OpenFile(@"Windows\System32\config\SOFTWARE", FileMode.Open, FileAccess.Read);
130+
131+
string version = DetectionHandler.ExtractVersionInfo2(hiveStream);
132+
133+
Logging.Log($"NTOS: Registry ({partition.Name}): {version}");
134+
}
135+
}
136+
catch
137+
{
138+
139+
}
140+
}
141+
}
142+
}
143+
}
144+
112145
private static void FindPackages(IEnumerable<IDisk> disks)
113146
{
114147
foreach (IDisk disk in disks)

src/Applications/WPBuildInfo/WPBuildInfo.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<Platforms>x86;x64;ARM64;ARM32</Platforms>
1010
</PropertyGroup>
1111

12+
<ItemGroup>
13+
<PackageReference Include="LTRData.DiscUtils.Registry" Version="1.0.55" />
14+
</ItemGroup>
15+
1216
<ItemGroup>
1317
<ProjectReference Include="..\..\MobilePackageGen.Common\MobilePackageGen.Common.csproj" />
1418
</ItemGroup>

0 commit comments

Comments
 (0)