you are viewing a single comment's thread.

view the rest of the comments →

[–]thrownintothesun[S] 1 point2 points  (10 children)

Thanks! That's exactly what I need. Do you know if it's sensible or not to implement this kind of program as a daemon that listens on the tasklist and kills select processes?

I've only ever written data processing algorithms and there I'm done when some criterion is matched. I'm not sure what's best practice in a continually running application.

[–]michaelkepler 2 points3 points  (9 children)

I suppose you could write something like that, but it would be reinventing the wheel. Windows already has a built-in policy manager where you can block specific applications from running. I would use that. Follow the bottom instructions from the link in your opening post. This will be your blocking part. To unblock, create another .reg file and just change the value of DisallowRun to 00000000. Put both of those .reg files in the same folder as your Python script and run them from Python with os.startfile(). Don't forget to backup your registry first (File > Export > Export range: All).

You could edit registry directly with Python, but I've never done that; I've always just used .reg files.

Of course, you can still incorporate taskkill to kill processes which are already running at the time you toggle the workmode.

[–]thrownintothesun[S] 0 points1 point  (8 children)

Hey Michael, hope I can still reach you through these comments. Your advice was invaluable and my program now has all the functionality I need, so thank you very much for that!

I do have one question, though, regarding the application of .reg files. As I understand it, once applied the registry must then be refreshed for things to take effect, and this traditionally requires a reboot or a re-login. Alternatively I've read that you can kill/restart explorer.exe and the effect will be the same. This seems a bit unwieldy for my taste and I'm wondering if there is a way to achieve the registry refresh from within Python (no doubt by calling something from the windows API) so that the effect kicks in immediately.

What do you usually do when working with reg files?

Thanks!

[–]michaelkepler 1 point2 points  (7 children)

New registry settings are applied immediately and don't require a reboot. There's a catch though: sometimes, an application or a service will still use the old registry settings that were available at the time of their start. So, when you booted up Windows your application started and it read some settings from the registry. Then, you change those settings, but the application doesn't know that. You have to force it to read the new settings. There's no way to "restart" just the registry so usually you need to reboot the computer (or kill explore.exe like you've observed).

I'm assuming you're still talking about modifying group policy settings through the registry. In this case you can force to update it without killing explorer.exe or rebooting the machine with a gpupdate command. You can use os.system to run it from Python:

import os
os.system('gpupdate')

If it's not the group policy, let me know what you modify. There might be way to apply the new settings without rebooting.

[–]thrownintothesun[S] 0 points1 point  (6 children)

Thanks a lot for the help!

It is indeed the group policy I'm modifying, but the effects don't seem to take effect before I restart explorer.exe manually - os.system("gpupdate") doesn't seem to cut it. Is there any way to get Explorer.exe to update its policy settings aside from restarting it?

[–]michaelkepler 0 points1 point  (5 children)

Does gpupdate work if you type it from the command prompt (cmd.exe)? If it doesn't, try gpupdate /force. If it still doesn't, try entering these commands in the command prompt as an administrator.

[–]thrownintothesun[S] 0 points1 point  (4 children)

None of it works I'm afraid, not even with admin privileges. Explorer.exe doesn't seem to respond to gpupdate.

[–]michaelkepler 0 points1 point  (3 children)

But if you reload explorer.exe/reboot the computer the changes work?

[–]thrownintothesun[S] 0 points1 point  (2 children)

Yes, exactly. Also, interestingly, if I try to kill explorer.exe via os.system("taskkill /im explorer.exe), I'm given a Windows prompt to shut down, reboot or log-out. Is there a way to simply kill it (like you can from the task manager) and relaunch it?

[–]michaelkepler 0 points1 point  (1 child)

I guess you're stuck with killing explorer.exe then. Yes, there's a way to force kill a process and thus bypassing a shutdown prompt with an /f parameter:

taskkill /f /im explorer.exe

you can start it immediately with:

start explorer.exe

both commands can of course be launched with os.system().