How to make a C ++ program listen to system commands

Recently I was asked how this is possible, if possible. I am not sure if this is possible, and I have been looking for it for a while.

Basically, take Windows , for example, there is a system command to turn off the computer. Let's say shutdown -s -t 30 -c "Shutdown"

Is there a way to write a program that will listen for the shutdown and then run shutdown -a in response to canceling this command?

In short, can you make it listen to certain system commands on the computer and execute commands in response?

+6
source share
2 answers

It is really possible. However, your example is probably not the best to describe a general problem. Windows has session events that applications can listen to, and shutdown is one of them . And in the end, shutdown.exe is not the only application that can ask Windows to shut down.

In general, however, applications "listening" for executable commands will have to integrate closely with the operating system. You can imagine that anti-virus software does just that and much more to prevent the execution of "bad" programs. I am not familiar with Windows technology, but I assume that this requires a Windows system call that executes binary files. Of course, this will require "administrative" permissions, and you may even need to write a kernel module.

+1
source

You can go as follows:

  • Check if the program is completed.
  • If so, cancel the program exit call and execute system("shutdown -a);
  • Otherwise, do what you want.

Well, maybe there are many more complicated ways to do this. And I agree with the answer @VladLazarenko; programs such as antiviruses continue to listen to system commands. But I assume that this will require a deep knowledge of the API. This is an easy way to do this. If you're not a newbie, you should visit this link, http://www.codeproject.com/Articles/2082/API-hooking-revealed (from @RedX's comment). Good luck! :)

+1
source

All Articles