Visual Studio 无法识别连接的 USB 设备(Nikon D3100 DSLR)

问题描述 投票:0回答:1

我按照 Microsoft 网站上的说明创建了一个桌面应用程序,用于使用 WinUSB 库函数连接到 USB 设备。但是,每次运行代码时,尽管我已使用 Zadig 将设备驱动程序更改为 WinUSB,并将 MyDriver.inf 设备名称更改为从设备管理器 > 属性 > 详细信息 > 硬件中获取的推荐值,但该软件无法识别任何连接的设备身份证。这是我的代码:

#include "pch.h"

#include <iostream>
#include <stdio.h>
using namespace std;
LONG __cdecl
_tmain(
    LONG     Argc,
    LPTSTR * Argv
    )
/*++

Routine description:

    Sample program that communicates with a USB device using WinUSB

--*/
{
    DEVICE_DATA           deviceData;
    HRESULT               hr;
    USB_DEVICE_DESCRIPTOR deviceDesc;
    BOOL                  bResult;
    BOOL                  noDevice;
    ULONG                 lengthReceived;

    UNREFERENCED_PARAMETER(Argc);
    UNREFERENCED_PARAMETER(Argv);
    //system ("pause");
    //
    // Find a device connected to the system that has WinUSB installed using our
    // INF
    //
    hr = OpenDevice(&deviceData, &noDevice);
    //cout << hr << endl;
    //system ("pause");
    if (FAILED(hr)) {

        if (noDevice) {

            wprintf(L"Device not connected or driver not installed\n");

        } else {

            wprintf(L"Failed looking for device, HRESULT 0x%x\n", hr);
        }

        return 0;
    }
    
    //
    // Get device descriptor
    //
    
    bResult = WinUsb_GetDescriptor(deviceData.WinusbHandle,
                                   USB_DEVICE_DESCRIPTOR_TYPE,
                                   0,
                                   0,
                                   (PBYTE) &deviceDesc,
                                   sizeof(deviceDesc),
                                   &lengthReceived);
    
    
    
    if (FALSE == bResult || lengthReceived != sizeof(deviceDesc)) {

        wprintf(L"Error among LastError %d or lengthReceived %d\n",
                FALSE == bResult ? GetLastError() : 0,
                lengthReceived);
        CloseDevice(&deviceData);
        return 0;
    }
    ;
    
    
    //
    // Print a few parts of the device descriptor
    //
    wprintf(L"Device found: VID_%04X&PID_%04X; bcdUsb %04X\n",
            deviceDesc.idVendor,
            deviceDesc.idProduct,
            deviceDesc.bcdUSB);
    
    CloseDevice(&deviceData);
    return 0;
    
    //hr = WinUsb_GetDescriptor
}

这是 MyDriver.inf 代码:


;
; MyDriver.inf
;
; Installs WinUsb
;

[Version]
Signature = "$Windows NT$"
Class     = USBDevice
ClassGUID = {88BAE032-5A81-49f0-BC3D-A4FF138216D6}
Provider = Nikon Corp.
CatalogFile=MyDriver.cat
PnpLockDown=1

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

[Manufacturer]
%ManufacturerName% = Nikon Corp.,NT$ARCH$

[Standard.NT$ARCH$]
%DeviceName% =USB_Install, USB\VID_04B0&PID_0427&REV_0101




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

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

[USB_Install.Services]
Include=winusb.inf
AddService=WinUsb,0x00000002,WinUsb_ServiceInstall

[WinUsb_ServiceInstall]
DisplayName     = %WinUsb_SvcDesc%
ServiceType     = 1
StartType       = 3
ErrorControl    = 1
ServiceBinary   = %12%\WinUSB.sys

[USB_Install.HW]
AddReg=Dev_AddReg

[Dev_AddReg]
; By default, USBDevice class uses iProduct descriptor to name the device in
; Device Manager on Windows 8 and higher.
; Uncomment for this device to use %DeviceName% on Windows 8 and higher:
;HKR,,FriendlyName,,%DeviceName%
HKR,,DeviceInterfaceGUIDs,0x10000,"{421ba06a-3521-46c0-b76e-dc9a04f79ad1}"

[USB_Install.CoInstallers]
AddReg=CoInstallers_AddReg
CopyFiles=CoInstallers_CopyFiles

[CoInstallers_AddReg]
HKR,,CoInstallers32,0x00010000,"WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll,WdfCoInstaller"

[CoInstallers_CopyFiles]
WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll

[DestinationDirs]
CoInstallers_CopyFiles=11

; ================= Source Media Section =====================

[SourceDisksNames]
1 = %DiskName%

[SourceDisksFiles]
WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll=1

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

[Strings]
ManufacturerName="<Your manufacturer name>" ;TODO: Replace with your manufacturer name
ClassName="Universal Serial Bus devices"
DiskName="MyDriver Installation Disk"
WinUsb_SvcDesc="WinUSB Driver"
DeviceName="MyDriver Device"
REG_MULTI_SZ = 0x00010000




我尝试在设备管理器中搜索设备属性并实现 MyDriver.inf 文件中的值,并搜索可能导致 Visual Studio 无法检测到 USB 连接设备的原因。 我期望代码找到的 hr 值为 true,这意味着找到并连接了设备,但无论我做什么,它都会将其识别为 false 值并打印“设备未连接或驱动程序未安装” 。有什么建议我必须做什么才能让软件识别我连接的设备吗?

c++ visual-studio usb connectivity winusb
1个回答
0
投票

HoloLens 2 上的部署问题的解决方案在这里可能有用。

在视觉工作室中,

  1. 转到工具
  2. -> 获取工具和功能(或使用 ctrl+Q 搜索功能并键入工具和功能,或使用 Visual Studio 安装程序)
  3. 修改
  4. 单独组件
  5. 安装 USB 设备连接(键入 USB 并搜索)
  6. 即使已经安装,请按修改重新安装。

针对 VS2019 和 VS2022 进行了测试

当然还应该检查安装Microsoft SDK和更新USB驱动程序。

© www.soinside.com 2019 - 2024. All rights reserved.