CreateFile / CreatefileA getlasterror returns 5

Hari 21 Reputation points
2022-05-13T09:53:39.85+00:00

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);
}
}
}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,414 questions
{count} votes

Accepted answer
  1. RLWA32 40,276 Reputation points
    2022-05-17T07:43:53.393+00:00

    So it would appear that the security descriptor for your device only permits access to the SYSTEM account.
    You can test this by using PsExec to open a command prompt window that is running under the SYSTEM account. In that command prompt window run your application.

    To accomplish this -

    1. Install PsExec
    2. Open a command prompt window with elevated privileges that is running as an Administrator.
    3. In the elevated command prompt run the command "psexec -s -i -d cmd"
    4. A new command prompt window will open that is running as SYSTEM. You can confirm with the whoami command.
    5. In the SYSTEM command prompt window run your application.
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful