I have some code that uses _spawnl to spawn a process. It works great. However, we've run into a problem where that process hangs and we want to kill it.
Given the PID (as in the # that appears in task manager), I can get the handle to the process using OpenProcess. As far as I can tell, this just returns the exactly same value that _spawnl does (makes sense since the documentation says _spawnl calls CreateProcess behind the scenes).
So far so good. The problem comes with I call TerminateProcess. It returns with the error "Access is denied." (that is what I get back from FormatMessage which presumably corresponds to ERROR_ACCESS_DENIED).
The question is why can't I terminate this process that I started (in the same process that I'm trying to terminate it from)? Presumably I could pass the PROCESS_TERMINATE permission to CreateProcess to allow this...if I was using CreateProcess. Is there a means to specify (when calling _spawnl) that I want permission to kill the process later? It seems like that would be the default.
My concern is that perhaps I'm barking up the wrong tree here. The documentation on TerminateProcess indicates that I need the PROCESS_TERMINATE permission but it doesn't exactly say what error message I will get if I don't have it. I'm just assuming that "Access is denied" indicates I don't have this permission...but perhaps I'm drawing the wrong conclusion here.
If anybody has any suggestions, I'm all ears. Ideally I'd like to be able to terminate a process spawned by _spawnl. Alternatives that involve rewriting the code (e.g. to use CreateProcess) are also welcome although I am concerned that I will rewrite the whole thing and it still won't work (because I'm misdiagnosing the issue).
Thanks.