From 0e4c7408596ecad7888cc138046420e7e4ac2bfb Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sun, 3 Sep 2017 22:50:15 +0500 Subject: [PATCH] Add Device.getLastErrorMessage() --- src/ui/util/device.cpp | 19 +++++++++++++++++++ src/ui/util/device.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/ui/util/device.cpp b/src/ui/util/device.cpp index 703be99d..fb09f271 100644 --- a/src/ui/util/device.cpp +++ b/src/ui/util/device.cpp @@ -56,3 +56,22 @@ bool Device::ioctl(int code, char *in, int inSize, return res; } + +QString Device::getLastErrorMessage() +{ + const DWORD err = GetLastError(); + LPWSTR buf = nullptr; + + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER + | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, err, 0, (LPWSTR) &buf, 0, NULL); + + if (!buf) { + return QString("System Error %1").arg(err); + } + + const QString text = QString::fromUtf16((const ushort *) buf).trimmed(); + LocalFree(buf); + return text; +} diff --git a/src/ui/util/device.h b/src/ui/util/device.h index 87f005fa..a1ddf7d9 100644 --- a/src/ui/util/device.h +++ b/src/ui/util/device.h @@ -11,6 +11,8 @@ public: explicit Device(QObject *parent = nullptr); virtual ~Device(); + static QString getLastErrorMessage(); + signals: public slots: