Could not open process: The parameter is incorrect. (error 87)
Hi All,
I am trying to use the package resusage, and I am getting the below error message, googled for this error and tried all the solution provided still facing the same issue, hence requesting your help.
Solution Provided
dism.exe /image:C: /cleanup-image /revertpendingactions dism.exe /online /cleanup-image /startcomponentcleanup
Error
std.windows.syserror.WindowsException@resusage\common.d(25): Could not open process: The parameter is incorrect. (error 87)
From, Vino.B
Hi. Probably the process doesn't exist by the time you request the information. E.g. it exits too soon. The message is somewhat misleading but that's what WinAPI returns. Need more context on what's happening in your app. It's also possible that you're providing something else than process id to the library API. Make sure it's a process id and not, for example, a thread id.
Hi All,
Please find the details, we are trying to capture the CPU and Memory utlization of a process id where the process is created by below
Program
import resusage.memory;
import resusage.cpu;
CPUWatcher cpuInfo;
result = std.process.pipeProcess(_program ~ _args, redirect, _env, _config, _workdir);
result = std.process.pipeShell(_command, redirect, _env, _config, _workdir, _shellPath);
int spwanPID = (result.pid).processID;
cpuInfo = new ProcessCPUWatcher(spwanPID);
auto memInfo = processMemInfo(spwanPID);
writeln("[MEMORY]: %s bytes\t".format(memInfo.usedRAM));
writeln("[CPU]: %s%%\t".format(cpuInfo.current()));
The above code works file for the process which is created by std.process.pipeProcess where same is failing for the process created by std.process.pipeShell.
cpuInfo = new ProcessCPUWatcher(spwanPID); // this is child process id
if we replace this with the parent process id it works , the same do not work using child process id, we actually need the CUP and Memory of the child process in both the case.(std.process.pipeProcess , std.process.pipeShell)
cpuInfo = new ProcessCPUWatcher(thisProcessID); // parent process id
I couldn't reproduce it.
I wrote the following program:
import std.stdio;
import resusage.memory;
import resusage.cpu;
import std.process;
import std.format;
void main()
{
CPUWatcher cpuInfo;
string appPath = "testd.exe";
//auto result = std.process.pipeProcess([appPath]);
auto result = std.process.pipeShell(appPath);
int spwanPID = (result.pid).processID;
writeln("Pid: ", spwanPID);
cpuInfo = new ProcessCPUWatcher(spwanPID);
auto memInfo = processMemInfo(spwanPID);
writeln("[MEMORY]: %s bytes\t".format(memInfo.usedRAM));
writeln("[CPU]: %s%%\t".format(cpuInfo.current()));
wait(result.pid);
}
With testd.exe being another D program
int main()
{
return 0;
}
If you provide reproducible case, I can look at it.
Please try the below code
` import core.time : minutes, seconds; import resusage.cpu; import resusage.memory; import std.container.array; import std.conv: to; import std.datetime.stopwatch : StopWatch, AutoStart; import std.datetime.systime: Clock, SysTime; import std.process; import std.range: empty; import std.stdio: writeln;
void main () { CPUWatcher cpuInfo; SysTime start, end; long duration; ProcessPipes result; Array!string output; Array!string errors;
auto sw = StopWatch(AutoStart.no);
sw.start();
start = Clock.currTime();
result = std.process.pipeShell(escapeShellCommand(["whoami", "/?"]), Redirect.stdout | Redirect.stderr);
int spwanPID = (result.pid).processID;
cpuInfo = new ProcessCPUWatcher(spwanPID);
auto memInfo = processMemInfo(spwanPID);
result.stdout.flush(); result.stderr.flush();
foreach (line; result.stdout.byLine) if(!empty(line)) { output.insertBack(line.to!string); }
foreach (line; result.stderr.byLine) if(!empty(line)) { errors.insertBack(line.to!string); }
int exitCode = wait(result.pid);
scope(exit) { wait(result.pid); sw.stop(); }
end = Clock.currTime();
duration = sw.peek.total!"msecs";
writeln(memInfo.usedRAM);
writeln(cpuInfo.current());
writeln(output[]);
writeln();
writeln(errors[]);
} `
The process you're trying to get the information from has already exited when you request the cpu time info. This library is for tracking long living processes.