我正在学习KMDF驱动程序与UWP应用开发,尝试通过UWP应用与KMDF驱动程序通信,根据面向应用开发人员的硬件支持应用 (HSA) 步骤 - Windows drivers | Microsoft Docs实现了,但是对驱动和UWP进行了一些更新后,UWP访问KMDF驱动会发生异常(拒绝访问。(Exception from HRESULT:0x80070005))
我不知道是否是在对UWP应用和驱动的更新过程中修改了什么发生了错误,因此我重新编写了一个驱动程序及其对应的UWP应用,同样发生了这个异常,下面是关于驱动程序和UWP应用的部分配置。
我想知道是什么导致了这个问题,如何解决这个问题?
驱动程序添加接口与自定义功能:
WDFSTRING symbolicLinkString = NULL;
UNICODE_STRING symbolicLinkName = SymbolicName;//符号链接名称
DEVPROP_BOOLEAN isRestricted;
status = WdfDeviceCreateDeviceInterface(
Device,
(LPGUID)&GUID_DEVINTERFACE_HSADrvSample,//设备接口GUID
NULL); // Reference String
if (!NT_SUCCESS(status)) {
KdPrint(("HSADrvSample:\t[ERROR]WdfDeviceCreateDeviceInterface failed with errorcode:%x\n", status));
goto Error;
}
if (g_pIoSetDeviceInterfacePropertyData != NULL) {
status = WdfStringCreate(NULL,
WDF_NO_OBJECT_ATTRIBUTES,
&symbolicLinkString);
if (!NT_SUCCESS(status)) {
KdPrint(("HSADrvSample:\t[ERROR]WdfStringCreate failed with errorcode:%x\n", status));
goto Error;
}
status = WdfDeviceRetrieveDeviceInterfaceString(
Device,
(LPGUID)&GUID_DEVINTERFACE_HSADrvSample,
NULL,
symbolicLinkString);
if (!NT_SUCCESS(status)) {
KdPrint(("HSADrvSample:\t[ERROR]WdfDeviceRetrieveDeviceInterfaceString failed with errorcode:%x\n", status));
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(("HSADrvSample:\t[ERROR]IoSetDeviceInterfacePropertyData failed with errorcode:%x\n", status));
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(("HSADrvSample:\t[ERROR]IoSetDeviceInterfacePropertyData failed with errorcode:%x\n", status));
goto Error;
}
#endif
WdfObjectDelete(symbolicLinkString);
}
Error:
if (symbolicLinkString != NULL) {
WdfObjectDelete(symbolicLinkString);
}
驱动程序INF文件
;
; HSADrvSample.inf
;
[Version]
Signature="$WINDOWS NT$"
Class=Sample ; TODO: edit Class
ClassGuid={78A1C341-4539-11d3-B88D-00C04FAD5171} ; TODO: edit ClassGuid
Provider=%ManufacturerName%
CatalogFile=HSADrvSample.cat
DriverVer = 05/18/2022,10.15.24.838
PnpLockDown=1
[DestinationDirs]
DefaultDestDir = 12
HSADrvSample_Device_CoInstaller_CopyFiles = 11
; ================= Class section =====================
[ClassInstall32]
Addreg=SampleClassReg
[SampleClassReg]
HKR,,,0,%ClassName%
HKR,,Icon,,-5
[SourceDisksNames]
1 = %DiskName%,,,""
[SourceDisksFiles]
HSADrvSample.sys = 1,,
WdfCoInstaller01011.dll=1 ; make sure the number matches with SourceDisksNames
;*****************************************
; Install Section
;*****************************************
[Manufacturer]
%ManufacturerName%=Standard,NTamd64
[Standard.NTamd64]
%HSADrvSample.DeviceDesc%=HSADrvSample_Device, Root\HSADrvSample ; TODO: edit hw-id
[HSADrvSample_Device.NT]
CopyFiles=Drivers_Dir
[Drivers_Dir]
HSADrvSample.sys
;-------------- Service installation
[HSADrvSample_Device.NT.Services]
AddService = HSADrvSample,%SPSVCINST_ASSOCSERVICE%, HSADrvSample_Service_Inst
; -------------- HSADrvSample driver install sections
[HSADrvSample_Service_Inst]
DisplayName = %HSADrvSample.SVCDESC%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ; SERVICE_DEMAND_START
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
ServiceBinary = %12%\HSADrvSample.sys
;
;--- HSADrvSample_Device Coinstaller installation ------
;
[HSADrvSample_Device.NT.CoInstallers]
AddReg=HSADrvSample_Device_CoInstaller_AddReg
CopyFiles=HSADrvSample_Device_CoInstaller_CopyFiles
[HSADrvSample_Device_CoInstaller_AddReg]
HKR,,CoInstallers32,0x00010000, "WdfCoInstaller01011.dll,WdfCoInstaller"
[HSADrvSample_Device_CoInstaller_CopyFiles]
WdfCoInstaller01011.dll
[HSADrvSample_Device.NT.Wdf]
KmdfService = HSADrvSample, HSADrvSample_wdfsect
[HSADrvSample_wdfsect]
KmdfLibraryVersion = 1.11
[Strings]
SPSVCINST_ASSOCSERVICE= 0x00000002
ManufacturerName="Colin" ;TODO: Replace with your manufacturer name
ClassName="Samples" ; TODO: edit ClassName
DiskName = "HSADrvSample Installation Disk"
HSADrvSample.DeviceDesc = "HSADrvSample Device"
HSADrvSample.SVCDESC = "HSADrvSample Service"
UWP包应用清单添加自定义功能:
<Capabilities>
<Capability Name="internetClient" />
<uap4:CustomCapability Name="ColinTest.HSADrvSample_2022051717171"/>
</Capabilities>
UWP应用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="ColinTest.HSADrvSample_2022051717171"></CustomCapability>
</CustomCapabilities>
<AuthorizedEntities AllowAny="true"/>
<Catalog>0000</Catalog>
</CustomCapabilityDescriptor>
UWP调用:
var selector = CustomDevice.GetDeviceSelector(DeviceInterfaceGuid);
Note.Text = "选择器:" + selector;
var enumDevice = DeviceInformation.FindAllAsync(selector).AsTask();
Task.WaitAll(enumDevice);
Note.Text = "找到了" + enumDevice.Result.Count + "个设备";
var DeviceID = enumDevice.Result[0].Id;
Note.Text = "设备ID:" + DeviceID;
try
{
var Device = CustomDevice.FromIdAsync(DeviceID, DeviceAccessMode.ReadWrite, DeviceSharingMode.Shared).AsTask();
Task.WaitAll(Device);
Note.Text = "设备打开成功";
}
catch (Exception ex)
{
Note.Text = ex.Message;
}
运行结果: