title: Program crashes when SetupDiGetDeviceInterfaceDetail is called
body: <p>I'm trying to get the path of a device by using the SetupDiGetDeviceInterfaceDetail() function, but it crashes everytime I call it. I've have been working on this for over 12 hours but still couldn't find out what is wrong with it... Can someone see if they can find what is actually causing this to happen? Heres the code:</p>
  
  <pre><code>//DeviceManager.h
  #include &lt;windows.h&gt;
  //#include &lt;hidsdi.h&gt;
  #include &lt;setupapi.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cfgmgr32.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;devpkey.h&gt;
  #include &lt;string&gt;
  
  extern "C"{
      #include &lt;hidsdi.h&gt;
  }
  
  //#pragma comment (lib, "setupapi.lib")
  
  class DeviceManager
  {
  public:
      DeviceManager();
      ~DeviceManager();
  
      void ListAllDevices();
      void GetDeviceHandler();
  
      //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()
  {
      //deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA;
      //deviceInterfaceDetailedData = new SP_DEVICE_INTERFACE_DETAIL_DATA;
  
      HidD_GetHidGuid(&amp;guid);
      deviceInfoSet = SetupDiGetClassDevs(&amp;guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); //Gets all Devices
      GetDeviceHandler();
  }
  
  DeviceManager::~DeviceManager()
  {
  }
  
  void DeviceManager::ListAllDevices()
  {
      DWORD deviceIndex = 0;
  
      deviceInfoData.cbSize = sizeof(deviceInfoData);
  
      while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &amp;deviceInfoData))
      {
          deviceInfoData.cbSize = sizeof(deviceInfoData);
  
          ULONG tcharSize;
          CM_Get_Device_ID_Size(&amp;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.
  
          std::cout &lt;&lt; deviceIDBuffer &lt;&lt; std::endl;
  
          deviceIndex++;
      }
  }
  
  void DeviceManager::GetDeviceHandler()
  {
       DWORD deviceIndex = 0;
  
  SP_DEVINFO_DATA deviceInfoData;
  SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
  SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData;
  
  deviceInfoData.cbSize = sizeof(deviceInfoData);
  
  while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &amp;deviceInfoData))
  {
      TCHAR deviceID[MAX_DEVICE_ID_LEN];
  
      CM_Get_Device_ID(deviceInfoData.DevInst, deviceID, MAX_DEVICE_ID_LEN, 0);
  
      //std::cout &lt;&lt; deviceID &lt;&lt; std::endl;
  
      deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
      if(SetupDiEnumDeviceInterfaces(deviceInfoSet, &amp;deviceInfoData, &amp;guid, 0, &amp;deviceInterfaceData))
      {
          DWORD bufferLength = 0;
          //deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
          deviceInterfaceData.cbSize = 2048;
          //std::cout &lt;&lt; "it works not" &lt;&lt; std::endl;
  
          if(SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &amp;deviceInterfaceData, NULL, 0, &amp;bufferLength, NULL))
          {
              //deviceInterfaceData.cbSize = sizeof(bufferLength);
              std::cout &lt;&lt; "It works!" &lt;&lt; std::endl;
          }
          else
          {
              std::cout &lt;&lt; GetLastError() &lt;&lt; std::endl;
          }
  
      }
      else
      {
          //std::cout &lt;&lt; GetLastError() &lt;&lt; std::endl;
      }
  
  
      deviceIndex++;
  }
  
  }
  
  //mainapp.cpp
  #pragma once
  
  int main()
  {
  DeviceManager deviceManager;
  
  return 0;
  }
  </code></pre>
  
  <p>The SetupDiGetDeviceInterfaceDetail function is called in the GetDeviceHandler() function of DeviceManager.</p>
  
  <p>Please help. Thanks.</p>
  
  <p>UPDATE: I have found out that it failed on the first SetupDiGetDeviceInterfaceDetail and is returning a 122 error (ERROR_INSUFFICIENT_BUFFER). But I am only trying to get the required buffer size, so how can this be??</p>
  
  <p>UPDATE 2: right, I have changed the function a bit (see above code) by setting the deviceInterfaceData.cbsize to 2048 (a huge space for testing purposes) and now I'm getting a ERROR_INVALID_PARAMETER. This is getting more and more confusing... How can the parameters I've given is invalid? Just doesn't make sense. The only difference is I passed in References instead of Pointers because otherwise I will get a access violation error...</p>