attach to command line args

This commit is contained in:
Akash Mozumdar 2019-02-18 22:39:04 -05:00
parent 25d2811c0f
commit 1f5edecbf6
3 changed files with 25 additions and 5 deletions

View File

@ -265,6 +265,15 @@ namespace Util
return {}; return {};
} }
std::vector<DWORD> GetAllProcessIds()
{
std::vector<DWORD> processIds(10000);
DWORD spaceUsed = 0;
EnumProcesses(processIds.data(), 10000 * sizeof(DWORD), &spaceUsed);
processIds.resize(spaceUsed / sizeof(DWORD));
return processIds;
}
std::optional<std::wstring> GetClipboardText() std::optional<std::wstring> GetClipboardText()
{ {
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {}; if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {};

View File

@ -7,6 +7,7 @@ namespace Util
{ {
std::optional<std::wstring> GetModuleFilename(DWORD processId, HMODULE module = NULL); std::optional<std::wstring> GetModuleFilename(DWORD processId, HMODULE module = NULL);
std::optional<std::wstring> GetModuleFilename(HMODULE module = NULL); std::optional<std::wstring> GetModuleFilename(HMODULE module = NULL);
std::vector<DWORD> GetAllProcessIds();
std::optional<std::wstring> GetClipboardText(); std::optional<std::wstring> GetClipboardText();
std::optional<std::wstring> StringToWideString(const std::string& text, UINT encoding = CP_UTF8); std::optional<std::wstring> StringToWideString(const std::string& text, UINT encoding = CP_UTF8);
// return true if repetition found (see https://github.com/Artikash/Textractor/issues/40) // return true if repetition found (see https://github.com/Artikash/Textractor/issues/40)

View File

@ -5,7 +5,7 @@
#include "extenwindow.h" #include "extenwindow.h"
#include "misc.h" #include "misc.h"
#include "host/util.h" #include "host/util.h"
#include <Psapi.h> #include <shellapi.h>
#include <winhttp.h> #include <winhttp.h>
#include <QFormLayout> #include <QFormLayout>
#include <QPushButton> #include <QPushButton>
@ -58,6 +58,18 @@ MainWindow::MainWindow(QWidget *parent) :
current = &Host::GetThread(Host::console); current = &Host::GetThread(Host::console);
Host::AddConsoleOutput(ABOUT); Host::AddConsoleOutput(ABOUT);
std::vector<DWORD> processIds = Util::GetAllProcessIds();
std::vector<std::wstring> processNames;
for (auto processId : processIds) processNames.emplace_back(Util::GetModuleFilename(processId).value_or(L""));
int argc;
std::unique_ptr<LPWSTR[], Functor<LocalFree>> argv(CommandLineToArgvW(GetCommandLineW(), &argc));
for (int i = 0; i < argc; ++i)
if (std::wstring arg = argv[i]; arg[0] == L'/' || arg[0] == L'-')
if (arg[1] == L'P')
if (DWORD processId = _wtoi(arg.substr(2).c_str())) Host::InjectProcess(processId);
else for (int i = 0; i < processIds.size(); ++i)
if (processNames[i].find(L"\\" + arg.substr(2)) != std::wstring::npos) Host::InjectProcess(processIds[i]);
std::thread([] std::thread([]
{ {
using InternetHandle = AutoHandle<Functor<WinHttpCloseHandle>>; using InternetHandle = AutoHandle<Functor<WinHttpCloseHandle>>;
@ -193,10 +205,8 @@ std::array<InfoForExtension, 10> MainWindow::GetMiscInfo(TextThread& thread)
void MainWindow::AttachProcess() void MainWindow::AttachProcess()
{ {
QMultiHash<QString, DWORD> allProcesses; QMultiHash<QString, DWORD> allProcesses;
DWORD allProcessIds[5000] = {}, spaceUsed = 0; for (auto processId : Util::GetAllProcessIds())
EnumProcesses(allProcessIds, sizeof(allProcessIds), &spaceUsed); if (auto processName = Util::GetModuleFilename(processId)) allProcesses.insert(QFileInfo(S(processName.value())).fileName(), processId);
for (int i = 0; i < spaceUsed / sizeof(DWORD); ++i)
if (auto processName = Util::GetModuleFilename(allProcessIds[i])) allProcesses.insert(QFileInfo(S(processName.value())).fileName(), allProcessIds[i]);
QStringList processList(allProcesses.uniqueKeys()); QStringList processList(allProcesses.uniqueKeys());
processList.sort(Qt::CaseInsensitive); processList.sort(Qt::CaseInsensitive);