diff --git a/packages/tosu/src/objects/instanceManager/instanceManager.ts b/packages/tosu/src/objects/instanceManager/instanceManager.ts index 890421ca..e0a335dd 100644 --- a/packages/tosu/src/objects/instanceManager/instanceManager.ts +++ b/packages/tosu/src/objects/instanceManager/instanceManager.ts @@ -30,8 +30,8 @@ export class InstanceManager { continue; } - const cmdLine = Process.getProcessCommandLine(processId); const osuInstance = new OsuInstance(processId); + const cmdLine = osuInstance.process.getProcessCommandLine(); if (cmdLine.includes('-spectateclient')) { const [_, ipcId] = cmdLine.split(' '); diff --git a/packages/tsprocess/lib/functions.cc b/packages/tsprocess/lib/functions.cc index 549c9fa2..98282baf 100644 --- a/packages/tsprocess/lib/functions.cc +++ b/packages/tsprocess/lib/functions.cc @@ -374,11 +374,12 @@ Napi::Value getProcessCommandLine(const Napi::CallbackInfo &args) { return env.Null(); } - auto pId = args[0].As().Uint32Value(); + auto handle = + reinterpret_cast(args[0].As().Uint32Value()); // Convert wide character array to a UTF-8 encoded string std::wstring_convert> converter; - std::string utf8Str = converter.to_bytes(memory::get_proc_command_line(pId)); + std::string utf8Str = converter.to_bytes(memory::get_proc_command_line(handle)); return Napi::String::From(env, Napi::String::New(env, utf8Str)); } diff --git a/packages/tsprocess/lib/process.h b/packages/tsprocess/lib/process.h index 55bcea68..2e3517f9 100644 --- a/packages/tsprocess/lib/process.h +++ b/packages/tsprocess/lib/process.h @@ -147,62 +147,50 @@ inline std::string get_process_path(HANDLE handle) { return filePath; } -inline std::wstring get_proc_command_line(uint32_t pid) +inline std::wstring get_proc_command_line(HANDLE process) { std::wstring commandLine; - // Open a handle to the process - HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); - if (process == NULL) + // Get the address of the PEB + PROCESS_BASIC_INFORMATION pbi = {}; + NTSTATUS status = NtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL); + if (status != 0) { - DWORD err = GetLastError(); - std::cerr << "failed to open the process, error: " << err << std::endl; + std::cerr << "failed to query the process, error: " << status << std::endl; } else { - // Get the address of the PEB - PROCESS_BASIC_INFORMATION pbi = {}; - NTSTATUS status = NtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL); - if (status != 0) + // Get the address of the process parameters in the PEB + PEB peb = {}; + if (!ReadProcessMemory(process, pbi.PebBaseAddress, &peb, sizeof(peb), NULL)) { - std::cerr << "failed to query the process, error: " << status << std::endl; + DWORD err = GetLastError(); + std::cerr << "failed to read the process PEB, error: " << err << std::endl; } else { - // Get the address of the process parameters in the PEB - PEB peb = {}; - if (!ReadProcessMemory(process, pbi.PebBaseAddress, &peb, sizeof(peb), NULL)) + // Get the command line arguments from the process parameters + RTL_USER_PROCESS_PARAMETERS params = {}; + if (!ReadProcessMemory(process, peb.ProcessParameters, ¶ms, sizeof(params), NULL)) { DWORD err = GetLastError(); - std::cerr << "failed to read the process PEB, error: " << err << std::endl; + std::cerr << "failed to read the process params, error: " << err << std::endl; } else { - // Get the command line arguments from the process parameters - RTL_USER_PROCESS_PARAMETERS params = {}; - if (!ReadProcessMemory(process, peb.ProcessParameters, ¶ms, sizeof(params), NULL)) + UNICODE_STRING &commandLineArgs = params.CommandLine; + std::vector buffer(commandLineArgs.Length / sizeof(WCHAR)); + if (!ReadProcessMemory(process, commandLineArgs.Buffer, buffer.data(), commandLineArgs.Length, NULL)) { DWORD err = GetLastError(); - std::cerr << "failed to read the process params, error: " << err << std::endl; + std::cerr << "failed to read the process command line, error: " << err << std::endl; } else { - UNICODE_STRING &commandLineArgs = params.CommandLine; - std::vector buffer(commandLineArgs.Length / sizeof(WCHAR)); - if (!ReadProcessMemory(process, commandLineArgs.Buffer, buffer.data(), commandLineArgs.Length, NULL)) - { - DWORD err = GetLastError(); - std::cerr << "failed to read the process command line, error: " << err << std::endl; - } - else - { - commandLine.assign(buffer.data(), buffer.size()); - } + commandLine.assign(buffer.data(), buffer.size()); } } } - - CloseHandle(process); } return commandLine; diff --git a/packages/tsprocess/src/process.ts b/packages/tsprocess/src/process.ts index 83911a7b..6f735d80 100644 --- a/packages/tsprocess/src/process.ts +++ b/packages/tsprocess/src/process.ts @@ -28,6 +28,10 @@ export class Process { return ProcessUtils.getProcessPath(this.handle); } + getProcessCommandLine(): string { + return ProcessUtils.getProcessCommandLine(this.handle); + } + readByte(address: number): number { return ProcessUtils.readByte(this.handle, address); } @@ -159,8 +163,4 @@ export class Process { static getProcesses(): Array { return ProcessUtils.getProcesses(); } - - static getProcessCommandLine(pid: number): string { - return ProcessUtils.getProcessCommandLine(pid); - } }