In terms of Windows is not strong, so I apologize in advance for the possible inaccuracy of the names.

Description of the problem:

There is a network card device file that is located at. \ Global {some-windows-hash} .tap

The file opens like this

std::wstring userModeDevice = L"\\\\.\\Global\\"; userModeDevice += WinRegReader::getDeviceGuid() + L".tap"; _fileHandle = CreateFile ( userModeDevice.c_str(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM|FILE_FLAG_OVERLAPPED, nullptr); 

Further, through DeviceIoControl () with the file the necessary manipulations are performed. It's all ok, and it works.

But I really want to get a file handle from _fileHandle, which is of type HANDLE - int (for such functions to work like read / write).

There are many examples of how this can be done, of which approximately this follows.

 int fd = _open_osfhandle((intptr_t)_fileHandle, O_RDWR); 

for example, here , but they all refer to user-space files, but if "\\. \ Global \" is used, the function returns -1, and errno is a constant corresponding to the text "Invalid argument".

Tell me how you can get an int handle from HANDLE (\\. \ Global \ ...), if at all possible. Thank.

  • -1 means an error has occurred. What code then turns out to be in errno? - Pavel Mayorov
  • By the way, are you sure that the device basically allows you to work with yourself via read / write? - Pavel Mayorov
  • the device allows (this is a network adapter), errno == 22 - Ilya Lesnoy
  • Well, look for that kind of mistake - 23 - Pavel Mayorov
  • so I also wrote that this error is an "invalid argument", i.e. o_rdwr is not suitable. o_rdonly did not fit either. where to dig further, I xs - Ilya Forest

0