0

I have asked a similar question previously, but 1. no one answered and 2. it is a bit different. I have managed to get the correct file Path and created a handler by using CreateFile. I have then tried to use the HidD_GetFeature() function, but when I print out the report, it is all in symbols - not letters, numbers or readable sign. Does anyone know why it is doing that?? Here's my code:

/*****************************Mainframe.cpp**************************************/
#include"DeviceManager.h"

int main()
{
    int iQuit;
    DeviceManager deviceManager;

    //deviceManager.ListAllDevices();
    deviceManager.GetDevice("8888", "0308");

    std::cin >> iQuit;

    return 0;
}

/***********************************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[57];
    GUID guid;
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()
{
    HidD_GetHidGuid(&guid);
    deviceInfoSet = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); //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 deviceInterfaceIndex = 0;
            deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);

            //HDEVINFO deviceInterfaceSet = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE);

            if(SetupDiEnumDeviceInterfaces(deviceInfoSet, &deviceInfoData, &guid, deviceInterfaceIndex, &deviceInterfaceData))
            {
                deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
                deviceInterfaceDetailedData.cbSize = sizeof(deviceInterfaceDetailedData);

                DWORD requiredSize;

                SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &requiredSize, &deviceInfoData); //Gets the size
                SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, &deviceInterfaceDetailedData, requiredSize, NULL, NULL); //Sets the deviceInterfaceDetailedData

                //std::cout << deviceInterfaceDetailedData.DevicePath << std::endl;

                PSMove = CreateFile(deviceInterfaceDetailedData.DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

                reportBuffer[0] = 0;
                ULONG reportBufferLength = sizeof(reportBuffer);
                HidD_GetFeature(PSMove, &reportBuffer, reportBufferLength);
                std::cout << reportBuffer << std::endl;
                //deviceInterfaceIndex++;
            }
            break;
        }

        deviceIndex++;
    }
}

the HidD_GetFeature() function is at the bottom of this huge block of code.

UPDATE: I've managed to print the report now, but it is just a random 8 bit hex. Everytime I close the program down and run it again, it prints out different results. Why's that?

Danny
  • 9,199
  • 16
  • 53
  • 75
  • 1
    It might be better to close this as a duplicate of [My SetupDiEnumDeviceInterfaces is not working](http://stackoverflow.com/questions/10587914/my-setupdienumdeviceinterfaces-is-not-working) and instead focus everyone's energies on figuring out how to improve and salvage *that* question. – Kerrek SB May 14 '12 at 21:57
  • @KerrekSB : Your're not looking back far enough -- [Why is SetupDiGetDeviceProperty function not working?](http://stackoverflow.com/questions/10574137/why-is-setupdigetdeviceproperty-function-not-working) – ildjarn May 14 '12 at 22:13
  • @ildjarn aint you smart... thats been solved already – Danny May 14 '12 at 22:25
  • @Danny : And so has this one, ostensibly. And yet I'd wager you'll be back with yet another mostly-similarly-titled question soon. Hmm... – ildjarn May 14 '12 at 22:27
  • @ildjarn and what if I do...hmm... Not trying to be funny or rude, and I'm may not be as smart as you or the others, but if I don't get or understand something then will seek for help... – Danny May 14 '12 at 22:30
  • @Danny : And if you don't ask sensible questions you won't get sensible answers. This sounds harsh, maybe, but you need to put more effort in on your questions. – ildjarn May 14 '12 at 22:32
  • @ildjarn seriously, so what is sensible questions to you then? I mean like, Kerrek said I better close this one and focus on the other post I've made, which no one solved and really didn't give me any ideas how to solve, but I managed to solve it myself (hence why I didn't "tick" a answer and posted a new one here) and now you're saying i'm not putting enough effort in the questions? :S... what the hell man – Danny May 14 '12 at 22:38
  • @Danny : Use some critical thinking -- people who are answering here _want_ to answer here. There's no other compensation. If you ask a question and _don't_ get an answer, that's usually because the question lacks effort. Do with that what you will. – ildjarn May 14 '12 at 22:40
  • @ildjarn fair enough, I'll keep that in mind next time – Danny May 14 '12 at 22:41

1 Answers1

1

The ListAllDevices() function isn't displaying the info correctly because you're outputting a wchar_t* to std::cout. You need to use std::wcout for wide chars.

Fraser
  • 74,704
  • 20
  • 238
  • 215
  • @Danny My answer fixes your original problem. If you want to ask a new question, please don't deselect the correct answer and edit your original post - ask a new question. If you *have* to edit your original question, at least show us the new code/errors, while leaving the original question intact for future reference. – Fraser May 15 '12 at 11:15
  • will keep that in mind. Its just in the past I have 'ask a new question' but people were telling me to delete it because the code where pretty much the same as my previous questions. Thats all. Sorry about that. – Danny May 21 '12 at 12:15