implement callbacks properly and update gui some more

This commit is contained in:
Akash Mozumdar 2018-07-24 10:39:02 -07:00
parent 97fe9800a6
commit 541e3cc4e7
9 changed files with 128 additions and 46 deletions

View File

@ -24,10 +24,12 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp mainwindow.cpp \
hostsignaller.cpp
HEADERS += \ HEADERS += \
mainwindow.h mainwindow.h \
hostsignaller.h
FORMS += \ FORMS += \
mainwindow.ui mainwindow.ui

19
GUI/hostsignaller.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "hostsignaller.h"
#include "../texthook/host.h"
void HostSignaller::Initialize()
{
Host::RegisterProcessAttachCallback([&](DWORD pid){ emit AddProcess(pid); });
Host::RegisterProcessDetachCallback([&](DWORD pid){ emit RemoveProcess(pid); });
Host::RegisterThreadCreateCallback([&](TextThread* thread)
{
emit AddThread(thread);
thread->RegisterOutputCallBack([&](TextThread* thread, std::wstring output)
{
//output = DispatchToExtensions(output);
emit ThreadOutput(thread, QString::fromWCharArray(output.c_str()));
return output;
});
});
Host::RegisterThreadRemoveCallback([&](TextThread* thread){ emit RemoveThread(thread); });
}

24
GUI/hostsignaller.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef HOSTSIGNALLER_H
#define HOSTSIGNALLER_H
#include <QObject>
#include <Windows.h>
#include "../texthook/host.h"
// Artikash 7/24/2018: This class is a workaround for the fact that Qt only lets me manipulate the GUI in the main thread.
class HostSignaller : public QObject
{
Q_OBJECT
public:
void Initialize();
signals:
void AddProcess(unsigned int processId);
void RemoveProcess(unsigned int processId);
void AddThread(TextThread* thread);
void RemoveThread(TextThread* thread);
void ThreadOutput(TextThread* thread, QString output);
};
#endif // HOSTSIGNALLER_H

View File

@ -1,11 +1,13 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "QCoreApplication"
#include "QTextBrowser" #include "QTextBrowser"
#include "QMessageBox" #include "QMessageBox"
#include "QComboBox" #include "QComboBox"
#include "QLineEdit" #include "QLineEdit"
#include "QTableWidget"
#include "QInputDialog" #include "QInputDialog"
#include <QCursor>
#include <Qt>
#include <Windows.h> #include <Windows.h>
#include <qdebug.h> #include <qdebug.h>
#include <Psapi.h> #include <Psapi.h>
@ -33,63 +35,82 @@ QString ProcessString(DWORD processId)
QString TextThreadString(TextThread* thread) QString TextThreadString(TextThread* thread)
{ {
ThreadParameter tp = thread->GetThreadParameter(); ThreadParameter tp = thread->GetThreadParameter();
return QString("%1:%2:%3:%4:%5:%6").arg( return QString("%1:%2:%3:%4:%5: ").arg(
QString::number(thread->Number()), QString::number(thread->Number()),
QString::number(tp.pid), QString::number(tp.pid),
QString::number(tp.hook, 16), QString::number(tp.hook, 16),
QString::number(tp.retn, 16), QString::number(tp.retn, 16),
QString::number(tp.spl, 16), QString::number(tp.spl, 16)
QString::fromWCharArray(Host::GetHookName(tp.pid, tp.hook).c_str()) ).toUpper() + QString::fromWCharArray(Host::GetHookName(tp.pid, tp.hook).c_str());
);
} }
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
ui(new Ui::MainWindow) ui(new Ui::MainWindow),
hostSignaller(new HostSignaller)
{ {
ui->setupUi(this); ui->setupUi(this);
mainWindow = this; mainWindow = this;
processCombo = mainWindow->findChild<QComboBox*>("processCombo"); processCombo = mainWindow->findChild<QComboBox*>("processCombo");
ttCombo = mainWindow->findChild<QComboBox*>("ttCombo"); ttCombo = mainWindow->findChild<QComboBox*>("ttCombo");
textOutput = this->findChild<QTextBrowser*>("textOutput"); textOutput = mainWindow->findChild<QTextBrowser*>("textOutput");
Host::Start(); Host::Start();
Host::RegisterProcessAttachCallback(AddProcess); hostSignaller->Initialize();
Host::RegisterProcessDetachCallback(RemoveProcess); connect(hostSignaller, &HostSignaller::AddProcess, this, &MainWindow::AddProcess);
Host::RegisterThreadCreateCallback(AddThread); connect(hostSignaller, &HostSignaller::RemoveProcess, this, &MainWindow::RemoveProcess);
Host::RegisterThreadRemoveCallback(RemoveThread); connect(hostSignaller, &HostSignaller::AddThread, this, &MainWindow::AddThread);
connect(hostSignaller, &HostSignaller::RemoveThread, this, &MainWindow::RemoveThread);
connect(hostSignaller, &HostSignaller::ThreadOutput, this, &MainWindow::ThreadOutput);
Host::Open(); Host::Open();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
Host::Close(); Host::Close();
delete hostSignaller;
delete ui; delete ui;
} }
void AddProcess(DWORD processId) void MainWindow::AddProcess(unsigned int processId)
{ {
processCombo->addItem(ProcessString(processId)); processCombo->addItem(ProcessString(processId), Qt::AlignHCenter);
} }
void RemoveProcess(DWORD processId) void MainWindow::RemoveProcess(unsigned int processId)
{ {
processCombo->removeItem(processCombo->findText(ProcessString(processId))); for (int i = 0; i < processCombo->count(); ++i)
if (processCombo->itemText(i).split(":")[0] == QString::number(processId))
processCombo->removeItem(i);
} }
void AddThread(TextThread* thread) void MainWindow::AddThread(TextThread* thread)
{ {
ttCombo->addItem(TextThreadString(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) void MainWindow::RemoveThread(TextThread* thread)
{ {
ttCombo->removeItem(ttCombo->findText(TextThreadString(thread))); for (int i = 0; i < ttCombo->count(); ++i)
if (ttCombo->itemText(i).split(":")[0] == QString::number(thread->Number()))
{
ttCombo->removeItem(i);
if (i == ttCombo->currentIndex())
{
ttCombo->setCurrentIndex(0);
on_ttCombo_activated(0);
}
}
}
void MainWindow::ThreadOutput(TextThread* thread, QString output)
{
if (TextThreadString(thread) == ttCombo->currentText())
{
textOutput->moveCursor(QTextCursor::End);
textOutput->insertPlainText(output);
textOutput->moveCursor(QTextCursor::End);
}
} }
void MainWindow::on_attachButton_clicked() void MainWindow::on_attachButton_clicked()
@ -104,5 +125,5 @@ void MainWindow::on_detachButton_clicked()
void MainWindow::on_ttCombo_activated(int index) void MainWindow::on_ttCombo_activated(int index)
{ {
textOutput->setText(QString::fromWCharArray(Host::GetThread(index)->GetStore().c_str())); textOutput->setText(QString::fromWCharArray(Host::GetThread(ttCombo->itemText(index).split(":")[0].toInt())->GetStore().c_str()));
} }

View File

@ -4,6 +4,7 @@
#include <QMainWindow> #include <QMainWindow>
#include <Windows.h> #include <Windows.h>
#include "../texthook/textthread.h" #include "../texthook/textthread.h"
#include "hostsignaller.h"
namespace Ui namespace Ui
{ {
@ -18,18 +19,20 @@ public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
QString ProcessOutput(TextThread *thread, QString output);
private slots: private slots:
void on_attachButton_clicked(); void on_attachButton_clicked();
void on_detachButton_clicked(); void on_detachButton_clicked();
void on_ttCombo_activated(int index); void on_ttCombo_activated(int index);
void AddProcess(unsigned int processId);
void RemoveProcess(unsigned int processId);
void AddThread(TextThread* thread);
void RemoveThread(TextThread* thread);
void ThreadOutput(TextThread* thread, QString output);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
HostSignaller* hostSignaller;
}; };
void AddProcess(DWORD processId);
void RemoveProcess(DWORD processId);
void AddThread(TextThread* thread);
void RemoveThread(TextThread* thread);
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -10,6 +10,11 @@
<height>600</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>NextHooker</string> <string>NextHooker</string>
</property> </property>
@ -33,6 +38,13 @@
<item> <item>
<widget class="QLineEdit" name="lineEdit"/> <widget class="QLineEdit" name="lineEdit"/>
</item> </item>
<item>
<widget class="QComboBox" name="processCombo">
<property name="insertPolicy">
<enum>QComboBox::InsertAtBottom</enum>
</property>
</widget>
</item>
<item> <item>
<widget class="QPushButton" name="attachButton"> <widget class="QPushButton" name="attachButton">
<property name="text"> <property name="text">
@ -40,9 +52,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QComboBox" name="processCombo"/>
</item>
<item> <item>
<widget class="QPushButton" name="detachButton"> <widget class="QPushButton" name="detachButton">
<property name="text"> <property name="text">
@ -79,6 +88,11 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -92,7 +106,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>800</width> <width>800</width>
<height>21</height> <height>23</height>
</rect> </rect>
</property> </property>
</widget> </widget>

View File

@ -45,10 +45,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID unused)
// jichi 8/24/2013: Create hidden window so that ITH can access timer and events // jichi 8/24/2013: Create hidden window so that ITH can access timer and events
dummyWindow = CreateWindowW(L"Button", L"InternalWindow", 0, 0, 0, 0, 0, 0, 0, hinstDLL, 0); dummyWindow = CreateWindowW(L"Button", L"InternalWindow", 0, 0, 0, 0, 0, 0, 0, hinstDLL, 0);
break; break;
case DLL_PROCESS_DETACH:
Host::Close();
DestroyWindow(dummyWindow);
break;
default: default:
break; break;
} }
@ -73,7 +69,6 @@ namespace Host
onAttach = onDetach = nullptr; onAttach = onDetach = nullptr;
onCreate = onRemove = nullptr; onCreate = onRemove = nullptr;
nextThreadNumber = 0; nextThreadNumber = 0;
// Console text thread
return true; return true;
} }
} }
@ -92,8 +87,9 @@ namespace Host
{ {
EnterCriticalSection(&hostCs); EnterCriticalSection(&hostCs);
running = false; running = false;
DestroyWindow(dummyWindow);
RemoveThreads([](auto one, auto two) { return true; }, {}); RemoveThreads([](auto one, auto two) { return true; }, {});
for (auto i : processRecordsByIds) UnregisterProcess(i.first); //for (auto i : processRecordsByIds) UnregisterProcess(i.first); // Artikash 7/24/2018 FIXME: This segfaults since UnregisterProcess invalidates the iterator
LeaveCriticalSection(&hostCs); LeaveCriticalSection(&hostCs);
DeleteCriticalSection(&hostCs); DeleteCriticalSection(&hostCs);
CloseHandle(preventDuplicationMutex); CloseHandle(preventDuplicationMutex);
@ -243,7 +239,8 @@ void RemoveThreads(bool(*RemoveIf)(ThreadParameter, ThreadParameter), ThreadPara
if (RemoveIf(i.first, cmp)) if (RemoveIf(i.first, cmp))
{ {
if (onRemove) onRemove(i.second); if (onRemove) onRemove(i.second);
delete i.second; //delete i.second; // Artikash 7/24/2018: FIXME: Qt GUI updates on another thread, so I can't delete this yet.
i.second->Reset(); // Temp workaround to free some memory.
removedThreads.push_back(i.first); removedThreads.push_back(i.first);
} }
for (auto i : removedThreads) textThreadsByParams.erase(i); for (auto i : removedThreads) textThreadsByParams.erase(i);

View File

@ -9,6 +9,7 @@
#include <Windows.h> #include <Windows.h>
#include "textthread.h" #include "textthread.h"
#include <string> #include <string>
#include <functional>
#include "../vnrhook/include/types.h" #include "../vnrhook/include/types.h"
struct ProcessRecord struct ProcessRecord
@ -20,8 +21,8 @@ struct ProcessRecord
HANDLE hostPipe; HANDLE hostPipe;
}; };
typedef void(*ProcessEventCallback)(DWORD pid); typedef std::function<void(DWORD)> ProcessEventCallback;
typedef void(*ThreadEventCallback)(TextThread*); typedef std::function<void(TextThread*)> ThreadEventCallback;
struct ThreadParameterHasher struct ThreadParameterHasher
{ {

View File

@ -7,6 +7,7 @@
#include <Windows.h> #include <Windows.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <functional>
struct ThreadParameter struct ThreadParameter
{ {
@ -23,7 +24,7 @@ struct ThreadParameter
}; };
class TextThread; class TextThread;
typedef std::wstring(*ThreadOutputCallback)(TextThread*, std::wstring data); typedef std::function<std::wstring(TextThread*, std::wstring)> ThreadOutputCallback;
//extern DWORD split_time,repeat_count,global_filter,cyclic_remove; //extern DWORD split_time,repeat_count,global_filter,cyclic_remove;