How to stop program start using C #?

Here is an idea, I would like to do a favor? which will search for a specific program, launch and dissolve it if certain conditions are not met.

Say I have a game in which I want to stop playing for a week. Therefore, if I start it on any day except Friday / Saturday / Sunday, it will intercept and cancel. Is this possible with C #?

The main thing I'm looking for is how to catch the launch of the program, rest should be easy.

+4
source share
8 answers

Well, you can definitely determine which programs are running by looking for the process names you need (GetProcessesByName ()) and killing them.

Process[] processes = Process.GetProcessesByName(processName); foreach(Process process in processes) { process.Kill(); } 

You can only have a list of them that you do not want to run, perform a time check (or any criteria that must be met), and then go through the list. I did something like this once for the test, and it works quite well.

+6
source

I do not know about C #, in particular, here, but why you can do it (I will do it in a dangerous way) using the Image File Execution execution options ( http://blogs.msdn.com/junfeng/archive/2004/04 /28/121871.aspx ) in the registry. For any executable that you are interested in intercepting, you can set the Debugger parameter for it and then create a small application that will be used in the debugger and then essentially filter these calls. If you want to allow it to run, start the process, otherwise do whatever you want. I have never tried this, but it seems that he can do what you want.

Or, if you want to react to starting a process and then shutting it down, you can use ProcessWatcher ( http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx ) and subscribe to the process generated by the event and then close it if necessary. However, this is more of a reactive approach, and a proactive approach, like the first.

+3
source

I'm not sure that you can run it, but you can try to find the program in the list of windows (was it ENUM_WINDOWS? I will never remember) and turn it off as soon as it appears.

Perhaps you can even do it in AutoIt!

Pull out Petzold and have fun with the windows ...

EDIT: Check the sysinternals for the source for a list of active processes - do it in a loop and you can catch your program when it starts. I do not think that there is still a source on the official website, but it should be somewhere there ...

0
source

Hrm, instead of intercepting its launch, your service may somehow sabotage the executable file (i.e. with permissions, rename the EXE, replace the EXE with something that scolds you for your weak will, etc. .).

If this is not good enough, you can try one of the following methods:

http://www.codeproject.com/KB/system/win32processusingwmi.aspx

http://msdn.microsoft.com/en-us/magazine/cc188966.aspx

0
source

There are always hooks. Although there is no support in the .net library, you can always wrap a win32 dll. Here is a good article: http://msdn.microsoft.com/sv-se/magazine/cc188966(en-us).aspx

This is low-level programming, and if you want your code to be portable, I would not use hooks.

0
source

You do not have to intercept running programs. You can write an application called โ€œLaunch program managerโ€ that launches programs for you (if they are listed in the white list). After you write the application, all you have to do is change the application shortcuts to indicate the launch manager with the appropriate parameters.

0
source

If you just need to disable the application, you can edit the registry to try to connect the debugger to this application automatically, if the debugger does not exist, windows will complain and help out.

[HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Image File Settings] is the key, see the MS article http://support.microsoft.com/default.aspx?kbid=238788 for more information.

0
source

Source: https://habr.com/ru/post/1277276/


All Articles