Skip to content

Commit

Permalink
fix memory corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
mgth committed May 7, 2024
1 parent 16e8a15 commit 53d6a66
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 511 deletions.
49 changes: 21 additions & 28 deletions LittleBigMouse.Hook/Daemon/LittleBigMouseDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,13 @@ void LittleBigMouseDaemon::Run(const std::string& path)

// pump messages
if(_hook)
_hook->RunThread();
_hook->Loop();

// wait remote server to stop
if(_remoteServer)
_remoteServer->Join();

// wait for mouse hook to stop
if(_hook)
{
_hook->Stop();
_hook->Join();
_remoteServer->Stop();
_remoteServer->Join();
}

// disconnect from events
Expand Down Expand Up @@ -191,7 +187,6 @@ void LittleBigMouseDaemon::ReceiveCommandMessage(tinyxml2::XMLElement* root, Rem
if(_hook && !_hook->Hooked())
{
LoadExcluded();
_hook->SetPriority(_engine->Layout.Priority);
if(!_paused)
_hook->Hook();
}
Expand All @@ -201,7 +196,6 @@ void LittleBigMouseDaemon::ReceiveCommandMessage(tinyxml2::XMLElement* root, Rem
{
if(_hook && _hook->Hooked())
{
_hook->SetPriority(Normal);
_hook->Unhook();
}
_paused = false;
Expand All @@ -212,15 +206,11 @@ void LittleBigMouseDaemon::ReceiveCommandMessage(tinyxml2::XMLElement* root, Rem

else if(strcmp(command, "Quit")==0)
{
if(_hook && _hook->Hooked())
if(_hook)
{
_hook->SetPriority(Normal);
_hook->Unhook();
_hook->Quit();
}
_paused = false;

if(_remoteServer)
_remoteServer->Stop();
}
}
}
Expand Down Expand Up @@ -341,59 +331,62 @@ void LittleBigMouseDaemon::ReceiveClientMessage(const std::string& message, Remo

void LittleBigMouseDaemon::LoadExcluded(const std::string& path)
{
PWSTR szPath = nullptr;

LPWSTR progDataPath = nullptr;
_excluded.clear();

if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &szPath)))
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &progDataPath)))
{
std::ifstream file;

//PathAppend(szPath, ToWString(path).c_str());
PathAppend(szPath, ToWString(path).data());
TCHAR szPath[MAX_PATH] = { '\0' };
PathCombine(szPath, progDataPath, ToWString(path).data());
CoTaskMemFree(progDataPath);

LOG_TRACE("Load Excluded : " << ToString(szPath));

file.open(szPath, std::ios::in);

std::string buffer;
std::string line;
while(std::getline(file, line))
{
LOG_TRACE("Excluded : " << line);

if(line.empty()) continue;
if(line[0] == ':') continue;

LOG_TRACE("Excluded : " << line);
_excluded.push_back(line);

}
CoTaskMemFree(szPath);
file.close();
}
else
{
LOG_DEBUG("Failed to load excluded");
}


}

void LittleBigMouseDaemon::LoadExcluded()
{
LoadExcluded(R"(\Mgth\LittleBigMouse\Excluded.txt)");
LoadExcluded(R"(.\Mgth\LittleBigMouse\Excluded.txt)");
}

void LittleBigMouseDaemon::LoadFromFile(const std::string& path)
{
auto wPath = ToWString(path);
PWSTR szPath;
LPWSTR progDataPath = nullptr;

if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &szPath)))
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &progDataPath)))
{
std::ifstream file;

PathAppend(szPath, wPath.c_str());
TCHAR szPath[MAX_PATH] = { '\0' };
PathCombine(szPath, progDataPath, ToWString(path).data());
CoTaskMemFree(progDataPath);

file.open(szPath, std::ios::in);

std::string buffer;
std::string line;
while(file)
{
Expand Down
96 changes: 42 additions & 54 deletions LittleBigMouse.Hook/Hook/Hooker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,120 +53,108 @@ void Hooker::DoHook()
HookEventSystemDesktopSwitch();
HookDisplayChange();

}
}


void Hooker::DoUnhook()
{
UnhookMouse();

UnhookFocusEvent();
//UnhookEventSystemDesktopSwitch();
UnhookEventSystemDesktopSwitch();
UnhookDisplayChange();

_instance.store(nullptr);
}

int Hooker::Loop()
bool Hooker::PumpMessages()
{
MSG msg;

LOG_TRACE("<Hook:Start>");


auto ret = GetMessage(&msg, nullptr, 0, 0);
while (ret)
while (ret>=0)
{
if (msg.message == WM_QUIT) {
LOG_TRACE("<Hook:Quit>");
return false;
}
if (msg.message == WM_BREAK_LOOP) {
LOG_TRACE("<Hook:Break Loop>");
return true;
}

// if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
TranslateMessage(&msg);
DispatchMessage(&msg);

if(ret == -1)
{
LOG_DEBUG("<Hook:Error>");
(int)msg.wParam;
}
ret = GetMessage(&msg, nullptr, 0, 0);
}

return (int)msg.wParam;
}


void Hooker::Loop2()
{
LOG_TRACE("<Hook:Start>");

MSG msg;
int ret = PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE);
if(ret == 0)
{
ret = GetMessage(&msg, nullptr, 0, 0);
}

//while we do not close our application
while (ret >= 0 && msg.message != WM_QUIT)
{
LOG_TRACE("msg : " << msg.message);

TranslateMessage(&msg);
DispatchMessage(&msg);

ret = GetMessage(&msg, nullptr, 0, 0);
}

if(ret == -1)
{
LOG_DEBUG("<Hook:Error>");
return false;
}

return false;
}

void Hooker::RunThread()


void Hooker::Loop()
{
_currentThreadId = GetCurrentThreadId();

while(!Stopping())
bool stopping = false;
while(!stopping)
{
DoSetPriority(_priority);
LOG_TRACE("SetPriority");


DoHook();
LOG_TRACE("Hook");

Loop();
LOG_TRACE("Loop");
stopping = !PumpMessages();

DoUnhook();
LOG_TRACE("Unhook");

DoSetPriority(Below);
LOG_TRACE("SetPriority");
}

LOG_TRACE("<Hook:Stopped>");
}


void Hooker::QuitLoop() const
void Hooker::BreakLoop() const
{
if (PostThreadMessage(_currentThreadId, WM_QUIT, 0, 0))
if (PostThreadMessage(_currentThreadId, WM_BREAK_LOOP, 0, 0))
{
LOG_TRACE("<hook:quit>");
LOG_TRACE("<Hook:BreakLoop>");
}
}

void Hooker::DoStop()
bool Hooker::Hooked() const
{
QuitLoop();
return _hookMouse && _mouseHookId;
}

void Hooker::OnStopped()
void Hooker::Hook()
{
_hookMouse = true;
BreakLoop();
}

bool Hooker::Hooked() const
void Hooker::Unhook()
{
return _hookMouse && _mouseHookId;
_hookMouse = false;
BreakLoop();
}

void Hooker::Quit() const
{
if (PostThreadMessage(_currentThreadId, WM_QUIT, 0, 0))
{
LOG_TRACE("<Hook:BreakLoop>");
}
}


Expand Down
25 changes: 9 additions & 16 deletions LittleBigMouse.Hook/Hook/Hooker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
class MouseEventArg;
class MouseEngine;

#define WM_CUSTOM_MESSAGE (WM_APP + 1)
#define WM_BREAK_LOOP (WM_APP + 1)

class Hooker final : public ThreadHost
class Hooker final
{
static std::atomic<Hooker*> _instance;

Expand Down Expand Up @@ -46,10 +46,9 @@ class Hooker final : public ThreadHost
void HookWindows();
void UnhookWindows();

static int Loop();
static void Loop2();
static bool PumpMessages();

void QuitLoop() const;
void BreakLoop() const;

static ATOM RegisterClassLbm(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance);
Expand All @@ -70,23 +69,17 @@ class Hooker final : public ThreadHost
HWND Hwnd() const { return _hwnd; }
static Hooker* Instance() { return _instance.load(); }

void RunThread() override;
void DoStop() override;
void OnStopped() override;
void Loop();

bool Hooked() const;

void SetPriority(const Priority priority) { _priority = priority; }

void Hook() {
_hookMouse = true;
QuitLoop();
}
void Hook();

void Unhook() {
_hookMouse = false;
QuitLoop();
}
void Unhook();

void Quit() const;

private:
static LRESULT __stdcall MouseCallback(const int nCode, const WPARAM wParam, const LPARAM lParam);
Expand Down
9 changes: 6 additions & 3 deletions LittleBigMouse.Hook/Hook/HookerDisplayChanged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ ATOM Hooker::RegisterClassLbm(HINSTANCE hInstance)
wcex.hCursor = nullptr; //LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = nullptr; //(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = nullptr; //MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
wcex.lpszClassName = L"HookerDisplayChange";
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = nullptr; //LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassExW(&wcex);
}

BOOL Hooker::InitInstance(HINSTANCE hInstance)
{
_hInst = hInstance;
_hInst = hInstance;

HWND hWnd = CreateWindowW(L"HookerDisplayChange", szWindowClass, WS_OVERLAPPEDWINDOW,
const HWND hWnd = CreateWindowW(szWindowClass, szWindowClass, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

if (!hWnd)
Expand All @@ -54,6 +54,9 @@ bool Hooker::HookDisplayChange()

const auto hInstance = GetModuleHandle(nullptr);

//LoadStringW(hInstance, IDS_APP_TITLE, szWindowClass, MAX_LOADSTRING);
wcscpy_s(szWindowClass, L"HookerDisplayChange");

auto c = RegisterClassLbm(hInstance);

if (!InitInstance (hInstance))
Expand Down
Loading

0 comments on commit 53d6a66

Please sign in to comment.