I have spent hours looking through this code trying different things. But to no avail. After running the code below sometimes it will throw me an exception ".exe has triggered a breakpoint.".
After i press continue it will give me "Unhandled exception at 0x00007FFCF935775F (ntdll.dll) in SeekThermalCPP_V3.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFCF93B86B0)."
Then i press continue again and i get my actual results (which are the devicepaths).
I tried setting my own breakpoints and look at values. But they all look fine. I switched between 32 and 64 bit but that didnt do much.
The problem seems to occur in GetDevicePathString(). if i remove it, no problem occurs in the other 1st two methods.
Thank you very much, any help and/or advice would be appreciated! If im not doing something right please tell.
Im trying to obtain a device path. First i find the GUIDs and then i look at the device interfaces. From the SetupDiGetDeviceInterfaceDetail im trying to obtain SP_DEVICE_INTERFACE_DETAIL_DATA and get the DevicePath. I want to use that device path later to send commands to my USB device. Im thinking of using WINUSB API to accomplish that.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <SetupAPI.h>
#include <Cfgmgr32.h>
#include <vector>
//#include <winusb.h>
#pragma comment (lib, "Setupapi.lib")
using namespace std;
vector<GUID> EnumerateAllWinUsbGuids();
void FindUsbDevicePath(GUID inputGUID);
void GetDevicePathString(HDEVINFO DeviceInfoSet, PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
int main()
{
vector<GUID> guid = EnumerateAllWinUsbGuids();
GUID guid0 = guid[0];
GUID guid1 = guid[1];
FindUsbDevicePath(guid0);
FindUsbDevicePath(guid1);
std::getchar();
return 0;
}
vector<GUID> EnumerateAllWinUsbGuids()
{
vector<GUID> dataVECTOR;
HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
PSP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
devInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
for (unsigned int i = 0;; i++)
{
bool checkINFO = SetupDiEnumDeviceInfo(DeviceInfoSet, i, devInfoData);
HKEY hkey = SetupDiOpenDevRegKey(DeviceInfoSet, devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY);
if (!checkINFO)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
RegCloseKey(hkey);
break;
}
wchar_t dataIN[MAX_DEVICE_ID_LEN];
unsigned long outLength = sizeof(dataIN);
LPCWSTR deviceProperty = L"DeviceInterfaceGUIDs";
long checkGET = RegGetValue(hkey, NULL, deviceProperty, RRF_RT_REG_SZ | RRF_RT_REG_MULTI_SZ, NULL, &dataIN, &outLength);
if (checkGET == 0)
{
GUID guid;
CLSIDFromString(dataIN, (LPCLSID)&guid);
dataVECTOR.push_back(guid);
}
}
return dataVECTOR;
}
void FindUsbDevicePath(GUID inputGUID)
{
HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(&inputGUID, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (GetLastError() == 8)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
cout << "LAST ERROR SetupDiGetClassDevs : " << GetLastError() << endl;
unsigned long i = 0;
while (true)
{
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
bool checkINFO = SetupDiEnumDeviceInterfaces(DeviceInfoSet, NULL, &inputGUID, i, &DeviceInterfaceData);
if (!checkINFO)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
break;
}
GetDevicePathString(DeviceInfoSet, &DeviceInterfaceData);
i++;
}
}
void GetDevicePathString(HDEVINFO DeviceInfoSet, PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
{
unsigned long checkSize;
bool check = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, DeviceInterfaceData, NULL, 0, &checkSize, NULL);
cout << "LAST ERROR SetupDiGetDeviceInterfaceDetail 1: " << GetLastError() << endl;
unsigned long trueSize = checkSize;
int** pointerSize;
cout << "size of a pointer : " << sizeof(pointerSize) << endl;
unsigned long structlength = 6;
if (sizeof(pointerSize) == 8) { structlength = 8; }
PSP_DEVICE_INTERFACE_DETAIL_DATA mem = new SP_DEVICE_INTERFACE_DETAIL_DATA();
mem->cbSize = structlength;
check = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, DeviceInterfaceData, mem, trueSize, &checkSize, NULL);
cout << "LAST ERROR SetupDiGetDeviceInterfaceDetail 2: " << GetLastError() << endl;
wstring dataSTR = wstring(mem->DevicePath);
wcout << dataSTR << endl;
}
Found my error.
Fixed by adding the following line to my code,
PSP_DEVICE_INTERFACE_DETAIL_DATA mem = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, size);
I needed to allocate a certain amount of bytes of memory. The reason it would break is because not enough memory was allocated using the "new"; that is my assumption. If anyone knows why this fixed the problem please comment down below to give more information.
The example from the link below helped me in finding the solution.
https://msdn.microsoft.com/en-us/library/windows/desktop/bb204769(v=vs.85).aspx
User contributions licensed under CC BY-SA 3.0