Changeset View
Changeset View
Standalone View
Standalone View
src/solid/devices/backends/win/winutils_p.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2016 Kevin Funk <kfunk@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef WINUTILS_P_H | ||||
22 | #define WINUTILS_P_H | ||||
23 | | ||||
24 | #include "windevicemanager.h" // for qGetLastError | ||||
25 | | ||||
26 | #include <qt_windows.h> | ||||
27 | | ||||
28 | /** | ||||
29 | * @brief RAII class for blocking Windows from reporting errors | ||||
30 | * | ||||
31 | * Internally calls GetThreadErrorMode + SetErrorMode. This class stores the | ||||
32 | * current error mode on construction and disables certain error reportings | ||||
33 | * during the life time of this object | ||||
34 | * | ||||
35 | * @code | ||||
36 | * { | ||||
37 | * WinErrorBlocker blocker; // custom error mode is set | ||||
38 | * // Your Windows API calls... | ||||
39 | * } // end of scope: original error mode is restored | ||||
40 | * @endcode | ||||
41 | * | ||||
42 | * Note that qstorageinfo_win.cpp (qtbase) does something similar to silence errors | ||||
43 | * | ||||
44 | * See MSDN documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/dd553630(v=vs.85).aspx | ||||
45 | */ | ||||
46 | class WinErrorBlocker | ||||
47 | { | ||||
48 | public: | ||||
49 | WinErrorBlocker() | ||||
50 | { | ||||
51 | if (!::SetThreadErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX, &m_oldmode)) { | ||||
52 | qWarning() << "Failed to call SetThreadErrorMode:" << qGetLastError(); | ||||
53 | } | ||||
54 | } | ||||
55 | ~WinErrorBlocker() | ||||
56 | { | ||||
57 | if (!::SetThreadErrorMode(m_oldmode, NULL)) { | ||||
58 | qWarning() << "Failed to call SetThreadErrorMode:" << qGetLastError(); | ||||
59 | } | ||||
60 | } | ||||
61 | | ||||
62 | private: | ||||
63 | Q_DISABLE_COPY(WinErrorBlocker) | ||||
64 | DWORD m_oldmode; | ||||
65 | }; | ||||
66 | | ||||
67 | #endif |