35#include "adminauthorization_p.h"
37#include <QtCore/QSettings>
38#include <QtCore/QVariant>
40#include <QtCore/qt_windows.h>
44static QString qt_create_commandline(
const QStringList &arguments);
49 : neededCoInit(CoInitialize(NULL) == S_OK)
62 SID_IDENTIFIER_AUTHORITY authority = { SECURITY_NT_AUTHORITY };
65 if (!AllocateAndInitializeSid(&authority,
67 SECURITY_BUILTIN_DOMAIN_RID,
68 DOMAIN_ALIAS_RID_ADMINS,
73 BOOL isInAdminGroup = FALSE;
74 if (!CheckTokenMembership(0, adminGroup, &isInAdminGroup))
75 isInAdminGroup = FALSE;
78 return (
bool)isInAdminGroup;
90 QLatin1String key(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
91 QSettings registry(key, QSettings::NativeFormat);
92 const QVariant enableLUA = registry.value(QLatin1String(
"EnableLUA"));
94#
if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
95 (enableLUA.typeId() == QMetaType::Int)
97 (enableLUA.type() == QVariant::Int)
99 && (enableLUA.toInt() == 0))
103 const QString file = QDir::toNativeSeparators(program);
104 const QString args = qt_create_commandline(arguments);
106 SHELLEXECUTEINFOW shellExecuteInfo;
107 ZeroMemory(&shellExecuteInfo,
sizeof(SHELLEXECUTEINFOW));
108 shellExecuteInfo.cbSize =
sizeof(SHELLEXECUTEINFOW);
109 shellExecuteInfo.lpVerb = L
"runas";
110 shellExecuteInfo.lpFile = (
wchar_t *)file.utf16();
111 shellExecuteInfo.lpParameters = (
wchar_t *)args.utf16();
112 shellExecuteInfo.nShow = SW_SHOW;
114 if (ShellExecuteExW(&shellExecuteInfo))
120QString qt_create_commandline(
const QStringList &arguments)
123 for (
int i = 0; i < arguments.size(); ++i) {
124 QString tmp = arguments.at(i);
126 tmp.replace(QLatin1String(
"\\\""), QLatin1String(
"\\\\\""));
128 tmp.replace(QLatin1Char(
'\"'), QLatin1String(
"\\\""));
129 if (tmp.isEmpty() || tmp.contains(QLatin1Char(
' ')) || tmp.contains(QLatin1Char(
'\t'))) {
133 QString endQuote(QLatin1Char(
'\"'));
134 int i = tmp.length();
135 while (i > 0 && tmp.at(i - 1) == QLatin1Char(
'\\')) {
137 endQuote += QLatin1Char(
'\\');
139 args += QLatin1String(
" \"") + tmp.left(i) + endQuote;
141 args += QLatin1Char(
' ') + tmp;
bool hasAdminRights() override
Tests whether this program already has elevated rights or not.
bool executeAsAdmin(const QString &program, const QStringList &arguments) override
Runs a program with the given arguments with elevated rights.