Skip to content

Commit

Permalink
fix: do not open another handle to process
Browse files Browse the repository at this point in the history
  • Loading branch information
KotRikD committed Mar 7, 2024
1 parent c461d24 commit de7ee37
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');

Expand Down
5 changes: 3 additions & 2 deletions packages/tsprocess/lib/functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,12 @@ Napi::Value getProcessCommandLine(const Napi::CallbackInfo &args) {
return env.Null();
}

auto pId = args[0].As<Napi::Number>().Uint32Value();
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Uint32Value());

// Convert wide character array to a UTF-8 encoded string
std::wstring_convert<std::codecvt_utf8<wchar_t>> 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));
}
Expand Down
52 changes: 20 additions & 32 deletions packages/tsprocess/lib/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params, 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, &params, sizeof(params), NULL))
UNICODE_STRING &commandLineArgs = params.CommandLine;
std::vector<WCHAR> 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<WCHAR> 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;
Expand Down
8 changes: 4 additions & 4 deletions packages/tsprocess/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -159,8 +163,4 @@ export class Process {
static getProcesses(): Array<ProcessInfo> {
return ProcessUtils.getProcesses();
}

static getProcessCommandLine(pid: number): string {
return ProcessUtils.getProcessCommandLine(pid);
}
}

0 comments on commit de7ee37

Please sign in to comment.