forked from Public-Mirror/Textractor
refactor
This commit is contained in:
parent
7c3ca92db3
commit
332e5aecaf
@ -21,11 +21,7 @@ namespace
|
||||
|
||||
LONG WINAPI ExceptionLogger(EXCEPTION_POINTERS* exception)
|
||||
{
|
||||
thread_local static auto _ = std::invoke([]
|
||||
{
|
||||
std::set_terminate(Terminate);
|
||||
return 0;
|
||||
});
|
||||
thread_local static auto _ = std::set_terminate(Terminate);
|
||||
|
||||
MEMORY_BASIC_INFORMATION info = {};
|
||||
VirtualQuery(exception->ExceptionRecord->ExceptionAddress, &info, sizeof(info));
|
||||
@ -47,10 +43,5 @@ namespace
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
auto _ = std::invoke([]
|
||||
{
|
||||
AddVectoredExceptionHandler(FALSE, ExceptionLogger);
|
||||
SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); });
|
||||
return 0;
|
||||
});
|
||||
auto _ = (AddVectoredExceptionHandler(FALSE, ExceptionLogger), SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); }));
|
||||
}
|
||||
|
@ -120,16 +120,6 @@ namespace
|
||||
processRecordsByIds->erase(processId);
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void StartCapturingClipboard()
|
||||
{
|
||||
SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE)
|
||||
if (auto text = Util::GetClipboardText()) Host::GetThread(Host::clipboard)->AddSentence(text.value());
|
||||
return CallNextHookEx(NULL, statusCode, wParam, lParam);
|
||||
}, NULL, GetCurrentThreadId());
|
||||
}
|
||||
}
|
||||
|
||||
namespace Host
|
||||
@ -144,8 +134,14 @@ namespace Host
|
||||
processRecordsByIds->try_emplace(console.processId, console.processId, INVALID_HANDLE_VALUE);
|
||||
textThreadsByParams->insert({ console, std::make_unique<TextThread>(console, HookParam{}, CONSOLE) });
|
||||
textThreadsByParams->insert({ Host::clipboard, std::make_unique<TextThread>(Host::clipboard, HookParam{}, CLIPBOARD) });
|
||||
StartCapturingClipboard();
|
||||
CreatePipe();
|
||||
|
||||
SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE)
|
||||
if (auto text = Util::GetClipboardText()) Host::GetThread(Host::clipboard)->AddSentence(text.value());
|
||||
return CallNextHookEx(NULL, statusCode, wParam, lParam);
|
||||
}, NULL, GetCurrentThreadId());
|
||||
}
|
||||
|
||||
bool InjectProcess(DWORD processId, DWORD timeout)
|
||||
@ -174,7 +170,7 @@ namespace Host
|
||||
#endif
|
||||
if (LPVOID remoteData = VirtualAllocEx(process, nullptr, (location.size() + 1) * sizeof(wchar_t), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE))
|
||||
{
|
||||
WriteProcessMemory(process, remoteData, location.c_str(), location.size() * 2 + 2, nullptr);
|
||||
WriteProcessMemory(process, remoteData, location.c_str(), (location.size() + 1) * sizeof(wchar_t), nullptr);
|
||||
if (AutoHandle<> thread = CreateRemoteThread(process, nullptr, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, remoteData, 0, nullptr))
|
||||
{
|
||||
WaitForSingleObject(thread, timeout);
|
||||
|
@ -6,12 +6,9 @@ namespace Util
|
||||
{
|
||||
std::optional<std::wstring> GetModuleFilename(DWORD processId, HMODULE module)
|
||||
{
|
||||
if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))
|
||||
{
|
||||
std::vector<wchar_t> buffer(MAX_PATH);
|
||||
std::vector<wchar_t> buffer(MAX_PATH);
|
||||
if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))
|
||||
if (GetModuleFileNameExW(process, module, buffer.data(), MAX_PATH)) return buffer.data();
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
@ -27,10 +24,10 @@ namespace Util
|
||||
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {};
|
||||
if (!OpenClipboard(NULL)) return {};
|
||||
|
||||
std::optional<std::wstring> ret;
|
||||
if (AutoHandle<Functor<GlobalUnlock>> clipboard = GetClipboardData(CF_UNICODETEXT)) ret = (wchar_t*)GlobalLock(clipboard);
|
||||
std::optional<std::wstring> text;
|
||||
if (AutoHandle<Functor<GlobalUnlock>> clipboard = GetClipboardData(CF_UNICODETEXT)) text = (wchar_t*)GlobalLock(clipboard);
|
||||
CloseClipboard();
|
||||
return ret;
|
||||
return text;
|
||||
}
|
||||
|
||||
std::optional<std::wstring> StringToWideString(std::string text, UINT encoding)
|
||||
@ -45,7 +42,7 @@ namespace Util
|
||||
wchar_t* end = text.data() + text.size();
|
||||
for (int len = text.size() / 3; len > 6; --len)
|
||||
if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0)
|
||||
return true | RemoveRepetition(text = end - len);
|
||||
return RemoveRepetition(text = end - len), true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user