ahm_zahran
ahm_zahran

Reputation: 101

Visual Studio cannot recognize connected USB device (Nikon D3100 DSLR)

I followed the instructions in Microsoft website to create a desktop app for connecting to a USB device using WinUSB library functions. However, every time I run the code, the software is unable to recognize any connected devices although I have changed the device driver to WinUSB using Zadig and changed MyDriver.inf Device Name to the recommended value obtained from device manager > properties > details > hardware IDs. here is my code:

#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
}

and here is MyDriver.inf code:


;
; 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




I tried to search for device properties in device manager and implement the values inside MyDriver.inf file and searched for causes that might make Visual Studio unable to not detect USB connected devices. I was expecting the code to find hr value is true which means a device was found and connected but whatever I do it recognize it as false value and print `Device not connected or driver not installed\n. Any suggestion what I have to do to let the software recognize my connected device?

Upvotes: 0

Views: 390

Answers (1)

GLARKI
GLARKI

Reputation: 81

Solution to a deployment problem on the HoloLens 2, might be usefull here.

In visual studio,

  1. Go to tools
  2. -> Get tools and features (or use the ctrl+Q search function and type tools and features, or use the Visual Studio installer)
  3. Modify
  4. Individual components
  5. Install USB Device Connectivity (type USB and search)
  6. Even if already installed, reinstall it by pressing modify.

Tested for VS2019 and VS2022

Of course installing the Microsoft SDK and updating USB drivers should also be checked.

Upvotes: 0

Related Questions