'UWP application use "CustomDevice.FromIdAsync" access KMDF driver is denied
I found a similar question, but it probably didn't help me much.
I'm learning to access drive devices via UWP, in the first two days, the program was working fine, but today, UWP error close when open device, the error message is "Access is denied. Exception from HRESULT: 0x80070005"
I don't know if something was modified during the update process that caused this problem. I only have a snapshot of the virtual machine that can open the drive device. On this snapshot, when I update the UWP application, I can also open the drive device, but after update the driver, the UWP application can no longer open the driver device.
Does this mean the problem is with the driver?
After that, I created a new KMDF project and a UWP project, only printed some information, and the same error occurred after installing to the virtual machine.
It is worth noting that when I saw the UWP access to the drive device in DbgView, the EvtDeviceFileCreate event callback was triggered, but the EvtDeviceClose event callback was triggered immediately, I think, there should be no problem with the device interface.
Also, in the beginning, I used the device manager to "Add legacy hardware" to load the driver, then I tried to install the driver using devcon, both can install the driver, but it seems to be slightly different, I am not sure if this also It will have an impact, although both installation methods currently do not solve the problem I am encountering.
Below are some settings for UWP app and drivers.
Register device interface and add custom capability:
In this part, I refer to Microsoft's official sample kmdf_fx2.
WDFSTRING symbolicLinkString = NULL;
UNICODE_STRING symbolicLinkName = SymbolicName;
DEVPROP_BOOLEAN isRestricted;
status = WdfDeviceCreateDeviceInterface(
hDevice,
(LPGUID)&GUID_INTERFACE_HSADRVSAMPLE1,
NULL); // Reference String
if (!NT_SUCCESS(status)) {
KdPrint(("WdfDeviceCreateDeviceInterface Fail\n"));
goto Error;
}
if (g_pIoSetDeviceInterfacePropertyData != NULL) {
status = WdfStringCreate(NULL,
WDF_NO_OBJECT_ATTRIBUTES,
&symbolicLinkString);
if (!NT_SUCCESS(status)) {
KdPrint(("WdfStringCreate Fail\n"));
goto Error;
}
status = WdfDeviceRetrieveDeviceInterfaceString(
hDevice,
(LPGUID)&GUID_INTERFACE_HSADRVSAMPLE1,
NULL,
symbolicLinkString);
if (!NT_SUCCESS(status)) {
KdPrint(("WdfDeviceRetrieveDeviceInterfaceString Fail\n"));
goto Error;
}
WdfStringGetUnicodeString(symbolicLinkString, &symbolicLinkName);
isRestricted = DEVPROP_TRUE;
status = g_pIoSetDeviceInterfacePropertyData(
&symbolicLinkName,
&DEVPKEY_DeviceInterface_Restricted,
0,
0,
DEVPROP_TYPE_BOOLEAN,
sizeof(isRestricted),
&isRestricted);
if (!NT_SUCCESS(status)) {
KdPrint(("g_pIoSetDeviceInterfacePropertyData Fail\n"));
goto Error;
}
#if defined(NTDDI_WIN10_RS2) && (NTDDI_VERSION >= NTDDI_WIN10_RS2)
static const wchar_t customCapabilities[] = L"ColinTest.HSADrvSample_2022051717171\0";
status = g_pIoSetDeviceInterfacePropertyData(
&symbolicLinkName,
&DEVPKEY_DeviceInterface_UnrestrictedAppCapabilities,
0,
0,
DEVPROP_TYPE_STRING_LIST,
sizeof(customCapabilities),
(PVOID)&customCapabilities);
if (!NT_SUCCESS(status)) {
KdPrint(("g_pIoSetDeviceInterfacePropertyData Fail\n"));
goto Error;
}
#endif
WdfObjectDelete(symbolicLinkString);
}
Error:
if (symbolicLinkString != NULL) {
WdfObjectDelete(symbolicLinkString);
}
Driver INF:
;
; HSADrvSample1.inf
;
[Version]
Signature="$WINDOWS NT$"
Class=Test ; TODO: edit Class
ClassGuid={160303BD-D84C-4819-B962-9B1BDBB52310} ; TODO: edit ClassGuid
Provider=%ManufacturerName%
CatalogFile=HSADrvSample1.cat
DriverVer = 05/19/2022,9.21.50.151
PnpLockDown=1
[DestinationDirs]
DefaultDestDir = 12
HSADrvSample1_Device_CoInstaller_CopyFiles = 11
; ================= Class section =====================
[ClassInstall32]
Addreg=SampleClassReg
[SampleClassReg]
HKR,,,0,%ClassName%
HKR,,Icon,,-5
[SourceDisksNames]
1 = %DiskName%,,,""
[SourceDisksFiles]
HSADrvSample1.sys = 1,,
WdfCoInstaller01011.dll=1 ; make sure the number matches with SourceDisksNames
;*****************************************
; Install Section
;*****************************************
[Manufacturer]
%ManufacturerName%=Standard,NTamd64
[Standard.NTamd64]
%HSADrvSample1.DeviceDesc%=HSADrvSample1_Device, Root\HSADrvSample1 ; TODO: edit hw-id
[HSADrvSample1_Device.NT]
CopyFiles=Drivers_Dir
[Drivers_Dir]
HSADrvSample1.sys
;-------------- Service installation
[HSADrvSample1_Device.NT.Services]
AddService = HSADrvSample1,%SPSVCINST_ASSOCSERVICE%, HSADrvSample1_Service_Inst
; -------------- HSADrvSample1 driver install sections
[HSADrvSample1_Service_Inst]
DisplayName = %HSADrvSample1.SVCDESC%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ; SERVICE_DEMAND_START
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
ServiceBinary = %12%\HSADrvSample1.sys
;
;--- HSADrvSample1_Device Coinstaller installation ------
;
[HSADrvSample1_Device.NT.CoInstallers]
AddReg=HSADrvSample1_Device_CoInstaller_AddReg
CopyFiles=HSADrvSample1_Device_CoInstaller_CopyFiles
[HSADrvSample1_Device_CoInstaller_AddReg]
HKR,,CoInstallers32,0x00010000, "WdfCoInstaller01011.dll,WdfCoInstaller"
[HSADrvSample1_Device_CoInstaller_CopyFiles]
WdfCoInstaller01011.dll
[HSADrvSample1_Device.NT.Wdf]
KmdfService = HSADrvSample1, HSADrvSample1_wdfsect
[HSADrvSample1_wdfsect]
KmdfLibraryVersion = 1.11
[Strings]
SPSVCINST_ASSOCSERVICE= 0x00000002
ManufacturerName="Colin" ;TODO: Replace with your manufacturer name
ClassName="Test" ; TODO: edit ClassName
DiskName = "HSADrvSample1 Installation Disk"
HSADrvSample1.DeviceDesc = "HSADrvSample1 Device"
HSADrvSample1.SVCDESC = "HSADrvSample1 Service"
SCCD File:
The file is configured as per the official documentationHardware Support App (HSA): Steps for App Developers, the file is already set to content.
<?xml version="1.0" encoding="utf-8"?>
<CustomCapabilityDescriptor xmlns="http://schemas.microsoft.com/appx/2018/sccd" xmlns:s="http://schemas.microsoft.com/appx/2018/sccd">
<CustomCapabilities>
<CustomCapability Name="ColinTest.HSADrvSample_2022051717171"></CustomCapability>
<AuthorizedEntities>
<AuthorizedEntity AppPackageFamilyName="Colin.HSA.APP_4a8dxf1xdwf12" CertificateSignatureHash="2d6e4f1418bc13447bb8c10067d0af418cb8a2f9e14c014e03a1e0ec811735af"></AuthorizedEntity>
</AuthorizedEntities>
<Catalog>FFFF</Catalog>
</CustomCapabilityDescriptor>
Part of Package.appxmanifest:
<Capabilities>
<Capability Name="internetClient" />
<uap4:CustomCapability Name="ColinTest.HSADrvSample_2022051717171"/>
</Capabilities>
Part of UWP Code:
string selector = CustomDevice.GetDeviceSelector(DeviceInterfaceGuid);
DeviceInformationCollection enumDevice = await DeviceInformation.FindAllAsync(selector);
string DeviceID = enumDevice[0].Id;
try
{
CustomDevice Device = await CustomDevice.FromIdAsync(DeviceID, DeviceAccessMode.ReadWrite, DeviceSharingMode.Shared);
if (null != Device)
{
Note.Text = "Open successfully";
}
}
catch (Exception ex)
{
Note.Text = "Failed to open!" + ex.Message;
}
I would like to know at which step am I getting the error? How to solve?
At the suggestion of @Nico Zhu - MSFT, I checked the
CustomCapabilityNameagain.
There are three places in my code related to CustomCapabilityName, but they are all the same.
To test if the problem lies here, I reset the name.
- Driver Code
static const wchar_t customCapabilities[] = L"Test.hsaTestCustomCapability_1653011401000\0";
- CustomCapability.SCCD
<?xml version="1.0" encoding="utf-8"?>
<CustomCapabilityDescriptor xmlns="http://schemas.microsoft.com/appx/2018/sccd" xmlns:s="http://schemas.microsoft.com/appx/2018/sccd">
<CustomCapabilities>
<CustomCapability Name="Test.hsaTestCustomCapability_1653011401000"></CustomCapability>
</CustomCapabilities>
<AuthorizedEntities AllowAny="true"/>
<Catalog>0000</Catalog>
</CustomCapabilityDescriptor>
- Package.appxmanifest
<Capabilities>
<Capability Name="internetClient" />
<uap4:CustomCapability Name="Test.hsaTestCustomCapability_1653011401000"/>
</Capabilities>
After that, I uninstalled the previously installed apps and drivers, restarted and installed the latest version of the program, but I still can't access it.
My SDK and WDK version is 10.0.19041.0, test machine OS version is Windows 10 Pro Build 19041.vb_release.191206-1406
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
