How to get a windows HANDLE from a QFile or QSaveF

2019-07-13 15:46发布

Given a QFileDevice instance (a QFile, or QSaveFile) - how would one get the native Windows HANDLE of the file? And can this handle be used with ReOpenFile?

1条回答
在下西门庆
2楼-- · 2019-07-13 16:34

The QFileDevice::handle() returns a C file descriptor (fd - a small integer) obtained from QFSFileEnginePrivate::nativeHandle. That file descriptor is what you'd get from _open_osfhandle. You need to use _get_osfhandle to go back to a HANDLE.

struct FdHandle {
  int fd = -1;
  HANDLE handle = INVALID_HANDLE_VALUE;
  operator HANDLE() const { return handle; }
  bool isFdValid() const { return fd != -1; }
  bool isHandleValid() const { return handle != INVALID_HANDLE_VALUE; }
};

FdHandle windowsHandleOf(QFileDevice* dev) {
  auto fd = dev->handle(); 
  return {fd, (HANDLE)_get_osfhandle(fd)};
}

ReOpenFile may return a different file handle, and thus you potentially need to reopen the file for it. An overloaded ReOpenFile handles it:

struct ReOpenResult {
  enum { OK = 0, FileClosed = 10, NoHandle = 20, 
         OldHandle = 30, OpenFailed = 40 } rc;
  HANDLE handle = INVALID_HANDLE_VALUE;
  operator HANDLE() const { return handle; }
  explicit operator bool() const { return rc == OK; }
  bool isHandleValid() const { return handle != INVALID_HANDLE_VALUE; }
};

ReOpenResult ReOpenFile(QFile *file, DWORD access, DWORD shareMode, DWORD flags) {
  if (!file->isOpen())
    return {ReOpenResult::FileClosed};
  auto oldHandle = windowsHandleOf(file);
  if (oldHandle == INVALID_HANDLE_VALUE)
    return {ReOpenResult::NoHandle};
  auto newHandle = ReOpenFile(oldHandle, access, shareMode, flags);
  if (newHandle == INVALID_HANDLE_VALUE)
    return {ReOpenResult::OldHandle, oldHandle};
  if (!open(file, file->openMode(), newHandle))
    return {ReOpenResult::OpenFailed, oldHandle};
  return {ReOpenResult::OK, newHandle};
}

The "missing" open that takes a HANDLE is:

struct OpenResult {
  enum { OK = 0, SameHandleOK = 1, InvalidHandle = 10, CCloseFailed = 20,
         InvalidFd = 30, OpenFailed = 40 } rc = OK;
  explicit operator bool() const { return rc < InvalidHandle; };
};

OpenResult open(QFile* file, QIODevice::OpenMode mode, HANDLE handle) {
  if (handle == INVALID_HANDLE_VALUE)
    return {OpenResult::InvalidHandle};
  if (file->isOpen() && windowsHandleOf(file) == handle)
    return {OpenResult::SameHandleOK};
  file->close();
  if (auto fd = file->handle() != -1)
    if (_close(fd)) // the C handle is still open, don't leak it
      return {OpenResult::CCloseFailed};
  int flags = 0;
  if (mode & QIODevice::Append)
    flags |= _O_APPEND;
  if (!(mode & QIODevice::WriteOnly))
    flags |= _O_RDONLY;
  auto fd = _open_osfhandle((intptr_t)handle, flags);
  if (fd == -1)
    return {OpenResult::InvalidFd};
  if (!file->open(fd, mode, QFileDevice::AutoCloseHandle))
    return {OpenResult::OpenFailed};
  return {};
}
查看更多
登录 后发表回答