Sandboxie/SandboxiePlus/SandMan/main.cpp

195 lines
5.6 KiB
C++
Raw Normal View History

2021-10-16 16:19:51 +01:00
#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>
2022-01-30 14:53:37 +00:00
#include "./Windows/SettingsWindow.h"
2022-08-05 17:23:19 +01:00
#include "./Wizards/SetupWizard.h"
2021-10-16 16:19:51 +01:00
CSettings* theConf = NULL;
2022-01-13 22:52:58 +00:00
QString g_PendingMessage;
2021-10-16 16:19:51 +01:00
int main(int argc, char *argv[])
{
2022-09-29 17:28:48 +01:00
srand(QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
2022-06-27 07:33:15 +01:00
2022-07-09 10:46:07 +01:00
wchar_t szPath[MAX_PATH];
GetModuleFileNameW(NULL, szPath, ARRAYSIZE(szPath));
*wcsrchr(szPath, L'\\') = L'\0';
QString AppDir = QString::fromWCharArray(szPath);
2023-07-11 21:35:59 +01:00
if (QFile::exists(AppDir + "\\Certificate.dat"))
2022-07-09 10:46:07 +01:00
CSettingsWindow::LoadCertificate(AppDir + "\\Certificate.dat");
2023-07-01 17:54:53 +01:00
// use AppFolder/PlusData when present, else fallback to AppFolder
QString ConfDir = AppDir + "\\PlusData";
if(!QFile::exists(ConfDir))
ConfDir = AppDir;
2022-07-09 10:46:07 +01:00
// use a shared setting location when used in a business environment for easier administration
2023-07-11 21:35:59 +01:00
theConf = new CSettings(ConfDir, "Sandboxie-Plus");
2022-07-09 10:46:07 +01:00
2022-08-10 19:14:37 +01:00
#ifndef _DEBUG
InitMiniDumpWriter(QString("SandMan-v%1").arg(CSandMan::GetVersion()).toStdWString().c_str() , QString(theConf->GetConfigDir()).replace("/", "\\").toStdWString().c_str());
#endif
2022-07-09 10:46:07 +01:00
// 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);
//}
2022-06-27 07:33:15 +01:00
2023-03-14 17:39:09 +00:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
2023-03-14 07:41:25 +00:00
QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
2023-03-14 17:39:09 +00:00
#endif
2021-10-16 16:19:51 +01:00
QtSingleApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
//InitConsole(false);
bool IsBoxed = GetModuleHandle(L"SbieDll.dll") != NULL;
2022-02-02 18:31:03 +00:00
if (!IsBoxed) {
SB_STATUS Status = CSbieUtils::DoAssist();
if (Status.GetStatus()) {
if (Status.GetStatus() != ERROR_OK)
return Status.GetStatus();
return 0;
}
2021-10-16 16:19:51 +01:00
}
QStringList Args = QCoreApplication::arguments();
2022-08-05 17:23:19 +01:00
int CmdPos = Args.indexOf("/OpenReg", Qt::CaseInsensitive);
2021-10-16 16:19:51 +01:00
if (CmdPos != -1) {
if (Args.count() > CmdPos + 2) {
QProcess::startDetached(Args.at(CmdPos + 2));
QThread::msleep(1000);
}
ShellOpenRegKey(Args.at(CmdPos + 1));
return 0;
}
2022-08-05 17:23:19 +01:00
CmdPos = Args.indexOf("/ShellUninstall", Qt::CaseInsensitive);
if (CmdPos != -1) {
CSetupWizard::ShellUninstall();
return 0;
}
2021-10-16 16:19:51 +01:00
CmdPos = Args.indexOf("-op", Qt::CaseInsensitive);
if (CmdPos != -1) {
QString Op;
if (Args.count() > CmdPos)
Op = Args.at(CmdPos + 1);
2022-01-13 22:52:58 +00:00
g_PendingMessage = "Op:" + Op;
2021-10-16 16:19:51 +01:00
}
2024-01-07 16:24:28 +00:00
// Context Menu invocations
int BoxPos = -1;
2023-07-01 17:54:53 +01:00
for (int i = 0; i < Args.size(); i++) {
2024-01-07 16:24:28 +00:00
if (Args[i].left(5).compare("/box:", Qt::CaseInsensitive) == 0) {
BoxPos = i;
break;
}
2023-07-01 17:54:53 +01:00
}
2024-01-07 16:24:28 +00:00
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)
{
2021-10-16 16:19:51 +01:00
// Note: a escaped command ending with \" will fail and unescape "
//QString CommandLine;
2024-01-07 16:24:28 +00:00
//for (int i = BoxPos + 1; i < Args.count(); i++)
2021-10-16 16:19:51 +01:00
// CommandLine += "\"" + Args[i] + "\" ";
2022-01-13 22:52:58 +00:00
//g_PendingMessage = "Run:" + CommandLine.trimmed();
2023-07-01 17:54:53 +01:00
LPWSTR cmdLine0 = wcsstr(GetCommandLineW(), L"/box:");
if (!cmdLine0) return -1;
LPWSTR cmdLine = wcschr(cmdLine0 + 5, L' ');
if (!cmdLine) return -2;
2021-10-16 16:19:51 +01:00
if (IsBoxed) {
2023-07-01 17:54:53 +01:00
ShellExecute(NULL, L"open", cmdLine + 1, NULL, NULL, SW_SHOWNORMAL);
2021-10-16 16:19:51 +01:00
return 0;
}
2023-07-01 17:54:53 +01:00
g_PendingMessage = "Run:" + QString::fromWCharArray(cmdLine + 1);
2022-01-13 22:52:58 +00:00
g_PendingMessage += "\nFrom:" + QDir::currentPath();
2023-07-01 17:54:53 +01:00
QString BoxName = QString::fromWCharArray(cmdLine0 + 5, cmdLine - (cmdLine0 + 5));
if(BoxName != "__ask__")
g_PendingMessage += "\nIn:" + BoxName;
2021-10-16 16:19:51 +01:00
}
2024-01-07 16:24:28 +00:00
// 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*";
}
2021-10-16 16:19:51 +01:00
2024-01-07 16:24:28 +00:00
2021-10-16 16:19:51 +01:00
if (IsBoxed) {
QMessageBox::critical(NULL, "Sandboxie-Plus", CSandMan::tr("Sandboxie Manager can not be run sandboxed!"));
return -1;
}
2022-01-13 22:52:58 +00:00
if (!g_PendingMessage.isEmpty()) {
if(app.sendMessage(g_PendingMessage))
2021-10-16 16:19:51 +01:00
return 0;
2022-12-07 16:32:40 +00:00
app.disableSingleApp(); // we start to do one job and exit, don't interfere with starting a regular instance
2021-07-23 08:44:35 +01:00
}
2022-09-18 10:17:42 +01:00
else {
if (app.arguments().contains("-autorun") && app.isRunning())
return 0;
if (app.sendMessage("ShowWnd"))
return 0;
}
2021-10-16 16:19:51 +01:00
//QThreadPool::globalInstance()->setMaxThreadCount(theConf->GetInt("Options/MaxThreadPool", 10));
CSandMan* pWnd = new CSandMan();
2023-02-01 20:57:39 +00:00
QObject::connect(&app, SIGNAL(messageReceived(const QString&)), pWnd, SLOT(OnMessage(const QString&)), Qt::QueuedConnection);
2021-10-16 16:19:51 +01:00
int ret = app.exec();
delete pWnd;
delete theConf;
theConf = NULL;
return ret;
}