开发者

Problem with detecting USB device using PID and VID functions in VC++

开发者 https://www.devze.com 2023-03-09 16:11 出处:网络
I am trying to develop an API to detect if a USB device has been connected to my USB port. I am using MAX3420e as my USB device which is controlled through a MSP430 uC.

I am trying to develop an API to detect if a USB device has been connected to my USB port. I am using MAX3420e as my USB device which is controlled through a MSP430 uC. I have little to zero knowledge of VC++, and I have been reading USB complete which mentions a sample code on how to detect a USB device and the functions associated with it... I have written a small code which would look for the PID and VID of the connected USB devices... however, I am getting the following error:

error LNK2019: unresolved external symbol "bool __cdecl HidD_GetAttributes(void *,struct _HIDD_ATTRIBUTES *)" (?HidD_GetAttributes@@YA_NPAXPAU_HIDD_ATTRIBUTES@@@Z) referenced in function _wmain

can anyone please let me know where I would be going wrong?? thanks...

// usb-complete-3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "winusb.h"
#include "Usb100.h"
#include "Setupapi.h"

HANDLE  hDevInfo;
GUID    HidGuid;

SP_DEVICE_INTERFACE_DATA            devInfoData;
int MemberIndex;
LONG Result;
//SP_DEVICE_INTERFACE_DATA          devInfoData;
HANDLE                              DeviceHandle;
ULONG                               Length;
ULONG                               Required;
PSP_DEVICE_INTERFACE_DETAIL_DATA    detailData;

typedef struct _HIDD_ATTRIBUTES {
ULONG Size;
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber; }
HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;


void HidD_GetHidGuid(OUT LPGUID HidGuid )
{};

void *SetupDiGetClassDevs(
IN LPGUID ClassGuid, OPTIONAL
IN PCTSTR Enumerator, OPTIONAL
IN HWND hwndParent, OPTIONAL
IN DWORD Flags){return (0);}



void *SetupDiEnumDeviceInterfaces(
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL
IN LPGUID InterfaceClassGuid, 
IN DWORD MemberIndex,
OUT PSP_DEVICE_INTERFACE_DATA
DeviceInterfaceData ){return (0);}


bool
HidD_GetAttributes(IN HANDLE HidDeviceObject,OUT PHIDD_ATTRIBUTES Attributes) ;


WINSETUPAPI
BOOL
WINAPI
  SetupDiGetDeviceInterfaceDetail(
    IN HDEVINFO  DeviceInfoSet,
    IN PSP_DEVICE_INTERFACE_DATA  DeviceInterfaceData,
    OUT PSP_DEVICE_INTERFACE_DETAIL_DATA  DeviceInterfaceDetailData,  OPTIONAL
    IN DWORD  DeviceInterfaceDetailDataSize,
    OUT PDWORD  RequiredSize,  OPTIONAL
    OUT PSP_DEVINFO_DATA  DeviceInfoData  OPTIONAL
    );


int _tmain(int argc, _TCHAR* argv[])
{
    while(1)
    {
        HIDD_ATTRIBUTES Attributes;

        HidD_GetHidGuid(&HidGuid);

        hDevInfo=SetupDiGetClassDevs (&HidGuid,NULL, NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); //get the device information

        devInfoData.cbSize = sizeof(devInfoData);

        SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,MemberIndex,&devInfoData); // check for the enumerated devices

        // The call will return with a "buffer too small" error which can be ignored.

        Result = SetupDiGetDeviceInterfaceDet开发者_运维问答ail(hDevInfo,&devInfoData,NULL,0,&Length,NULL);// Allocate memory for the hDevInfo structure, using the returned Length. 

        detailData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);

        // Set cbSize in the detailData structure.
        detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);// Call the function again, this time passing it the returned buffer size.

        Result = SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,detailData,Length,&Required, NULL);

        // Set the Size member to the number of bytes in the structure.
        Attributes.Size = sizeof (Attributes) ;

        HidD_GetAttributes(DeviceHandle,&Attributes);  // get the PID and VID of the devices connected

    }
    return 0;
}


Put setupapi.lib as additional dependacy in Linker input tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消