Czy ktoś może sprawdzić, czy robię to prawo należy:Moi SetupDiEnumDeviceInterfaces nie działa
//DeviceManager.h
#include <windows.h>
//#include <hidsdi.h>
#include <setupapi.h>
#include <iostream>
#include <cfgmgr32.h>
#include <tchar.h>
#include <devpkey.h>
extern "C"{
#include <hidsdi.h>
}
//#pragma comment (lib, "setupapi.lib")
class DeviceManager
{
public:
DeviceManager();
~DeviceManager();
void ListAllDevices();
void GetDevice(std::string vid, std::string pid);
HANDLE PSMove;
byte reportBuffer;
private:
HDEVINFO deviceInfoSet; //A list of all the devices
SP_DEVINFO_DATA deviceInfoData; //A device from deviceInfoSet
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData;
};
//DeviceManager.cpp
#include"DeviceManager.h"
DeviceManager::DeviceManager()
{
deviceInfoSet = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); //Gets all Devices
}
DeviceManager::~DeviceManager()
{
}
void DeviceManager::ListAllDevices()
{
DWORD deviceIndex = 0;
deviceInfoData.cbSize = sizeof(deviceInfoData);
while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
{
deviceInfoData.cbSize = sizeof(deviceInfoData);
ULONG tcharSize;
CM_Get_Device_ID_Size(&tcharSize, deviceInfoData.DevInst, 0);
TCHAR* deviceIDBuffer = new TCHAR[tcharSize]; //the device ID will be stored in this array, so the tcharSize needs to be big enough to hold all the info.
//Or we can use MAX_DEVICE_ID_LEN, which is 200
CM_Get_Device_ID(deviceInfoData.DevInst, deviceIDBuffer, MAX_PATH, 0); //gets the devices ID - a long string that looks like a file path.
/*
//SetupDiGetDevicePropertyKeys(deviceInfoSet, &deviceInfoData, &devicePropertyKey, NULL, 0, 0);
if(deviceIDBuffer[8]=='8' && deviceIDBuffer[9]=='8' && deviceIDBuffer[10]=='8' && deviceIDBuffer[11]=='8' && //VID
deviceIDBuffer[17]=='0' && deviceIDBuffer[18]=='3' && deviceIDBuffer[19]=='0' && deviceIDBuffer[20]=='8') //PID
{
std::cout << deviceIDBuffer << "\t<-- Playstation Move" << std::endl;
}
else
{
std::cout << deviceIDBuffer << std::endl;
}*/
std::cout << deviceIDBuffer << std::endl;
deviceIndex++;
}
}
void DeviceManager::GetDevice(std::string vid, std::string pid)
{
DWORD deviceIndex = 0;
deviceInfoData.cbSize = sizeof(deviceInfoData);
while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
{
deviceInfoData.cbSize = sizeof(deviceInfoData);
ULONG IDSize;
CM_Get_Device_ID_Size(&IDSize, deviceInfoData.DevInst, 0);
TCHAR* deviceID = new TCHAR[IDSize];
CM_Get_Device_ID(deviceInfoData.DevInst, deviceID, MAX_PATH, 0);
if(deviceID[8]==vid.at(0) && deviceID[9]==vid.at(1) && deviceID[10]==vid.at(2) && deviceID[11]==vid.at(3) && //VID
deviceID[17]==pid.at(0) && deviceID[18]==pid.at(1) && deviceID[19]==pid.at(2) && deviceID[20]==pid.at(3)) //PID
{
//DWORD requiredBufferSize;
//SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &requiredBufferSize,
HDEVINFO deviceInterfaceSet = SetupDiGetClassDevs(&deviceInfoData.ClassGuid, NULL, NULL, DIGCF_ALLCLASSES);
DWORD deviceInterfaceIndex = 0;
deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
while(SetupDiEnumDeviceInterfaces(deviceInterfaceSet, NULL, &deviceInterfaceData.InterfaceClassGuid, deviceInterfaceIndex, &deviceInterfaceData))
{
deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
std::cout << deviceInterfaceIndex << std::endl;
deviceInterfaceIndex++;
}
//std::cout << deviceInterfaceData.cbSize << std::endl;
break;
}
deviceIndex++;
}
}
Moje SetupDiEnumDeviceInterfaces (w funkcji GetDevice()) nie robi nic. Nawet nie wchodzi w pętlę while ... Czy ktoś może spróbować wskazać, co robię źle, proszę?
Dzięki
Edycja/Więcej informacji: Właśnie zwane funkcję GetLastError() i to zwróciło 259 - ERROR_NO_MORE_ITEMS. Czy urządzenie może nawet nie zawierać żadnych interfejsów?
'SetupDiEnumDeviceInfo' powraca fałszywa wtedy. Przejrzyj dokumentację, aby ustalić, dlaczego tak się dzieje, a następnie użyj swojego debuggera. Jeśli nadal nie możesz tego rozgryźć, zadaj bardziej szczegółowe pytanie o to, gdzie utknąłeś. – AJG85
Następnie proszę zapytać, czy deviceInfoData.ClassGuid zostało poprawnie przekazane? Mam na myśli, że jestem suppsem, aby przekazać ten przewodnik, aby uzyskać urządzenie InterfaceSet? Czy też mogę przekazać mu inny identyfikator GUID? – Danny
Jeśli nigdy nie wprowadzisz pętli while, nigdy nie dojdziesz do tego wiersza kodu, co oznacza, że używasz 'deviceInfoSet' przypisanego w konstruktorze. Wskazówka: Użyj 'FormatMessage' z' GetLastError', aby dokładnie określić, co poszło nie tak po wywołaniu 'SetupDiEnumDeviceInfo' Może to oznaczać, że w systemie nie ma obecnie aktywnych aktywnych urządzeń USB. – AJG85