summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuhang Zhao <2546789017@qq.com>2022-07-25 10:35:29 +0800
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-03 16:04:39 +0000
commit24ccadbee0b8733615c46edc62c383a6202ebcda (patch)
tree77280b9792173a87b979f05e3eaac272c8b7c0c6
parent0cead45f8c3e5ad36b80390e75d2fe1757fd89de (diff)
QtBase: Windows: Replace some deprecated API usages
Microsoft recommends to use CoInitializeEx() and SetWindowLongPtr()/GetWindowLongPtr() in new code. Use COINIT_DISABLE_OLE1DDE to avoid overhead of initializing and using obsolete technology. Change-Id: I9d16943e864d4487dd4f46fd9325579c298c52b9 Reviewed-by: Oliver Wolff <oliver.wolff@qt.io> (cherry picked from commit 8a0e0f8cc1f93d4f5fe205de75cacd2265017395) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/corelib/io/qfilesystemengine_win.cpp6
-rw-r--r--src/corelib/kernel/qeventdispatcher_win.cpp8
-rw-r--r--src/network/kernel/qnetconmonitor_win.cpp2
-rw-r--r--src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp4
-rw-r--r--src/plugins/platforms/windows/qwindowstheme.cpp2
-rw-r--r--src/plugins/platforms/windows/qwindowswindow.cpp14
-rw-r--r--tests/auto/corelib/io/qfile/tst_qfile.cpp2
-rw-r--r--tests/auto/other/qaccessibility/tst_qaccessibility.cpp2
-rw-r--r--tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp2
9 files changed, 17 insertions, 25 deletions
diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp
index a04878ca3b..d36e896305 100644
--- a/src/corelib/io/qfilesystemengine_win.cpp
+++ b/src/corelib/io/qfilesystemengine_win.cpp
@@ -679,7 +679,7 @@ static QString readLink(const QFileSystemEntry &link)
if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
neededCoInit = true;
- CoInitialize(nullptr);
+ CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink,
(LPVOID *)&psl);
}
@@ -1668,7 +1668,7 @@ bool QFileSystemEngine::createLink(const QFileSystemEntry &source, const QFileSy
bool neededCoInit = false;
if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
neededCoInit = true;
- CoInitialize(nullptr);
+ CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink,
reinterpret_cast<void **>(&psl));
}
@@ -1762,7 +1762,7 @@ bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
// we need the "display name" of the file, so can't use nativeAbsoluteFilePath
const QString sourcePath = QDir::toNativeSeparators(absoluteName(source).filePath());
- CoInitialize(nullptr);
+ CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
IFileOperation *pfo = nullptr;
IShellItem *deleteItem = nullptr;
FileOperationProgressSink *sink = nullptr;
diff --git a/src/corelib/kernel/qeventdispatcher_win.cpp b/src/corelib/kernel/qeventdispatcher_win.cpp
index 709e1494b9..04e1abf16a 100644
--- a/src/corelib/kernel/qeventdispatcher_win.cpp
+++ b/src/corelib/kernel/qeventdispatcher_win.cpp
@@ -99,11 +99,7 @@ LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPA
if (dispatcher->filterNativeEvent(QByteArrayLiteral("windows_dispatcher_MSG"), &msg, &result))
return result;
-#ifdef GWLP_USERDATA
auto q = reinterpret_cast<QEventDispatcherWin32 *>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
-#else
- auto q = reinterpret_cast<QEventDispatcherWin32 *>(GetWindowLong(hwnd, GWL_USERDATA));
-#endif
QEventDispatcherWin32Private *d = nullptr;
if (q != nullptr)
d = q->d_func();
@@ -289,11 +285,7 @@ static HWND qt_create_internal_window(const QEventDispatcherWin32 *eventDispatch
return 0;
}
-#ifdef GWLP_USERDATA
SetWindowLongPtr(wnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(eventDispatcher));
-#else
- SetWindowLong(wnd, GWL_USERDATA, reinterpret_cast<LONG>(eventDispatcher));
-#endif
return wnd;
}
diff --git a/src/network/kernel/qnetconmonitor_win.cpp b/src/network/kernel/qnetconmonitor_win.cpp
index c91c4d5c23..df19ed5f29 100644
--- a/src/network/kernel/qnetconmonitor_win.cpp
+++ b/src/network/kernel/qnetconmonitor_win.cpp
@@ -299,7 +299,7 @@ bool QNetworkConnectionEvents::stopMonitoring()
QNetworkConnectionMonitorPrivate::QNetworkConnectionMonitorPrivate()
{
- auto hr = CoInitialize(nullptr);
+ auto hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) {
qCDebug(lcNetMon) << "Failed to initialize COM:" << errorStringFromHResult(hr);
comInitFailed = true;
diff --git a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp
index eb42abc48e..9673d2cf69 100644
--- a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp
+++ b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagernetworkinformationbackend.cpp
@@ -121,7 +121,7 @@ public:
QNetworkListManagerNetworkInformationBackend::QNetworkListManagerNetworkInformationBackend()
{
- auto hr = CoInitialize(nullptr);
+ auto hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) {
qCWarning(lcNetInfoNLM) << "Failed to initialize COM:" << errorStringFromHResult(hr);
comInitFailed = true;
@@ -178,7 +178,7 @@ bool QNetworkListManagerNetworkInformationBackend::start()
Q_ASSERT(!monitoring);
if (comInitFailed) {
- auto hr = CoInitialize(nullptr);
+ auto hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) {
qCWarning(lcNetInfoNLM) << "Failed to initialize COM:" << errorStringFromHResult(hr);
comInitFailed = true;
diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp
index f9e20969eb..ef7caa60d9 100644
--- a/src/plugins/platforms/windows/qwindowstheme.cpp
+++ b/src/plugins/platforms/windows/qwindowstheme.cpp
@@ -983,7 +983,7 @@ QPixmap QWindowsFileIconEngine::filePixmap(const QSize &size, QIcon::Mode, QIcon
{
/* We don't use the variable, but by storing it statically, we
* ensure CoInitialize is only called once. */
- static HRESULT comInit = CoInitialize(nullptr);
+ static HRESULT comInit = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
Q_UNUSED(comInit);
static QCache<QString, FakePointer<int> > dirIconEntryCache(1000);
diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp
index 9a9ba36910..a795328bdf 100644
--- a/src/plugins/platforms/windows/qwindowswindow.cpp
+++ b/src/plugins/platforms/windows/qwindowswindow.cpp
@@ -475,15 +475,15 @@ static bool shouldShowMaximizeButton(const QWindow *w, Qt::WindowFlags flags)
// Qt::WindowTransparentForInput (in combination with WS_EX_TRANSPARENT).
bool QWindowsWindow::setWindowLayered(HWND hwnd, Qt::WindowFlags flags, bool hasAlpha, qreal opacity)
{
- const LONG exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
+ const LONG_PTR exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
const bool needsLayered = (flags & Qt::WindowTransparentForInput)
|| (hasAlpha && (flags & Qt::FramelessWindowHint)) || opacity < 1.0;
const bool isLayered = (exStyle & WS_EX_LAYERED);
if (needsLayered != isLayered) {
if (needsLayered) {
- SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_LAYERED);
+ SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle | WS_EX_LAYERED);
} else {
- SetWindowLong(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED);
+ SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED);
}
}
return needsLayered;
@@ -1084,8 +1084,8 @@ QMargins QWindowsGeometryHint::frame(const QWindow *w, HWND hwnd)
{
if (!w->isTopLevel() || w->flags().testFlag(Qt::FramelessWindowHint))
return {};
- return frame(w, hwnd, DWORD(GetWindowLongPtrW(hwnd, GWL_STYLE)),
- DWORD(GetWindowLongPtrW(hwnd, GWL_EXSTYLE)));
+ return frame(w, hwnd, DWORD(GetWindowLongPtr(hwnd, GWL_STYLE)),
+ DWORD(GetWindowLongPtr(hwnd, GWL_EXSTYLE)));
}
// For newly created windows.
@@ -1207,7 +1207,7 @@ bool QWindowsGeometryHint::positionIncludesFrame(const QWindow *w)
bool QWindowsBaseWindow::isRtlLayout(HWND hwnd)
{
- return (GetWindowLongPtrW(hwnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL) != 0;
+ return (GetWindowLongPtr(hwnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL) != 0;
}
QWindowsBaseWindow *QWindowsBaseWindow::baseWindowOf(const QWindow *w)
@@ -2310,7 +2310,7 @@ bool QWindowsWindow::handleWmPaint(HWND hwnd, UINT message,
return true;
}
// QTBUG-75455: Suppress WM_PAINT sent to invisible windows when setting WS_EX_LAYERED
- if (!window()->isVisible() && (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_LAYERED) != 0)
+ if (!window()->isVisible() && (GetWindowLongPtr(hwnd, GWL_EXSTYLE) & WS_EX_LAYERED) != 0)
return false;
// Ignore invalid update bounding rectangles
if (!GetUpdateRect(m_data.hwnd, 0, FALSE))
diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp
index fe412641f5..828a90e86e 100644
--- a/tests/auto/corelib/io/qfile/tst_qfile.cpp
+++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp
@@ -1546,7 +1546,7 @@ static QString getWorkingDirectoryForLink(const QString &linkFileName)
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
neededCoInit = true;
- CoInitialize(NULL);
+ CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
}
diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp
index fa9f2a5866..89ef57e29b 100644
--- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp
+++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp
@@ -3817,7 +3817,7 @@ void tst_QAccessibility::bridgeTest()
POINT pt{nativePos.x(), nativePos.y()};
// Initialize COM stuff.
- HRESULT hr = CoInitialize(nullptr);
+ HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
QVERIFY(SUCCEEDED(hr));
// Get UI Automation interface.
diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
index 2fc3feb5e8..b43494ab54 100644
--- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
+++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
@@ -594,7 +594,7 @@ void tst_QWidget::getSetCheck()
#if defined (Q_OS_WIN)
obj1.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
const HWND handle = reinterpret_cast<HWND>(obj1.winId()); // explicitly create window handle
- QVERIFY(GetWindowLong(handle, GWL_STYLE) & LONG(WS_POPUP));
+ QVERIFY(GetWindowLongPtr(handle, GWL_STYLE) & LONG_PTR(WS_POPUP));
#endif
}