Skip to content

Commit aab610f

Browse files
committed
Correctly handle different architectures for devdeviceID (#43471)
1 parent 8fd78e9 commit aab610f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Cli/dotnet/Telemetry/DevDeviceIDGetter.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ private static string GetCachedDeviceId()
3838

3939
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
4040
{
41-
// Get device Id from Windows registry
42-
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
41+
// Get device Id from Windows registry matching the OS architecture
42+
using (var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
4343
{
4444
deviceId = key?.GetValue("deviceid") as string;
4545
}
@@ -80,10 +80,16 @@ private static void CacheDeviceId(string deviceId)
8080
{
8181
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
8282
{
83-
// Cache device Id in Windows registry
84-
using (var key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
83+
// Cache device Id in Windows registry matching the OS architecture
84+
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
8585
{
86-
key.SetValue("deviceid", deviceId);
86+
using(var key = baseKey.CreateSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
87+
{
88+
if (key != null)
89+
{
90+
key.SetValue("deviceid", deviceId);
91+
}
92+
}
8793
}
8894
}
8995
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))

src/Tests/dotnet.Tests/TelemetryCommonPropertiesTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,16 @@ public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddress(
5050
}
5151

5252
[Fact]
53-
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotDevDeviceId()
53+
public void TelemetryCommonPropertiesShouldEnsureDevDeviceIDIsCached()
5454
{
5555
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
5656
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];
5757

5858
Guid.TryParse(assignedMachineId, out var _).Should().BeTrue("it should be a guid");
59+
var secondAssignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];
60+
61+
Guid.TryParse(secondAssignedMachineId, out var _).Should().BeTrue("it should be a guid");
62+
secondAssignedMachineId.Should().Be(assignedMachineId, "it should match the previously assigned guid");
5963
}
6064

6165
[Fact]

0 commit comments

Comments
 (0)