lolautoaccept/src/clientaccess_windows.cpp

115 lines
2.9 KiB
C++
Raw Permalink Normal View History

2022-08-24 16:12:03 +02:00
#include "clientaccess.h"
#include <cstring>
# include <windows.h>
# include <tlhelp32.h>
# include <tchar.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <Log.h>
#include "defer.h"
2023-05-31 22:22:23 +02:00
static const QString CLIENTNAME = "LeagueClientUx.exe";
2022-08-24 16:12:03 +02:00
2023-05-31 22:22:23 +02:00
static QString narrow(WCHAR* str, size_t len) {
QString out;
2022-08-24 16:12:03 +02:00
out.reserve(len);
for(uint32_t i = 0; i < len && str[i]; ++i) {
out.append(1, (char) str[i]);
}
return out;
}
2023-05-31 22:22:23 +02:00
static QString getProcessPath(DWORD dwPID) {
2022-08-24 16:12:03 +02:00
// Take a snapshot of all modules in the specified process.
HANDLE hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if(hModuleSnap == INVALID_HANDLE_VALUE) {
2023-05-31 22:22:23 +02:00
qCritical() << "CreateToolhelp32Snapshot (of modules) failed";
2022-08-24 16:12:03 +02:00
return {}; // empty string
}
defer( CloseHandle(hModuleSnap) );
// Set the size of the structure before using it.
MODULEENTRY32 me32;
me32.dwSize = sizeof(MODULEENTRY32);
// Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32First( hModuleSnap, &me32 ) ) {
2023-05-31 22:22:23 +02:00
qCritical() << "Module32First";
2022-08-24 16:12:03 +02:00
return {};
}
return narrow((WCHAR*) me32.szExePath, sizeof(me32.szExePath));
}
static std::shared_ptr<ClientAccess> findUsingLockfile(PROCESSENTRY32& proc) {
2023-05-31 22:22:23 +02:00
const QString exepath = getProcessPath(proc.th32ProcessID);
2022-08-24 16:12:03 +02:00
Log::note << "exepath: " << exepath;
// lockfile path
2023-05-31 22:22:23 +02:00
const QString lockfilepath = exepath.substr(0, exepath.rfind('\\')+1) + "lockfile"; // possible out of bounds
qDebug() << "Lockfile: " << lockfilepath;
2022-08-24 16:12:03 +02:00
std::ifstream lockfile(lockfilepath);
if(!lockfile) {
2023-05-31 22:22:23 +02:00
qCritical() << "lockfile could not be opend";
2022-08-24 16:12:03 +02:00
return nullptr;
}
return createFromLockfile(lockfile);
}
2022-08-24 19:24:18 +02:00
std::shared_ptr<ClientAccess> ClientAccess::find() {
2022-08-24 16:12:03 +02:00
// example code: https://docs.microsoft.com/de-de/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes
// Take a snapshot of all processes in the system.
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
2023-05-31 22:22:23 +02:00
qCritical() << "CreateToolhelp32Snapshot (of processes) failed";
2022-08-24 16:12:03 +02:00
return nullptr;
}
defer( CloseHandle(hProcessSnap) );
PROCESSENTRY32 pe32;
// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32)) {
2023-05-31 22:22:23 +02:00
qCritical() << "Process32First failed"; // show cause of failure
2022-08-24 16:12:03 +02:00
return nullptr;
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do {
2023-05-31 22:22:23 +02:00
QString exename = narrow((WCHAR*) pe32.szExeFile, sizeof(pe32.szExeFile));
2022-08-24 16:28:15 +02:00
// Log::note << "found process: " << exename;
2022-08-24 16:12:03 +02:00
if(exename == CLIENTNAME) {
2023-05-31 22:22:23 +02:00
qInfo() << CLIENTNAME << " found";
2022-08-24 16:12:03 +02:00
std::shared_ptr<ClientAccess> out;
out = findUsingLockfile(pe32);
if(out) {
return out;
}
}
} while(Process32Next(hProcessSnap, &pe32));
return nullptr;
}