From 130d28028ee7ddc43c76fcfc258fcfb38b4e4720 Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 1/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 43 +++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index f0b56ec7d11..204dda36d90 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -15,21 +15,56 @@ #include #include #include +#include +#include +#include static const char runPathC[] = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"; +typedef HRESULT (WINAPI *SHGetKnownFolderPathFun)( + const GUID &rfid, + DWORD dwFlags, + HANDLE hToken, + PWSTR *ppszPath +); + static void setupFavLink_private(const QString &folder) { // Windows Explorer: Place under "Favorites" (Links) - wchar_t path[MAX_PATH]; - SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE); - QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path)); + + static SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = NULL; + QString linkName; QDir folderDir(QDir::fromNativeSeparators(folder)); - QString linkName = profile+QLatin1String("/Links/") + folderDir.dirName() + QLatin1String(".lnk"); + if (!SHGetKnownFolderPathPtr) + { + QLibrary kernel32Lib("shell32.dll"); + if(kernel32Lib.load()) + { + SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) kernel32Lib.resolve("SHGetKnownFolderPath"); + } + } + + if(SHGetKnownFolderPathPtr) { + /* Use new WINAPI functions */ + wchar_t *path = NULL; + if(SHGetKnownFolderPathPtr(FOLDERID_Links, 0, NULL, &path) == S_OK) { + QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); + linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); + } + } else { + /* Use legacy functions */ + wchar_t path[MAX_PATH]; + SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE); + QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path)); + linkName = QDir(profile).filePath(QDir(QLatin1String("Links")).filePath(folderDir.dirName() + QLatin1String(".lnk"))); + } + qDebug() << Q_FUNC_INFO << " creating link from " << linkName << " to " << folder; if (!QFile::link(folder, linkName)) qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!"; + } + bool hasLaunchOnStartup_private(const QString &appName) { QString runPath = QLatin1String(runPathC); From c4b5b78745bf4c12168288817d26c12b6fe3154a Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 2/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index 204dda36d90..19a8482bb1e 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -21,42 +21,20 @@ static const char runPathC[] = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"; -typedef HRESULT (WINAPI *SHGetKnownFolderPathFun)( - const GUID &rfid, - DWORD dwFlags, - HANDLE hToken, - PWSTR *ppszPath -); + static void setupFavLink_private(const QString &folder) { // Windows Explorer: Place under "Favorites" (Links) - static SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = NULL; QString linkName; QDir folderDir(QDir::fromNativeSeparators(folder)); - if (!SHGetKnownFolderPathPtr) - { - QLibrary kernel32Lib("shell32.dll"); - if(kernel32Lib.load()) - { - SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) kernel32Lib.resolve("SHGetKnownFolderPath"); - } - } - if(SHGetKnownFolderPathPtr) { - /* Use new WINAPI functions */ - wchar_t *path = NULL; - if(SHGetKnownFolderPathPtr(FOLDERID_Links, 0, NULL, &path) == S_OK) { - QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); - linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); - } - } else { - /* Use legacy functions */ - wchar_t path[MAX_PATH]; - SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE); - QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path)); - linkName = QDir(profile).filePath(QDir(QLatin1String("Links")).filePath(folderDir.dirName() + QLatin1String(".lnk"))); + /* Use new WINAPI functions */ + wchar_t *path = NULL; + if(SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { + QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); + linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); } qDebug() << Q_FUNC_INFO << " creating link from " << linkName << " to " << folder; if (!QFile::link(folder, linkName)) From 42371c82511775f2ab14cc81b69c91f9d474d214 Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 3/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index 19a8482bb1e..a0826b3d052 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -37,6 +37,7 @@ static void setupFavLink_private(const QString &folder) linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); } qDebug() << Q_FUNC_INFO << " creating link from " << linkName << " to " << folder; + CoTaskMemFree(*path); if (!QFile::link(folder, linkName)) qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!"; From 9cf67765481567b58dcdb7c4f774a0a0db2f7436 Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 4/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index a0826b3d052..dc46a56378d 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -31,13 +31,14 @@ static void setupFavLink_private(const QString &folder) QDir folderDir(QDir::fromNativeSeparators(folder)); /* Use new WINAPI functions */ - wchar_t *path = NULL; + PWSTR path; + if(SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); } qDebug() << Q_FUNC_INFO << " creating link from " << linkName << " to " << folder; - CoTaskMemFree(*path); + CoTaskMemFree(path); if (!QFile::link(folder, linkName)) qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!"; From 9df6dff46b89b784e7e5834e84e9f3c8577c813a Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 5/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index dc46a56378d..e49a5967780 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -36,9 +36,9 @@ static void setupFavLink_private(const QString &folder) if(SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); + CoTaskMemFree(path); } qDebug() << Q_FUNC_INFO << " creating link from " << linkName << " to " << folder; - CoTaskMemFree(path); if (!QFile::link(folder, linkName)) qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!"; From a836759c3be70db9ca0445258d2e680a1bc2afd6 Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 6/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index e49a5967780..9c7ec91b24f 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -12,9 +12,11 @@ * for more details. */ +#define WINVER 0x0600 #include #include #include +#include #include #include #include @@ -33,7 +35,7 @@ static void setupFavLink_private(const QString &folder) /* Use new WINAPI functions */ PWSTR path; - if(SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { + if(shell32.SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); CoTaskMemFree(path); From f1b449affd9929c342c5cdf1d5c93e628d2a97d4 Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 7/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index 9c7ec91b24f..e9d7ee464cc 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -35,7 +35,7 @@ static void setupFavLink_private(const QString &folder) /* Use new WINAPI functions */ PWSTR path; - if(shell32.SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { + if(SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) { QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); CoTaskMemFree(path); From 3307aeefb1c3e438206ea3f074c5a83f34a5f6dc Mon Sep 17 00:00:00 2001 From: Luca Lorenzetto Date: Mon, 10 Apr 2017 10:36:39 +0200 Subject: [PATCH 8/8] When creating explorer favorite use more specific windows functions Fixes https://github.com/owncloud/client/issues/2719 --- src/libsync/utility_win.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsync/utility_win.cpp b/src/libsync/utility_win.cpp index e9d7ee464cc..eec0e89ac7d 100644 --- a/src/libsync/utility_win.cpp +++ b/src/libsync/utility_win.cpp @@ -12,6 +12,7 @@ * for more details. */ +#define _WIN32_WINNT 0x0600 #define WINVER 0x0600 #include #include