I am trying to access a device in the device manager. (The device exposes some functionality, only after getting the handle I could perform IOCTL calls)
from the C++ app, I was able to get device details with the help of GUID.
I am getting the device path from SetupDiGetDeviceInterfaceDetail(). but with the device path, when I try to open using createfile / createFileA I am getting error code as 5, which is access denied.
I am in admin mode only. Below is the sample code.
#pragma comment(lib,"Setupapi.lib")
#ifdef __cplusplus
extern "C" {
#endif
#include <windows.h>
#include <tchar.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
class __declspec(uuid("{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}")) guid;
GUID W_GUID = __uuidof(guid);
int main()
{
HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA DevIntfData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DevIntfDetailData;
SP_DEVINFO_DATA DevData;
hDevInfo = SetupDiGetClassDevs(
&W_GUID, NULL, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hDevInfo != INVALID_HANDLE_VALUE)
{
DevIntfData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
dwMemberIdx = 0;
bool result = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &W_GUID ,
dwMemberIdx, &DevIntfData);
DevData.cbSize = sizeof(DevData);
SetupDiGetDeviceInterfaceDetail(
hDevInfo, &DevIntfData, NULL, 0, &dwSize, NULL);
DevIntfDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
DevIntfDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevIntfData,
DevIntfDetailData, dwSize, &dwSize, &DevData);
HANDLE _hDevice = CreateFile((LPCWSTR)DevIntfDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (_hDevice == INVALID_HANDLE_VALUE)
{
int err = GetLastError();
if (err != 0)
{
std::cout << "error";
}
std::cout << _hDevice;
std::cout << "Error ";
}
SetupDiDestroyDeviceInfoList(_hDevice);
}
}
}