MatthewKing / DeviceId

A simple library providing functionality to generate a 'device ID' that can be used to uniquely identify a computer.
MIT License
722 stars 118 forks source link

DeviceIdBuilder AddSystemDriveSerialNumber() - System.Management.ManagementException: Invalid class #69

Closed robkite closed 1 year ago

robkite commented 1 year ago

Hi

We're seeing an issue on some users machines (few and far in between, but regular enough to be a problem). It seems that using the AddSystemDriveSerialNumber() method for DeviceIdBuilder is causing an exception to be thrown on these machines (stack trace below). We are currently still using 5.2.0 primarily due to the WMI vs MMI issue, so I would expect to receive the same issue on the newer 6.x versions using WMI as I can't see that anything was changed in this area.

System.Management.ManagementException: Invalid class 
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
   at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
   at DeviceId.Components.SystemDriveSerialNumberDeviceIdComponent.GetValue()
   at DeviceId.Formatters.HashDeviceIdFormatter.<>c.<GetDeviceId>b__3_1(IDeviceIdComponent x)
   at System.Linq.Enumerable.SelectIPartitionIterator`2.PreallocatingToArray(Int32 count)
   at System.Linq.Enumerable.SelectIPartitionIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at DeviceId.Formatters.HashDeviceIdFormatter.GetDeviceId(IEnumerable`1 components)
   at DeviceId.DeviceIdBuilder.ToString()

As this is only happening on some machines, and we don't have direct access to any of them I can't provide any code to reproduce this. I'm hoping that you may have some idea as to why this may happen and how we can work around the issue?

Thanks

Rob

MatthewKing commented 1 year ago

It's very tricky without having direct access to the device.

I'm going to guess it's a WMI issue. Googling the "invalid class" WMI error message returns a lot of results of other people having similar issues with WMI, and there are many suggested resolutions there. You could investigate some of those.

Otherwise, you can just duplicate the component from here, add it to your codebase, and make any necessary changes to suit your specific use case. Then use your custom component instead of the built-in one.

Cheers

robkite commented 1 year ago

Hi Matthew

Thanks for the quick response. I figured this would likely be the case, as a note we have opted to omit the drive serial number from our device id and migrate users across to the new id.