Textractor/GUI/mainwindow.cpp

109 lines
2.9 KiB
C++
Raw Normal View History

2018-07-21 15:40:16 -07:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
2018-07-23 22:57:54 -07:00
#include "QTextBrowser"
2018-07-22 16:53:51 -07:00
#include "QMessageBox"
2018-07-23 22:57:54 -07:00
#include "QComboBox"
2018-07-23 12:25:02 -07:00
#include "QLineEdit"
#include "QTableWidget"
#include "QInputDialog"
2018-07-22 16:53:51 -07:00
#include <Windows.h>
2018-07-23 12:25:02 -07:00
#include <qdebug.h>
#include <Psapi.h>
2018-07-22 16:53:51 -07:00
#include "../texthook/host.h"
2018-07-21 15:40:16 -07:00
2018-07-23 22:57:54 -07:00
QMainWindow* mainWindow;
QComboBox* processCombo;
QComboBox* ttCombo;
QTextBrowser* textOutput;
2018-07-23 12:25:02 -07:00
QString GetModuleName(DWORD processId, HMODULE module = NULL)
{
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
wchar_t buffer[MAX_PATH];
GetModuleFileNameExW(handle, module, buffer, MAX_PATH);
2018-07-23 22:57:54 -07:00
CloseHandle(handle);
2018-07-23 12:25:02 -07:00
return QString::fromWCharArray(wcsrchr(buffer, L'\\') + 1);
}
2018-07-23 22:57:54 -07:00
QString ProcessString(DWORD processId)
2018-07-23 12:25:02 -07:00
{
2018-07-23 22:57:54 -07:00
return QString("%1: %2").arg(QString::number(processId), GetModuleName(processId));
}
QString TextThreadString(TextThread* thread)
{
ThreadParameter tp = thread->GetThreadParameter();
return QString("%1:%2:%3:%4:%5:%6").arg(
QString::number(thread->Number()),
QString::number(tp.pid),
QString::number(tp.hook, 16),
QString::number(tp.retn, 16),
QString::number(tp.spl, 16),
QString::fromWCharArray(Host::GetHookName(tp.pid, tp.hook).c_str())
);
2018-07-23 12:25:02 -07:00
}
2018-07-21 15:40:16 -07:00
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
2018-07-23 22:57:54 -07:00
mainWindow = this;
processCombo = mainWindow->findChild<QComboBox*>("processCombo");
ttCombo = mainWindow->findChild<QComboBox*>("ttCombo");
textOutput = this->findChild<QTextBrowser*>("textOutput");
2018-07-23 12:25:02 -07:00
2018-07-23 22:57:54 -07:00
Host::Start();
Host::RegisterProcessAttachCallback(AddProcess);
Host::RegisterProcessDetachCallback(RemoveProcess);
Host::RegisterThreadCreateCallback(AddThread);
Host::RegisterThreadRemoveCallback(RemoveThread);
2018-07-23 12:25:02 -07:00
Host::Open();
2018-07-21 15:40:16 -07:00
}
MainWindow::~MainWindow()
{
2018-07-23 22:57:54 -07:00
Host::Close();
2018-07-21 15:40:16 -07:00
delete ui;
}
2018-07-22 16:53:51 -07:00
2018-07-23 22:57:54 -07:00
void AddProcess(DWORD processId)
{
processCombo->addItem(ProcessString(processId));
}
void RemoveProcess(DWORD processId)
{
processCombo->removeItem(processCombo->findText(ProcessString(processId)));
}
void AddThread(TextThread* thread)
{
ttCombo->addItem(TextThreadString(thread));
thread->RegisterOutputCallBack([](auto thread, auto data)
{
if (ttCombo->currentText() == TextThreadString(thread)) textOutput->append(QString::fromWCharArray(data.c_str()));
return data + L"\r\n";
});
}
void RemoveThread(TextThread* thread)
{
ttCombo->removeItem(ttCombo->findText(TextThreadString(thread)));
}
2018-07-23 12:25:02 -07:00
void MainWindow::on_attachButton_clicked()
2018-07-22 16:53:51 -07:00
{
2018-07-23 22:57:54 -07:00
Host::InjectProcess(QInputDialog::getInt(this, "Process ID?", "You can find this under Task Manager -> Details"));
}
void MainWindow::on_detachButton_clicked()
{
Host::DetachProcess(processCombo->currentText().split(":")[0].toInt());
}
void MainWindow::on_ttCombo_activated(int index)
{
textOutput->setText(QString::fromWCharArray(Host::GetThread(index)->GetStore().c_str()));
2018-07-22 16:53:51 -07:00
}