forked from MatsuriDayo/nekoray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinCommander.cpp
105 lines (93 loc) · 3.04 KB
/
WinCommander.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/****************************************************************************
**
** Copyright (C) 2014 UpdateNode UG (haftungsbeschränkt)
** Contact: [email protected]
**
** This file is part of the UpdateNode Client.
**
** Commercial License Usage
** Licensees holding valid commercial UpdateNode license may use this file
** under the terms of the the Apache License, Version 2.0
** Full license description file: LICENSE.COM
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation. Please review the following information to ensure the
** GNU General Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
** Full license description file: LICENSE.GPL
**
****************************************************************************/
#include "WinCommander.hpp"
#include <QSysInfo>
#include <QDir>
#ifdef Q_OS_WIN
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <shellapi.h>
#include <sddl.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
#endif
/*!
Executes a command elevated specified by \apath , using paramters \aparameters.
\n
Parameter /aaWait decides if the function should return immediatelly after it's\n
execution or wait for the exit of the launched process
\n
Returns the return value of the executed command
*/
uint WinCommander::runProcessElevated(const QString &path,
const QStringList ¶meters,
const QString &workingDir,
int nShow, bool aWait) {
uint result = 0;
#ifdef Q_OS_WIN
QString params;
HWND hwnd = NULL;
LPCTSTR pszPath = (LPCTSTR)path.utf16();
foreach(QString item, parameters)
params += "\"" + item + "\" ";
LPCTSTR pszParameters = (LPCTSTR)params.utf16();
QString dir;
if (workingDir.count() == 0)
dir = QDir::toNativeSeparators(QDir::currentPath());
else
dir = QDir::toNativeSeparators(workingDir);
LPCTSTR pszDirectory = (LPCTSTR)dir.utf16();
SHELLEXECUTEINFO shex;
DWORD dwCode = 0;
ZeroMemory(&shex, sizeof(shex));
shex.cbSize = sizeof(shex);
shex.fMask = SEE_MASK_NOCLOSEPROCESS;
shex.hwnd = hwnd;
shex.lpVerb = TEXT("runas");
shex.lpFile = pszPath;
shex.lpParameters = pszParameters;
shex.lpDirectory = pszDirectory;
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
shex.nShow = nShow;
ShellExecuteEx(&shex);
if (shex.hProcess)
{
if(aWait)
{
WaitForSingleObject(shex.hProcess, INFINITE );
GetExitCodeProcess(shex.hProcess, &dwCode);
}
CloseHandle (shex.hProcess) ;
}
else
return -1;
result = (uint)dwCode;
#else
Q_UNUSED(path);
Q_UNUSED(parameters);
Q_UNUSED(workingDir);
Q_UNUSED(aWait);
#endif
return result;
}