-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOwnKeyWindow.xaml.cs
287 lines (252 loc) · 13.1 KB
/
OwnKeyWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Win32;
using HGM.Hotbird64.Vlmcs;
using System.Globalization;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
using HGM.Hotbird64.LicenseManager.Extensions;
namespace HGM.Hotbird64.LicenseManager
{
public partial class OwnKeyWindow
{
[SuppressMessage("ReSharper", "MemberCanBePrivate.Local")]
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
private class KeyListItem
{
public string ProductName { get; set; }
public DateTime InstallDate { get; set; }
public string HiveName { get; set; }
public DigitalProductId3 Id3 { get; set; }
public DigitalProductId4 Id4 { get; set; }
public override string ToString() => $"{InstallDate}: {ProductName}";
public string DisplayDate => $"{(InstallDate != DateTimeExtensions.Epoch ? InstallDate.ToString(CultureInfo.CurrentCulture) : "")}";
}
private class HiveItem
{
public string HiveName;
public string DisplayName;
}
private static readonly IReadOnlyList<HiveItem> officeList = new[]
{
new HiveItem { HiveName=@"SOFTWARE\Microsoft\Office\14.0\Registration", DisplayName="Office2010", },
new HiveItem { HiveName=@"SOFTWARE\Microsoft\Office\15.0\Registration", DisplayName="Office2013", },
new HiveItem { HiveName=@"SOFTWARE\Microsoft\Office\16.0\Registration", DisplayName="Office2016", },
};
private static readonly IReadOnlyList<HiveItem> alternateWindowsList = new[]
{
new HiveItem { HiveName=@"SOFTWARE\Microsoft\Internet Explorer\Registration", DisplayName="Current Windows Key from IE", },
new HiveItem { HiveName=@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey", DisplayName="Default Windows Key 1", },
new HiveItem { HiveName=@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey2", DisplayName="Default Windows Key 2", },
};
private static readonly IReadOnlyList<HiveItem> sqlServerList = new[]
{
new HiveItem { HiveName = @"SOFTWARE\Microsoft\Microsoft SQL Server\130\Tools\Setup", DisplayName = "SQL Server 2016", },
new HiveItem { HiveName = @"SOFTWARE\Microsoft\Microsoft SQL Server\120\Tools\Setup", DisplayName = "SQL Server 2014", },
new HiveItem { HiveName = @"SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup", DisplayName = "SQL Server 2012", },
new HiveItem { HiveName = @"SOFTWARE\Microsoft\Microsoft SQL Server\105\Tools\Setup", DisplayName = "SQL Server 2008 R2", },
new HiveItem { HiveName = @"SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup", DisplayName = "SQL Server 2008", },
new HiveItem { HiveName = @"SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\Setup", DisplayName = "SQL Server 2005", },
};
public static RoutedUICommand InstallKey;
public static InputGestureCollection CtrlE = new InputGestureCollection();
static OwnKeyWindow()
{
CtrlE.Add(new KeyGesture(Key.E, ModifierKeys.Control));
InstallKey = new RoutedUICommand("Check or Install Key", "Install", typeof(ScalableWindow), CtrlE);
}
public unsafe OwnKeyWindow(MainWindow mainWindow) : base(mainWindow)
{
InitializeComponent();
TopElement.LayoutTransform = Scaler;
List<KeyListItem> productKeyList = new List<KeyListItem>();
using (RegistryKey sysKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess ? RegistryView.Registry64 : RegistryView.Default))
{
DigitalProductId4 id4;
DigitalProductId3 id3;
using (RegistryKey registryKey = sysKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
{
if (registryKey != null)
{
GetProductIds(registryKey, out id3, out id4);
if (id3.size == sizeof(DigitalProductId3) || id4.size == sizeof(DigitalProductId4))
{
productKeyList.Add(new KeyListItem
{
HiveName = registryKey.Name,
Id3 = id3,
Id4 = id4,
InstallDate = DateTimeExtensions.Epoch.AddSeconds(unchecked((uint)(int)registryKey.GetValue("InstallDate", 0))).ToLocalTime(),
ProductName = $"{registryKey.GetValue("ProductName", "unknown OS")} ({registryKey.GetValue("CurrentBuild", "N/A")})",
});
}
}
}
foreach (HiveItem windowsItem in alternateWindowsList)
{
using (RegistryKey registryKey = sysKey.OpenSubKey(windowsItem.HiveName))
{
if (registryKey != null)
{
GetProductIds(registryKey, out id3, out id4, isOffice: false);
if (id4.size == sizeof(DigitalProductId4))
{
productKeyList.Add(new KeyListItem
{
HiveName = registryKey.Name,
Id4 = id4,
Id3 = id3,
InstallDate = DateTimeExtensions.Epoch,
ProductName = windowsItem.DisplayName,
});
}
}
}
}
using (RegistryKey regKey = sysKey.OpenSubKey(@"SYSTEM\Setup"))
{
if (regKey != null)
{
foreach (string subKeyName in regKey.GetSubKeyNames().Where(n => n.StartsWith("Source OS")))
{
using (RegistryKey subKey = regKey.OpenSubKey(subKeyName))
{
GetProductIds(subKey, out id3, out id4, isOffice: false);
if (id4.size != sizeof(DigitalProductId4) && id3.size != sizeof(DigitalProductId3))
{
continue;
}
if (subKey != null)
{
productKeyList.Add(new KeyListItem
{
HiveName = System.IO.Path.Combine(@"HKEY_LOCAL_MACHINE\SYSTEM\Setup", subKeyName),
Id3 = id3,
Id4 = id4,
InstallDate = DateTimeExtensions.Epoch.AddSeconds(unchecked((uint)(int)subKey.GetValue("InstallDate", 0))).ToLocalTime(),
ProductName = $"{subKey.GetValue("ProductName", "unknown OS")} ({subKey.GetValue("CurrentBuild", "N/A")})",
});
}
}
}
}
}
foreach (HiveItem officeItem in officeList)
{
using (RegistryKey registryKey = sysKey.OpenSubKey(officeItem.HiveName))
{
if (registryKey == null)
{
continue;
}
foreach (string subKeyName in registryKey.GetSubKeyNames())
{
using (RegistryKey subKey = registryKey.OpenSubKey(subKeyName))
{
GetProductIds(subKey, out id3, out id4, isOffice: true);
if (id4.size != sizeof(DigitalProductId4))
{
continue;
}
if (subKey != null)
{
productKeyList.Add(new KeyListItem
{
HiveName = System.IO.Path.Combine(subKey.Name),
Id4 = id4,
InstallDate = DateTimeExtensions.Epoch,
ProductName =
$"{subKey.GetValue("ProductNameNonQualified", "")} ({officeItem.DisplayName})",
});
}
}
}
}
}
foreach (HiveItem sqlServerItem in sqlServerList)
{
using (RegistryKey registryKey = sysKey.OpenSubKey(sqlServerItem.HiveName))
{
object bytes = registryKey?.GetValue("DigitalProductId");
if (!(bytes is byte[]) || ((byte[])bytes).Length != 16)
{
continue;
}
id3 = default(DigitalProductId3);
id3.BinaryKey = new BinaryProductKey((byte[])bytes);
productKeyList.Add(new KeyListItem
{
HiveName = $"{System.IO.Path.Combine("HKEY_LOCAL_MACHINE", sqlServerItem.HiveName)}: DigitalProductId",
Id3 = id3,
InstallDate = DateTimeExtensions.Epoch,
ProductName = sqlServerItem.DisplayName,
});
}
}
}
DataGridKeys.ItemsSource = productKeyList.OrderBy(p => p.InstallDate);
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint GetSystemFirmwareTable(uint firmwareTableProviderSignature, uint firmwareTableID, IntPtr firmwareTableBuffer, uint bufferSize);
private void InstallKey_CanExecute(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = IsKeyInCurrentCell();
private bool IsKeyInCurrentCell()
{
DataGridCellInfo cell = DataGridKeys.SelectedCells.FirstOrDefault();
object cellValue = cell.Item.GetType().GetProperties().Single(p => p.Name == cell.Column.SortMemberPath).GetValue(cell.Item);
BinaryProductKey binaryKey = default(BinaryProductKey);
if (cellValue is DigitalProductId3 || cellValue is DigitalProductId4)
{
binaryKey = ((dynamic)cellValue).BinaryKey;
}
return !binaryKey.IsNullKey;
}
private void InstallKey_Executed(object sender, ExecutedRoutedEventArgs e)
{
DataGridCellInfo cell = DataGridKeys.SelectedCells.FirstOrDefault();
object cellValue = cell.Item.GetType().GetProperties().Single(p => p.Name == cell.Column.SortMemberPath).GetValue(cell.Item);
new ProductBrowser(MainWindow, cellValue.ToString()).Show();
}
private void DataGrid_Keys_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
InstallKey.Execute(null, this);
e.Handled = true;
}
public static void GetProductIds(RegistryKey regKey, out DigitalProductId3 id3, out DigitalProductId4 id4, bool isOffice = false)
{
id3 = new DigitalProductId3();
id4 = new DigitalProductId4();
if (!isOffice)
{
try
{
if (regKey?.GetValue("DigitalProductId") is byte[] productId3)
{
id3 = (DigitalProductId3)productId3;
}
}
catch { }
}
try
{
if (regKey?.GetValue(isOffice ? "DigitalProductID" : "DigitalProductId4") is byte[] productId4)
{
id4 = (DigitalProductId4)productId4;
}
}
catch { }
}
private void DataGrid_Keys_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) => InstallButton.IsEnabled = IsKeyInCurrentCell();
private void InstallButton_Click(object sender, RoutedEventArgs e) => InstallKey_Executed(sender, null);
private void CancelButton_Click(object sender, RoutedEventArgs e) => Close();
private void DataGrid_Keys_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.MouseDoubleClick += DataGrid_Keys_MouseDoubleClick;
e.Row.Background = App.DatagridBackgroundBrushes[e.Row.GetIndex() % App.DatagridBackgroundBrushes.Count];
}
}
}