| 31 | } |
| 32 | |
| 33 | int main(int argc, char** argv) |
| 34 | { |
| 35 | try { |
| 36 | // Initial logging |
| 37 | LOGI << "Injector process started" << std::endl; |
| 38 | |
| 39 | // Initialize arguments |
| 40 | if (auto res = clio::Options::Init(argc, argv, true)) { |
| 41 | return *res; |
| 42 | } |
| 43 | auto& opts = clio::Options::Get(); |
| 44 | |
| 45 | stdfs::path injectorPath; |
| 46 | { |
| 47 | std::vector<char> buffer(MAX_PATH); |
| 48 | auto size = GetModuleFileNameA(NULL, buffer.data(), static_cast<DWORD>(buffer.size())); |
| 49 | if (size == 0) { |
| 50 | LOGE << "Failed to get this executable path." << std::endl;; |
| 51 | } |
| 52 | injectorPath = std::string(buffer.begin(), buffer.begin() + size); |
| 53 | injectorPath = injectorPath.parent_path(); |
| 54 | } |
| 55 | |
| 56 | // DLL to inject |
| 57 | const stdfs::path libraryPath = injectorPath / std::format("FlashInjectorLibrary-{}.dll", PM_BUILD_PLATFORM); |
| 58 | |
| 59 | const bool weAre32Bit = PM_BUILD_PLATFORM == "Win32"s; |
| 60 | |
| 61 | if (!stdfs::exists(libraryPath)) { |
| 62 | LOGE << "Cannot find library: " << libraryPath << std::endl;; |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | LOGI << "Waiting for processes that match executable name..." << std::endl; |
| 67 | |
| 68 | std::mutex targetModuleNameMtx; |
| 69 | std::string targetModuleName; |
| 70 | // thread whose sole job is to read from stdin without blocking the main thread |
| 71 | std::thread{ [&] { |
| 72 | std::string line; |
| 73 | while (true) { |
| 74 | std::getline(std::cin, line); |
| 75 | std::lock_guard lk{ targetModuleNameMtx }; |
| 76 | targetModuleName = str::ToLower(line); |
| 77 | } |
| 78 | } }.detach(); |
| 79 | |
| 80 | // keep a set of processes already attached so we don't attempt multiple attachments per process |
| 81 | std::unordered_set<DWORD> processesAttached; |
| 82 | while (true) { |
| 83 | // atomic load target name and skip if empty string |
| 84 | const auto tgt = [&] { std::lock_guard lk{ targetModuleNameMtx }; return targetModuleName; }(); |
| 85 | if (!tgt.empty()) { |
| 86 | for (auto&& [processId, processName] : LibraryInject::GetProcessNames()) { |
| 87 | const auto processNameLower = str::ToLower(processName); |
| 88 | if (processNameLower == tgt && !processesAttached.contains(processId)) { |
| 89 | auto hProcTarget = win::OpenProcess(processId, PROCESS_QUERY_LIMITED_INFORMATION); |
| 90 | if (win::ProcessIs32Bit(hProcTarget) == weAre32Bit) { |
nothing calls this directly
no test coverage detected