-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.cpp
194 lines (160 loc) · 5.63 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "stdafx.h"
#include "SandMan.h"
#include <QtWidgets/QApplication>
#include "../QSbieAPI/SbieAPI.h"
#include "../QtSingleApp/src/qtsingleapplication.h"
#include "../QSbieAPI/SbieUtils.h"
#include "../MiscHelpers/Common/Common.h"
#include <windows.h>
#include "./Windows/SettingsWindow.h"
#include "./Wizards/SetupWizard.h"
CSettings* theConf = NULL;
QString g_PendingMessage;
int main(int argc, char *argv[])
{
srand(QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
wchar_t szPath[MAX_PATH];
GetModuleFileNameW(NULL, szPath, ARRAYSIZE(szPath));
*wcsrchr(szPath, L'\\') = L'\0';
QString AppDir = QString::fromWCharArray(szPath);
if (QFile::exists(AppDir + "\\Certificate.dat"))
CSettingsWindow::LoadCertificate(AppDir + "\\Certificate.dat");
// use AppFolder/PlusData when present, else fallback to AppFolder
QString ConfDir = AppDir + "\\PlusData";
if(!QFile::exists(ConfDir))
ConfDir = AppDir;
// use a shared setting location when used in a business environment for easier administration
theConf = new CSettings(ConfDir, "Sandboxie-Plus");
#ifndef _DEBUG
InitMiniDumpWriter(QString("SandMan-v%1").arg(CSandMan::GetVersion()).toStdWString().c_str() , QString(theConf->GetConfigDir()).replace("/", "\\").toStdWString().c_str());
#endif
// this must be done before we create QApplication
int DPI = theConf->GetInt("Options/DPIScaling", 1);
if (DPI == 1) {
//SetProcessDPIAware();
//SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
//SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
typedef DPI_AWARENESS_CONTEXT(WINAPI* P_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext);
P_SetThreadDpiAwarenessContext pSetThreadDpiAwarenessContext = (P_SetThreadDpiAwarenessContext)GetProcAddress(GetModuleHandle(L"user32.dll"), "SetThreadDpiAwarenessContext");
if(pSetThreadDpiAwarenessContext) // not present on windows 7
pSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
else
SetProcessDPIAware();
}
else if (DPI == 2) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
}
//else {
// QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
//}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
#endif
QtSingleApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
//InitConsole(false);
bool IsBoxed = GetModuleHandle(L"SbieDll.dll") != NULL;
if (!IsBoxed) {
SB_STATUS Status = CSbieUtils::DoAssist();
if (Status.GetStatus()) {
if (Status.GetStatus() != ERROR_OK)
return Status.GetStatus();
return 0;
}
}
QStringList Args = QCoreApplication::arguments();
int CmdPos = Args.indexOf("/OpenReg", Qt::CaseInsensitive);
if (CmdPos != -1) {
if (Args.count() > CmdPos + 2) {
QProcess::startDetached(Args.at(CmdPos + 2));
QThread::msleep(1000);
}
ShellOpenRegKey(Args.at(CmdPos + 1));
return 0;
}
CmdPos = Args.indexOf("/ShellUninstall", Qt::CaseInsensitive);
if (CmdPos != -1) {
CSetupWizard::ShellUninstall();
return 0;
}
CmdPos = Args.indexOf("-op", Qt::CaseInsensitive);
if (CmdPos != -1) {
QString Op;
if (Args.count() > CmdPos)
Op = Args.at(CmdPos + 1);
g_PendingMessage = "Op:" + Op;
}
// Context Menu invocations
int BoxPos = -1;
for (int i = 0; i < Args.size(); i++) {
if (Args[i].left(5).compare("/box:", Qt::CaseInsensitive) == 0) {
BoxPos = i;
break;
}
}
int DfpPos = Args.indexOf("/disable_force", Qt::CaseInsensitive);
// the first argument wins
if (BoxPos != -1 && DfpPos != -1) {
if (BoxPos < DfpPos) DfpPos = -1;
else BoxPos = -1;
}
// run sandboxed
if (BoxPos != -1)
{
// Note: a escaped command ending with \" will fail and unescape "
//QString CommandLine;
//for (int i = BoxPos + 1; i < Args.count(); i++)
// CommandLine += "\"" + Args[i] + "\" ";
//g_PendingMessage = "Run:" + CommandLine.trimmed();
LPWSTR cmdLine0 = wcsstr(GetCommandLineW(), L"/box:");
if (!cmdLine0) return -1;
LPWSTR cmdLine = wcschr(cmdLine0 + 5, L' ');
if (!cmdLine) return -2;
if (IsBoxed) {
ShellExecute(NULL, L"open", cmdLine + 1, NULL, NULL, SW_SHOWNORMAL);
return 0;
}
g_PendingMessage = "Run:" + QString::fromWCharArray(cmdLine + 1);
g_PendingMessage += "\nFrom:" + QDir::currentPath();
QString BoxName = QString::fromWCharArray(cmdLine0 + 5, cmdLine - (cmdLine0 + 5));
if(BoxName != "__ask__")
g_PendingMessage += "\nIn:" + BoxName;
}
// run un sandboxed
if (DfpPos != -1)
{
LPWSTR cmdLine0 = wcsstr(GetCommandLineW(), L"/disable_force");
if (!cmdLine0) return -1;
LPWSTR cmdLine = cmdLine0 + 14;
if (IsBoxed) {
ShellExecute(NULL, L"open", cmdLine + 1, NULL, NULL, SW_SHOWNORMAL);
return 0;
}
g_PendingMessage = "Run:" + QString::fromWCharArray(cmdLine + 1);
g_PendingMessage += "\nFrom:" + QDir::currentPath();
g_PendingMessage += "\nIn:*DFP*";
}
if (IsBoxed) {
QMessageBox::critical(NULL, "Sandboxie-Plus", CSandMan::tr("Sandboxie Manager can not be run sandboxed!"));
return -1;
}
if (!g_PendingMessage.isEmpty()) {
if(app.sendMessage(g_PendingMessage))
return 0;
app.disableSingleApp(); // we start to do one job and exit, don't interfere with starting a regular instance
}
else {
if (app.arguments().contains("-autorun") && app.isRunning())
return 0;
if (app.sendMessage("ShowWnd"))
return 0;
}
//QThreadPool::globalInstance()->setMaxThreadCount(theConf->GetInt("Options/MaxThreadPool", 10));
CSandMan* pWnd = new CSandMan();
QObject::connect(&app, SIGNAL(messageReceived(const QString&)), pWnd, SLOT(OnMessage(const QString&)), Qt::QueuedConnection);
int ret = app.exec();
delete pWnd;
delete theConf;
theConf = NULL;
return ret;
}