Im a gamer, but I confess i need to play videogames a bit less. I had the idea of writing a program that will run in the background and close the video game when i try to start it up. There is one thing I need though: a function that will close the program. All my web searches are inconclusive, because when you searh "ending external programs with C++" you get everyhig. There is one function that did stick out: TerminateProcess(). But it closes the program leaving the residue of the memory it was unable to close. Not only that, but i couldn't understand any of the syntax (they may have been using OOP) so i couldn't exactly pinpoint the part that identifies what program the function is supposed to close. If any of you could help me out, i would very much appreciate it.
TerminateProcess() is the way to go. You need a handle to the process being terminated. You can enumerate periodically the active processes using one of the approaches listed here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684865(v=vs.85).aspx . Once you find your process by executable name, terminate it.
Note that if TerminateProcess() is successful, there is no memory residue. If you have evidence of this "residue", post it.
To build on what webJose said TerminateProcess() cleans all of the memory that was allocated to that process. What you are reading means that the process is not given the chance to delete temp files and things like that. If the game spawned a child process that was not able to check back with it's parent then that process might be orphaned but I doubt that anything modern would have that problem.
What ever doesn't get picked up by TerminateProcess() will get deallocated by the systems Garbage collector. Some random entity might remain as part of a DLL or something but that's hardley significant. The other options involve thread injection which we can explore if you would like to.
well, although what webJose provided is nice, I still don't entirely grasp the entire syntax/concept.
for example:
for enumProcess(), does the memory pointer "*processIDs" contain the array of processes or does the DWORD?
and once you have that array, do you use the address (&processhandle) or the actual handle to end the process. How do you even identify what program's process you want to shut down (and then locate it in the array)?
How do we tell terminate process what process to terminate? I assume the process handle goes into the parentheses, but i don't want to jack up my computer.
These are things I can't find answers to. I really appreciate your help.
off to do homework (online... hope i don't type in minecraft.net... :/ I'm contemplating it, lol)
EnumProcesses() fills an array with process ID's. You can use those process ID's with OpenProcess() to obtain the process handle. With that handle you can terminate the process using TerminateProcess().
Of course that you don't want to terminate all processes and I guess you'll differentiate by process name (the name of the executable). So, after getting the process handle using OpenProcess(), use the handle with QueryFullProcessImageName(). This tells you the executable name. If the name matches the name(s) you are expecting, call TerminateProcess().
I've got a working function that does just this. Would you like me to post it or would you like to struggle through it?
I use
EnumProcesses()
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE,...)
GetModuleBaseName()
TerminateProcess()
CloseHandle()
Along every step I check for the success of the operation. For example, if OpenProcess returns 0, then it means that this isn't a process that we are interested in. In that case, we just move along to the next PID.