From 5903bbe2e4c46633465bdb62d40fc0f7805f4f71 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 10 Jan 2019 21:47:16 -0500 Subject: [PATCH] fix freeze when connecting process, crash when process name cant be found, and other refactors --- GUI/CMakeLists.txt | 1 + GUI/extenwindow.cpp | 6 +++--- GUI/host/host.cpp | 17 +++++++++-------- GUI/mainwindow.cpp | 18 ++++++++++-------- GUI/misc.h | 10 +++------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/GUI/CMakeLists.txt b/GUI/CMakeLists.txt index 590eadd..aced78d 100644 --- a/GUI/CMakeLists.txt +++ b/GUI/CMakeLists.txt @@ -1,6 +1,7 @@ include(QtUtils) msvc_registry_search() find_qt5(Core Widgets) +set(AUTOMOC OFF) set(RESOURCE_FILES Textractor.rc Textractor.ico) add_compile_options(/GL) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index f3b8c53..b36fe12 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -87,7 +87,7 @@ ExtenWindow::ExtenWindow(QWidget* parent) : ui->extenList->installEventFilter(this); - for (auto extenName : QString(QAutoFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split(">")) Load(extenName); + for (auto extenName : QString(QTextFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly).readAll()).split(">")) Load(extenName); Sync(); } @@ -99,12 +99,12 @@ ExtenWindow::~ExtenWindow() void ExtenWindow::Sync() { ui->extenList->clear(); - QAutoFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate); + QTextFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate); std::shared_lock readLock(extenMutex); for (auto extenName : extenNames) { ui->extenList->addItem(extenName); - extenSaveFile->write((extenName + ">").toUtf8()); + extenSaveFile.write((extenName + ">").toUtf8()); } } diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index 6ebd595..d1b0363 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -16,7 +16,7 @@ namespace processId(processId), pipe(pipe), mappedFile(OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(processId)).c_str())), - view(MapViewOfFile(mappedFile, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2)), // jichi 1/16/2015: Changed to half to hook section size + view(*(const TextHook(*)[MAX_HOOK])MapViewOfFile(mappedFile, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2)), // jichi 1/16/2015: Changed to half to hook section size viewMutex(ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId)) { OnConnect(processId); @@ -32,24 +32,26 @@ namespace { if (view == nullptr) return {}; std::scoped_lock lock(viewMutex); - auto hooks = (const TextHook*)view; - for (int i = 0; i < MAX_HOOK; ++i) - if (hooks[i].address == addr) return hooks[i]; + for (auto hook : view) + if (hook.address == addr) return hook; return {}; } template void Send(T data) { - std::enable_if_t DUMMY; - WriteFile(pipe, &data, sizeof(data), &DUMMY, nullptr); + std::thread([=] + { + std::enable_if_t DUMMY; + WriteFile(pipe, &data, sizeof(data), &DUMMY, nullptr); + }).detach(); } private: DWORD processId; HANDLE pipe; AutoHandle<> mappedFile; - LPCVOID view; + const TextHook(&view)[MAX_HOOK]; WinMutex viewMutex; }; @@ -78,7 +80,6 @@ namespace SetSecurityDescriptorDacl(&pipeSD, TRUE, NULL, FALSE); // Allow non-admin processes to connect to pipe created by admin host SECURITY_ATTRIBUTES pipeSA = { sizeof(pipeSA), &pipeSD, FALSE }; - struct PipeCloser { void operator()(HANDLE h) { DisconnectNamedPipe(h); CloseHandle(h); } }; AutoHandle hookPipe = CreateNamedPipeW(HOOK_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 0, PIPE_BUFFER_SIZE, MAXDWORD, &pipeSA), diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index 1d99b7b..0717b90 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -34,7 +34,7 @@ MainWindow::MainWindow(QWidget *parent) : } ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding)); - connect(ui->ttCombo, (void(QComboBox::*)(int))&QComboBox::activated, this, &MainWindow::ViewThread); + connect(ui->ttCombo, qOverload(&QComboBox::activated), this, &MainWindow::ViewThread); QSettings settings(CONFIG_FILE, QSettings::IniFormat); if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect()); @@ -88,10 +88,10 @@ void MainWindow::ProcessConnected(DWORD processId) if (processId == 0) return; QMetaObject::invokeMethod(this, [this, processId] { - QString process = S(Util::GetModuleFilename(processId).value()); + QString process = S(Util::GetModuleFilename(processId).value_or(L"???")); ui->processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName()); - QStringList allProcesses = QString(QAutoFile(HOOK_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split("\r", QString::SkipEmptyParts); + QStringList allProcesses = QString(QTextFile(HOOK_SAVE_FILE, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts); // Can't use QFileInfo::absoluteFilePath since hook save file has '\\' as path separator auto hookList = std::find_if(allProcesses.rbegin(), allProcesses.rend(), [&](QString hookList) { return hookList.contains(process); }); if (hookList != allProcesses.rend()) @@ -136,7 +136,7 @@ bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence) { if (DispatchSentenceToExtensions(sentence, GetMiscInfo(thread))) { - sentence += L"\r\n"; + sentence += L"\n"; QString ttString = TextThreadString(thread); QMetaObject::invokeMethod(this, [this, ttString, sentence] { @@ -227,11 +227,13 @@ void MainWindow::SaveHooks() for (int i = 0; i < ui->ttCombo->count(); ++i) { ThreadParam tp = ParseTextThreadString(ui->ttCombo->itemText(i)); - if (tp.processId == GetSelectedProcessId() && !(Host::GetHookParam(tp).type & HOOK_ENGINE)) hookCodes[tp.addr] = GenerateCode(Host::GetHookParam(tp), tp.processId); + if (tp.processId == GetSelectedProcessId()) + { + HookParam hp = Host::GetHookParam(tp); + if (!(hp.type & HOOK_ENGINE)) hookCodes[tp.addr] = GenerateCode(hp, tp.processId); + } } - QString hookList = S(processName.value()); - for (auto hookCode : hookCodes) hookList += " , " + hookCode; - QAutoFile(HOOK_SAVE_FILE, QIODevice::Append)->write((hookList + "\r\n").toUtf8()); + QTextFile(HOOK_SAVE_FILE, QIODevice::Append).write((S(processName.value()) + " , " + QStringList(hookCodes.values()).join(" , ") + "\n").toUtf8()); } } diff --git a/GUI/misc.h b/GUI/misc.h index 7eadb92..d07407d 100644 --- a/GUI/misc.h +++ b/GUI/misc.h @@ -3,14 +3,10 @@ #include "qtcommon.h" #include "types.h" -class QAutoFile +struct QTextFile : QFile { -public: - QAutoFile(const QString& name, QIODevice::OpenMode mode) : f(name) { f.open(mode); } - QFile* operator->() { return &f; } - -private: - QFile f; + using QFile::QFile; + QTextFile(const QString& name, QIODevice::OpenMode mode) : QFile(name) { open(mode | QIODevice::Text); } }; inline std::wstring S(const QString& S) { return { S.toStdWString() }; }