I want to open a handle to my camera device using CreateFile(), but I do not have the device path.
I know I can get the device path with the SetupDiGetDeviceInterfaceDetail inside SetupDiEnumDeviceInterfaces. However I have a problem getting the interfaces for the camera devices.
I for example tried to use SetupDiGetClassDevs with KEYBOARD GUID, it works perfectly. I got the interfaces data and associated device paths. It also works for several GUID.
The problem appears with some GUID such as Camera (the one I want) . Apparently my camera device doesn't have any interface.
Is there a way to get a device path directly within the SetupDiEnumDeviceInfo loop?
Or what parameter from the SetupDiGetDeviceRegistryProperty can I pass to CreateFile to get a valid handle?
int getDeviceHandlesByClass(WCHAR* inputClassName)
{
HDEVINFO hDevInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i = 0;
hDevInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (hDevInfoSet == INVALID_HANDLE_VALUE)
{
std::cout << "Error on SetupDiGetClassDevs" << std::endl;
exit(0);
}
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
while (SetupDiEnumDeviceInfo(hDevInfoSet, i, &DeviceInfoData))
{
i++;
WCHAR deviceClassName[256];
if (!SetupDiGetDeviceRegistryProperty(hDevInfoSet, &DeviceInfoData, SPDRP_CLASS, NULL, (BYTE*)deviceClassName, sizeof(deviceClassName), NULL))
{
return 1;
}
if (wcscmp(deviceClassName, inputClassName) == 0)
{
std::wcout << "PDO: " << deviceClassName << std::endl;
}
DWORD size;
WCHAR instanceID[256];
if (SetupDiGetDeviceInstanceId(hDevInfoSet, &DeviceInfoData, instanceID, 256, &size))
{
std::wcout << "INSTANCE ID: " << instanceID << std::endl;
}
HANDLE hFile = CreateFile(L"???WHAT SHALL I PASS HERE???", // file path
GENERIC_READ, // desired access
FILE_SHARE_READ, // share mode
NULL, // security attributes
OPEN_EXISTING, // creation disposition
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL); // template file
if (hFile == INVALID_HANDLE_VALUE)
{
// Handle error
cout << GetLastError() << endl;
}
else {
cout << "test" << endl;
}
// Close the file handle
CloseHandle(hFile);
}
SetupDiDestroyDeviceInfoList(hDevInfoSet);
}