Sandboxie/SandboxiePlus/SandMan/main.cpp

112 lines
3.1 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/qRC4.h"
#include "../MiscHelpers/Common/Common.h"
#include <windows.h>
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[])
{
#ifdef Q_OS_WIN
2022-01-14 10:56:53 +00:00
//SetProcessDPIAware();
2021-10-16 16:19:51 +01:00
#endif // Q_OS_WIN
2022-01-14 10:56:53 +00:00
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
2021-10-16 16:19:51 +01:00
//QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
QtSingleApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
//InitConsole(false);
bool IsBoxed = GetModuleHandle(L"SbieDll.dll") != NULL;
SB_STATUS Status = CSbieUtils::DoAssist();
if (Status.GetStatus()) {
if(Status.GetStatus() == ERROR_OK) app.sendMessage("Status:OK");
else app.sendMessage("Status:" + CSandMan::FormatError(Status)); // todo: localization
return 0;
}
QStringList Args = QCoreApplication::arguments();
int CmdPos = Args.indexOf("-open_reg", 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("-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
}
CmdPos = Args.indexOf("/box:__ask__", Qt::CaseInsensitive);
if (CmdPos != -1) {
// Note: a escaped command ending with \" will fail and unescape "
//QString CommandLine;
//for (int i = CmdPos + 1; i < Args.count(); i++)
// CommandLine += "\"" + Args[i] + "\" ";
2022-01-13 22:52:58 +00:00
//g_PendingMessage = "Run:" + CommandLine.trimmed();
2021-10-16 16:19:51 +01:00
LPWSTR ChildCmdLine = wcsstr(GetCommandLineW(), L"/box:__ask__") + 13;
if (IsBoxed) {
ShellExecute(NULL, L"open", ChildCmdLine, NULL, NULL, SW_SHOWNORMAL);
return 0;
}
2022-01-13 22:52:58 +00:00
g_PendingMessage = "Run:" + QString::fromWCharArray(ChildCmdLine);
g_PendingMessage += "\nFrom:" + QDir::currentPath();
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-01-30 08:54:37 +00:00
app.disableSingleApp(); // we start to do one job and exit, don't interfear with starting a regular instance
2021-07-23 08:44:35 +01:00
}
2021-07-05 12:37:28 +01:00
else if (app.sendMessage("ShowWnd"))
2021-10-16 16:19:51 +01:00
return 0;
theConf = new CSettings("Sandboxie-Plus");
//QThreadPool::globalInstance()->setMaxThreadCount(theConf->GetInt("Options/MaxThreadPool", 10));
CSandMan* pWnd = new CSandMan();
QObject::connect(&app, SIGNAL(messageReceived(const QString&)), pWnd, SLOT(OnMessage(const QString&)));
int ret = app.exec();
delete pWnd;
delete theConf;
theConf = NULL;
return ret;
}
/*HANDLE hServerPipe = CreateFileW(L"\\\\.\\pipe\\qtsingleapp-sandma-ca4a-1", GENERIC_ALL, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hServerPipe != INVALID_HANDLE_VALUE) {
DWORD lenWritten;
WriteFile(hServerPipe, "test", 4, &lenWritten, NULL)
CloseHandle(hServerPipe);
2021-07-05 12:37:28 +01:00
}*/