0

In Win32 api I could use functions HidD_GetSerialNumberString to get a device serial number. How I can get such properties in UWP? There seems to be no way to get SerialNumber.

To be clear, I am making sandboxed UWP app, and cannot use P/Invoke.

Shadow
  • 2,089
  • 2
  • 23
  • 45
  • Maybe this is an [XY problem](http://xyproblem.info/)? – Uwe Keim Oct 13 '20 at 17:25
  • The serial number will only be there if the driver adds the serial number. first check with Device Manager if the serial number is being shown. If so then use the Pinvoke code to get the device info. See : http://www.pinvoke.net/default.aspx/setupapi/SetupDiGetDeviceInterfaceDetail.html Also check if you are using the default Microsoft Driver or the vendor driver. I would recommend using the vendors driver. – jdweng Oct 13 '20 at 17:29
  • @UweKeim you suggest that I do not need my device serial number or what? – Shadow Oct 13 '20 at 17:48
  • @jdweng I have working P/Invoke code, which returns correct serial number. However I want to create UWP app, which does not have access to P/Invoke because of sandbox restrictions, therefore I need UWP apis to query that. – Shadow Oct 13 '20 at 17:49
  • Did you try the Pinvoke? It may work. See : https://stackoverflow.com/questions/36286806/uwp-limitations-in-desktop-apps A lot of methods in Net Library are just wrappers that calls the Windows dll. So in WCF you are actually using the dlls the are in Pinvoke. – jdweng Oct 13 '20 at 18:04
  • 1
    @jdweng please see tags and comments and description, I've explicitly stated that I cannot use P/Invoke because my app will not be certified in windows store. – Shadow Oct 13 '20 at 18:11
  • Did you see following : https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions. – jdweng Oct 13 '20 at 18:22
  • @jdweng is this the correct link? I do not understand how file access permissions are connected to hid/usb devices. – Shadow Oct 13 '20 at 18:24
  • You can't get the windows dll from UDP because you do not have access to the c:\Windows\system32 folder where the dll is located. – jdweng Oct 13 '20 at 18:28
  • Yes! If you enhance your question by explaining _why_ you need the serial number maybe someone can give you solutions that do work with UWP. – Uwe Keim Oct 14 '20 at 05:57
  • @jdweng but I do not want to P/Invoke any dll, I'm asking about UWP api which does this. I know how to do it with win32 api, but I don't want to use unmanaged apis or P/Invoke as they require run full trust permissions, and my app is sandboxed. – Shadow Oct 14 '20 at 08:55
  • @UweKeim I need serial number because I need to display it to the user. – Shadow Oct 14 '20 at 08:55
  • Each device has a header and the serial number is one of the properties in the header. It doesn't look like c# allows you to get the serial number in one step. I think the serial number may be found by using one of the methods (probably DisplayDeviceInterfaceArray) on the following link : https://learn.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration.DeviceInformation?view=winrt-19041 – jdweng Oct 14 '20 at 09:42
  • @jdweng I suspect that there is property with serial number, but I am unable to find which one it is. I need to pass properties I request to DeviceInformation object. Apparently this is none of these https://learn.microsoft.com/en-us/windows/win32/properties/devices-bumper – Shadow Oct 14 '20 at 10:07

1 Answers1

1

Serial number can reside in Feature report of the HID device (You should check with Drivers team once).

This is how I did it, may / may not work for you.

using HidDevice hidDevice = await HidDevice.FromIdAsync(Id, FileAccessMode.ReadWrite);
                
var controlDataList = hidDevice.GetNumericControlDescriptions(HidReportType.Feature, 0, UsageId);
if (controlDataList == null || controlDataList.Count == 0)
{
    return;
}

ushort featureReportId = controlDataList[0].ReportId;
HidFeatureReport response = await hidDevice.GetFeatureReportAsync(featureReportId);
byte[] data = new byte[response.Data.Length];
response.Data.CopyTo(data);
CustomFeatureReport? featureReport = data.BytesToStruct<CustomFeatureReport>();
ggeorge
  • 1,496
  • 2
  • 13
  • 19
Rob_Stark
  • 11
  • 2