Windows 10与Zebra LP2844的WebUSB打印问题:DOMException:访问被拒绝

8
在一个项目中,我试图使用Chrome中可用的WebUSB API来使用Zebra LP2844打印机进行打印。在OSX上没有问题,在Ubuntu和ChromeOS上也成功了,感谢this post,它谈到了解除内核驱动程序的绑定,这样Chrome就能访问设备了。我用的页面是通过https服务的,因为文档要求这样做。然而,在Windows 10上,我能够像这里所示使用navigator.usb.requestDevice连接打印机。

connected printer

但是,当我在打印过程中调用已连接打印机的open()方法时,我一直收到以下错误:

错误 DOMException: 访问被拒绝。

以下是打印方法的代码:

print : async function(printString) {
    let startTime = new Date().getTime();

    if (!this.pairedPrinter) {
      console.log("No printer connected");
      return;
    }

    try {
      if (!this.pairedPrinter.opened) {
        await this.pairedPrinter.open();
      }

      await this.pairedPrinter.claimInterface(0);

      let encoder = new TextEncoder();
      const printBuffer = encoder.encode(printString);

      let printResult = await this.pairedPrinter.transferOut(6, printBuffer);

      await this.pairedPrinter.releaseInterface(0);

      await this.pairedPrinter.close();

    } catch (err) {
      console.log("Error");
      console.log(err);
    } finally {
      let endTime = new Date().getTime();
      let printTime = endTime - startTime;
      console.log("Print time: " + printTime + " ms");
    }
}

我发现一个类似的问题在这里,它指向另一个答案。当我插入打印机时,Windows安装默认驱动程序会阻止Chrome访问它(就像Ubuntu一样),这很有道理。
所以,我按照这里描述的方法实现了一个自定义的.inf文件。
这是我的inf文件:
;
;
; Installs WinUsb
;

; =================== Strings ===================

[Strings]
ManufacturerName = "Zebra"
ClassName = "Universal Serial Bus devices"
DeviceName = "Aeropost Zebra LP2844"
SourceName = "Aeropost Zebra LP2844 Install Disk"
DeviceID   = "VID_0A5F&PID_0009"
DeviceGUID = "{c3bd2e26-7e03-4189-8c9d-852faf628494}"
REG_MULTI_SZ = 0x00010000

[Version]
Signature = "$Windows NT$"
Class     = Printer
ClassGuid = {4d36e979-e325-11ce-bfc1-08002be10318}
Provider  = %ManufacturerName%
CatalogFile = zebrawinusb.cat
DriverVer = 03/02/2018,1.0.0.0

; ========== Class definition ===========

[ClassInstall32]
AddReg = ClassInstall_AddReg

[ClassInstall_AddReg]
HKR,,,0,"Universal Serial Bus devices"
HKR,,Icon,,-20

; ========== Manufacturer/Models sections ===========

[Manufacturer]
%ManufacturerName% = Standard,NTx86
%ManufacturerName% = Standard,NTamd64

[Standard.NTx86]
%DeviceName% = USB_Install, USB\%DeviceID%

[Standard.NTamd64]
%DeviceName% = USB_Install, USB\%DeviceID%

; =================== Installation ===================

[USB_Install]
Include = winusb.inf
Needs   = WINUSB.NT

[USB_Install.Services]
Include = winusb.inf
Needs   = WINUSB.NT.Services

[USB_Install.Wdf]
KmdfService = WINUSB, WinUsb_Install

[WinUSB_Install]
KmdfLibraryVersion = 1.11

[USB_Install.HW]
AddReg=Dev_AddReg

[Dev_AddReg]
HKR,,DeviceInterfaceGUIDs,0x10000,%DeviceGUID%

[USB_Install.CoInstallers]
AddReg = CoInstallers_AddReg
CopyFiles = CoInstallers_CopyFiles

[CoInstallers_AddReg]
HKR,,CoInstallers32,0x00010000,"WdfCoInstaller01011.dll,WdfCoInstaller","WinUsbCoInstaller2.dll"

[CoInstallers_CopyFiles]
WinUsbCoInstaller2.dll
WdfCoInstaller01011.dll

[DestinationDirs]
; If your INF needs to copy files, you must not use the DefaultDestDir directive here.  
CoInstallers_CopyFiles=11
; ================= Source Media Section =====================

[SourceDisksNames]
1 = %SourceName%

[SourceDisksFiles.x86]
WinUSBCoInstaller2.dll = 1,x86
WdfCoInstaller01011.dll = 1,x86

[SourceDisksFiles.x64]
WinUSBCoInstaller2.dll = 1,x64
WdfCoInstaller01011.dll = 1,x64

使用inf2cat构建了驱动程序,进行了测试签名。当我试图更新打印机驱动程序以使用它时,出现错误“找不到指定的文件(0x00000002)”。setupapi.dev.log文件并未提供任何有关未找到哪个文件的信息。以下是驱动程序更新过程的日志文件:
>>>  [Device Install (DiShowUpdateDevice) - USBPRINT\ZEBRA_LP2844_\6&3295E28F&3&USB001]
>>>  Section start 2018/03/02 16:14:00.023
      cmd: "C:\WINDOWS\system32\mmc.exe" C:\WINDOWS\system32\devmgmt.msc
     dvi: {DIF_UPDATEDRIVER_UI} 16:14:00.023
     dvi:      Class installer: Enter 16:14:00.023
     dvi:      Class installer: Exit
     dvi:      Default installer: Enter 16:14:00.039
     dvi:      Default installer: Exit
     dvi: {DIF_UPDATEDRIVER_UI - exit(0xe000020e)} 16:14:00.054
     ndv: {Update Driver Software Wizard - USBPRINT\ZEBRA_LP2844_\6&3295E28F&3&USB001}
     dvi:      {DIF_SELECTDEVICE} 16:14:05.662
     dvi:           Class installer: Enter 16:14:05.678
     dvi:           Class installer: Exit
     dvi:      {DIF_SELECTDEVICE - exit(0xe000020e)} 16:14:05.678
     dvi:      {DIF_SELECTDEVICE} 16:14:12.148
     dvi:           Class installer: Enter 16:14:12.148
     dvi:           Class installer: Exit
     dvi:      {DIF_SELECTDEVICE - exit(0xe000020e)} 16:14:12.163
     ndv:      Driver package 'C:\WINDOWS\System32\DriverStore\FileRepository\zebrawinusb.inf_amd64_ddcc3ed00fd3e8a8\zebrawinusb.inf' is already imported.
     sto:      {Setup Import Driver Package: c:\zebrawinusbdriver\zebrawinusb.inf} 16:14:16.158
     sto:           Driver package already imported as 'oem20.inf'.
     sto:      {Setup Import Driver Package - exit (0x00000000)} 16:14:16.180
     dvi:      Searching for hardware ID(s):
     dvi:           usbprint\zebra_lp2844_5bc4
     dvi:           zebra_lp2844_5bc4
     dvi:      Class GUID of device remains: {4d36e979-e325-11ce-bfc1-08002be10318}.
     dvi:      {Plug and Play Service: Device Install for USBPRINT\ZEBRA_LP2844_\6&3295E28F&3&USB001}
     dvi:           Driver INF Path: C:\WINDOWS\INF\oem20.inf
     dvi:           Driver Node Name: zebrawinusb.inf:c14ce8840c48fa1f:USB_Install:1.0.0.0:usb\vid_0a5f&pid_0009,
     dvi:           Driver Store Path: C:\WINDOWS\System32\DriverStore\FileRepository\zebrawinusb.inf_amd64_ddcc3ed00fd3e8a8\zebrawinusb.inf
     dvi:           Searching for hardware ID(s):
     dvi:                usbprint\zebra_lp2844_5bc4
     dvi:                zebra_lp2844_5bc4
     dvi:           Class GUID of device changed to: {4d36e979-e325-11ce-bfc1-08002be10318}.
     dvi:           {Core Device Install} 16:14:16.313
!    pol:                Selected driver node does not match this device (force-install)
     dvi:                {Install Device - USBPRINT\ZEBRA_LP2844_\6&3295E28F&3&USB001} 16:14:16.316
     dvi:                     Device Status: 0x0180200a, Problem: 0x0 (0x00000000)
     dvi:                     Parent device: USB\VID_0A5F&PID_0009\42J113802152
!    dvi:                     Unable to determine matching device ID for oem20.inf. Error = 0xE0000228
!    dvi:                     Unable to configure device, falling back to standard device installation.
     dvi:                     {DIF_ALLOW_INSTALL} 16:14:16.324
     dvi:                          Using exported function 'ClassInstall32' in module 'C:\WINDOWS\system32\ntprint.dll'.
     dvi:                          Class installer == ntprint.dll,ClassInstall32
     dvi:                          Class installer: Enter 16:14:16.336
     dvi:                          Class installer: Exit
     dvi:                          Default installer: Enter 16:14:16.340
     dvi:                          Default installer: Exit
     dvi:                     {DIF_ALLOW_INSTALL - exit(0xe000020e)} 16:14:16.342
     dvi:                     {DIF_INSTALLDEVICEFILES} 16:14:16.343
     dvi:                          Class installer: Enter 16:14:16.344
!!!  dvi:                          Class installer: failed(0x00000002)!
     dvi:                     {DIF_INSTALLDEVICEFILES - exit(0x00000002)} 16:14:16.383
!    dvi:                     Queueing up error report for device install failure.
     dvi:                {Install Device - exit(0x00000002)} 16:14:16.383
     dvi:           {Core Device Install - exit(0x00000002)} 16:14:16.383
     dvi:           {DIF_DESTROYPRIVATEDATA} 16:14:16.383
     dvi:                Class installer: Enter 16:14:16.383
     dvi:                Class installer: Exit
     dvi:                Default installer: Enter 16:14:16.399
     dvi:                Default installer: Exit
     dvi:           {DIF_DESTROYPRIVATEDATA - exit(0xe000020e)} 16:14:16.399
     ump:      {Plug and Play Service: Device Install exit(00000002)}
!!!  ndv:      Device install failed for device.
     ndv: {Update Driver Software Wizard exit(00000002)}
<<<  Section end 2018/03/02 16:15:14.498
<<<  [Exit status: FAILURE(0x00000002)]

我甚至尝试使用zadig替换打印机驱动程序,但结果相同(访问被拒绝错误)。

此时,任何帮助都将不胜感激。

更新 03/05/2018

今天,我通过以下步骤成功完成了自定义inf安装程序:

  • 防止Windows 10在插入打印机时搜索并安装驱动程序(在此查看详细信息
  • 更新我的自定义inf安装程序以使用USBDevice类和{88bae032-5a81-49f0-bc3d-a4ff138216d6} ClassGuid值
  • 构建驱动程序cat文件并签名
  • 卸载设备的所有驱动程序并拔掉打印机
  • 重新启动计算机
  • 再次插入打印机。此时,在设备管理器中,该设备被检测为通用串行总线设备 如图所示
  • 最后,按照此说明更新设备驱动程序,使用之前创建的驱动程序。

驱动安装成功,并且regedit反映该设备现在正在使用WinUSB driver updated

遗憾的是,当我尝试从网页打印时,仍然会收到"DOMException:拒绝访问。"错误...所以我仍然需要帮助:(

附注:这可能是我所知道的stackoverflow上最长的帖子(至少在前十位)。

更新 03/06/2018

根据Reilly Grant的要求,我包括了USBView的输出(类似于Windows的lsusb)

[Port1]  :  Aeropost Zebra LP2844


Is Port User Connectable:         yes
Is Port Debug Capable:            no
Companion Port Number:            10
Companion Hub Symbolic Link Name: USB#ROOT_HUB30#4&4f0abe8&0&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
Protocols Supported:
 USB 1.1:                         yes
 USB 2.0:                         yes
 USB 3.0:                         no

Device Power State:               PowerDeviceD0

       ---===>Device Information<===---
*!*ERROR:  No String Descriptor for index 4!

ConnectionStatus:                  
Current Config Value:              0x01  -> Device Bus Speed: Full (is not SuperSpeed or higher capable)
Device Address:                    0x01
Open Pipes:                           2

          ===>Device Descriptor<===
bLength:                           0x12
bDescriptorType:                   0x01
bcdUSB:                          0x0100
bDeviceClass:                      0x07
*!*ERROR:  unknown bDeviceClass 7
bDeviceSubClass:                   0x01
*!*ERROR:  bDeviceSubClass of 1 is invalid
bDeviceProtocol:                   0x02
*!*ERROR:  bDeviceProtocol of 2 is invalid
bMaxPacketSize0:                   0x08 = (8) Bytes
idVendor:                        0x0A5F = Zebra Technologies
idProduct:                       0x0009
bcdDevice:                       0x0001
iManufacturer:                     0x02
*!*ERROR:  No String Descriptor for index 2!
iProduct:                          0x04
*!*ERROR:  No String Descriptor for index 4!
iSerialNumber:                     0x06
*!*ERROR:  No String Descriptor for index 6!
bNumConfigurations:                0x01

          ---===>Open Pipes<===---

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x85  -> Direction: IN - EndpointID: 5
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x06  -> Direction: OUT - EndpointID: 6
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

       ---===>Full Configuration Descriptor<===---

          ===>Configuration Descriptor<===
bLength:                           0x09
bDescriptorType:                   0x02
wTotalLength:                    0x0020  -> Validated
bNumInterfaces:                    0x01
bConfigurationValue:               0x01
iConfiguration:                    0x00
bmAttributes:                      0xC0  -> Self Powered
  -> Bus Powered
MaxPower:                          0x00 =   0 mA

          ===>Interface Descriptor<===
bLength:                           0x09
bDescriptorType:                   0x04
bInterfaceNumber:                  0x00
bAlternateSetting:                 0x00
bNumEndpoints:                     0x02
bInterfaceClass:                   0x07  -> This is a Printer USB Device Interface Class
bInterfaceSubClass:                0x01
bInterfaceProtocol:                0x02
iInterface:                        0x00

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x85  -> Direction: IN - EndpointID: 5
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x06  -> Direction: OUT - EndpointID: 6
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

有趣的是(实际上很奇怪),当我试图获取这个新信息时,我的Google Chrome开始抛出错误。
TypeError: Cannot read property 'getDevices' of undefined

控制台出现错误,并且在控制台中键入navigator.usb返回undefined

由于这没有任何意义,我重新安装了Chrome,但错误仍然存在。

因此,我安装了Chrome Canary(其中navigator.usb存在),结果打印机开始打印,访问被拒绝的错误消失了。

我卸载了Chrome Canary,不知何故GA Chrome也起作用了。直到我关闭Chrome并再次打开它。然后我再次收到“无法读取未定义的属性'getDevices'”错误。

我注意到删除AppData / Local / Google中Chrome的用户数据文件夹(整个配置文件文件夹)会使Chrome再次工作,直到我将其关闭并再次打开它。

也许我的打印代码中的某些内容仍然保留着某些东西,尽管我正在调用releaseInterface和close?

有什么想法吗,现在问题似乎有点改变了?


你能否在遇到“Access denied.”错误时,包含lsusb -v命令的输出以及chrome://device-log中显示的错误信息。 - Reilly Grant
@ReillyGrant 我已经相应地更新了问题... 但是情况似乎有些变化。我非常感谢您能快速查看并分享您的想法。 - jmrodriguez
原来getDevices错误是由于navigator.usb未定义引起的,这是一个已报告并详细解释的错误[https://bugs.chromium.org/p/chromium/issues/detail?id=819197](在此处)。该问题发生在Chrome 64.0.3282.186中(我正在使用的版本)。 “解决方案”是使用“--enable-features = WebUSB”标志启动Chrome。 - jmrodriguez
这是错误报告链接 https://bugs.chromium.org/p/chromium/issues/detail?id=819197。另一个“解决方案”是将Chrome升级到65.X.X.X版本。 - jmrodriguez
2个回答

6

在其他Windows机器上进行了几次测试后,我使用 Zadig 软件替换了Windows安装的驱动程序,使一切正常运作。

关键是在替换驱动程序后重新启动计算机。

虽然我开发的自定义驱动程序也有效,但是Zadig的程序使事情变得更加容易 :)


4
也许你可以在这里使用我的 WebUSB 测试工具进行快速测试:https://larsgk.github.io/webusb-tester/,它应该会显示系统中已声明的接口(如果这是导致失败的原因的话)。

谢谢分享你的测试工具。非常有用。请查看上面的最新更新。 - jmrodriguez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接