What options do I have? (WinAPI and so on) Does my program receive any
notification as soon as another program uses OpenProcess?
No, OpenProcess
happens in the kernel, and runs completely without interaction with the process being opened. Since it is used by debuggers (intended for) and could happen when your process is stuck or hung, it would be unpractical to have some sort of need for your process to be "aware" that it's been opened.
Your only option (and that's a bad one) is to use some form of intercept on the OpenProcess
system call. Note that there are valid uses for OpenProcess
, and I think many virus scanners use this at some point or another - preventing that would the virus scanner into "I've been attacked" mode, quite possibly.
And if you think you can do this in a way that can't be quite easily circumvented, then you are deluding yourself. It wouldn't be terribly hard to put ANOTHER layer of hooks into the OpenProcess
system call that skips over your interference, and just calls the "real" OpenProcess
. So, assuming your program is REALLY interesting to open, someone will find a way to do that...