Python win32api registry key change - python

I am trying to trigger an event every time a registry value is being modified.
import win32api
import win32event
import win32con
import _winreg
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Control Panel\Desktop',0,_winreg.KEY_READ)
sub_key = _winreg.CreateKey(key,'Wallpaper')
evt = win32event.CreateEvent(None,0,0,None)
win32api.RegNotifyChangeKeyValue(sub_key,1,win32api.REG_NOTIFY_CHANGE_ATTRIBUTES,evt,True)
ret_code=win32event.WaitForSingleObject(evt,3000)
if ret_code == win32con.WAIT_OBJECT_0:
print "CHANGED"
if ret_code == win32con.WAIT_TIMEOUT:
print "TIMED"
my problem is that this is never triggered , the event always time-out.
(the reg key I am trying to follow is the wallpaper)
[
please note I trigger the event by 1) manually changing the registry value in regedit 2) an automated script which run this :
from ctypes import windll
from win32con import *
windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,"C:\wall.jpg",SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE)
]
Thanks for any help in advance :)
EDIT:: sorry about formatting

"WallPaper" is a value not a key/subkey. So if you bring up regedit.exe, you'll notice that you've created a new key "HKCU\Control Panel\Desktop\WallPaper" which is distinct from the "WallPaper" value under the "HKCU\Control Panel\Desktop" key.
Here's one way to modify your code to listen for changes:
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Control Panel\Desktop', 0, _winreg.KEY_READ)
evt = win32event.CreateEvent(None, 0, 0, None)
win32api.RegNotifyChangeKeyValue(key, 1, win32api.REG_NOTIFY_CHANGE_LAST_SET, evt, True)
Note that we don't use a WallPaper subkey any more and note that the "notify fitler" has been changed to NOTIFY_CHANGE_LAST_SET; from the docs this will:
Notify the caller of changes to a value of the key. This can include adding or deleting a value, or changing an existing value.
The rest of your code will work, but you'll need to use the QueryValueEx function before and after to ensure it was the WallPaper value that changed and not some other. (I don't know of a way to listen for specific values.)

Related

How to set windows environment variable from python

I am writing a python program that needs to store a value in a persistent user environment variable. I believe these values are stored in the registry. I have tried something like this using the winreg python module.
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', access=winreg.KEY_WRITE)
winreg.SetValueEx(key, envVarName, 0, winreg.REG_SZ, envVarValue)
winreg.Close(key)
I can see from using Registry Editor that this value is successfully set. But if I try and read the value from another python script or from a new powershell instance, I don't see the new value until the machine is rebooted.
What else do I need to do to make this value available to other processes without rebooting?
Looking at this answer gives a possible answer:
https://stackoverflow.com/a/61757725/18300067
os.system("SETX {0} {1} /M".format("start", "test"))
This is how we do it in production on one of Windows servers (code is simplified):
import winreg
regdir = "Environment-test"
keyname = "Name-test"
keyvalue = "Value-test"
def setRegistry(regdir, keyname, keyvalue):
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, regdir) as _:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, regdir, 0, winreg.KEY_WRITE) as writeRegistryDir:
winreg.SetValueEx(writeRegistryDir, keyname, 0, winreg.REG_SZ, keyvalue)
def getRegistry(regdir, keyname):
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, regdir) as accessRegistryDir:
value, _ = winreg.QueryValueEx(accessRegistryDir, keyname)
return(value)
If I firstly set the value using:
setRegistry(regdir, keyname, keyvalue)
Then check if it's created:
Now I use the getRegistry and open a PS in admin mode to run it:
print(getRegistry(regdir, keyname))

How does one add an item to GTK's "recently used" file list from Python?

I'm trying to add to the "recently used" files list from Python 3 on Ubuntu.
I am able to successfully read the recently used file list like this:
from gi.repository import Gtk
recent_mgr = Gtk.RecentManager.get_default()
for item in recent_mgr.get_items():
print(item.get_uri())
This prints out the same list of files I see when I look at "Recent" in Nautilus, or look at the "Recently Used" place in the file dialog of apps like GIMP.
However, when I tried adding an item like this (where /home/laurence/foo/bar.txt is an existing text file)...
recent_mgr.add_item('file:///home/laurence/foo/bar.txt')
...the file does not show up in the Recent section of Nautilus or in file dialogs. It doesn't even show up in the results returned by get_items().
How can I add a file to GTK's recently used file list from Python?
A Gtk.RecentManager needs to emit the changed signal for the update to be written in a private attribute of the C++ class. To use a RecentManager object in an application, you need to start the event loop by calling Gtk.main:
from gi.repository import Gtk
recent_mgr = Gtk.RecentManager.get_default()
uri = r'file:/path/to/my/file'
recent_mgr.add_item(uri)
Gtk.main()
If you don't call Gtk.main(), the changed signal is not emitted and nothing happens.
To answer #andlabs query, the reason why RecentManager.add_item returns a boolean is because the g_file_query_info_async function is called. The callback function gtk_recent_manager_add_item_query_info then gathers the mimetype, application name and command into a GtkRecentData struct and finally calls gtk_recent_manager_add_full. The source is here.
If anything goes wrong, it is well after add_item has finished, so the method just returns True if the object it is called from is a RecentManager and if the uri is not NULL; and False otherwise.
The documentation is inaccurate in saying:
Returns TRUE if the new item was successfully added to the recently used resources list
as returning TRUE only means that an asynchronous function was called to deal with the addition of a new item.
As suggested by Laurence Gonsalves, the following runs pseudo-synchronously:
from gi.repository import Gtk, GObject
recent_mgr = Gtk.RecentManager.get_default()
uri = r'file:/path/to/my/file'
recent_mgr.add_item(uri)
GObject.idle_add(Gtk.main_quit)
Gtk.main()
This is my solution (complet script) with timer for quit GTK.main() loop
#!/usr/bin/env python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
import sys
import os
import subprocess
recent_mgr = Gtk.RecentManager.get_default()
if len(sys.argv) <= 1:
paths = (os.getcwd(),)
else:
paths = sys.argv[1:]
for path in paths:
if os.path.exists(path):
if path[0] != "/":
path = os.getcwd() + "/" + path
subprocess.call(["touch", "-a", path])
uri = r"file:" + path
recent_mgr.add_item(uri)
GLib.timeout_add(22, Gtk.main_quit, None)
Gtk.main()

Opens same registry twice?

I am trying to get all installed programs of my windows computer, therefore I read out the registry.
But somehow python reads the 32bit programs out twice (even though I give him another registry entry)
Here is the code snipped:
def get_programs(registry):
reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
programList = []
key = OpenKey(reg, registry)
print(QueryInfoKey(key))
for i in range(0, QueryInfoKey(key)[0]):
programList.append(EnumKey(key, i))
CloseKey(key)
CloseKey(reg)
return programList
I call this function like this:
registry32bit = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
registry64bit = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
programs32bit = get_programs(registry32bit)
programs64bit = get_programs(registry64bit)
Why does python open and read out the same registry (for 32 bit) twice and return the exactly same list?
This appears to work and uses #eryksun suggestion in a comment below about just letting the redirection happen and not explicitly referencing the Wow6432Node registry key. The central idea is to just specify either the KEY_WOW64_32KEY or KEY_WOW64_64KEY flag when opening the uninstall subkey and let the magic happen.
Note: I also Pythonized the code in the get_programs() function some. This made it shorter and more readable in my opinion.
import sys
from _winreg import *
# Assure registry handle objects with context manager protocol implemented.
if sys.version_info.major*10 + sys.version_info.minor < 26:
raise AssertionError('At least Python 2.6 is required.')
def get_programs(subkey, regBitView):
with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as hive:
with OpenKey(hive, subkey, 0, regBitView | KEY_READ) as key:
return [EnumKey(key, i) for i in range(QueryInfoKey(key)[0])]
UNINSTALL_REG_KEY = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
programs32bit = get_programs(UNINSTALL_REG_KEY, KEY_WOW64_32KEY)
programs64bit = get_programs(UNINSTALL_REG_KEY, KEY_WOW64_64KEY)
print('32-bit programs:\n{}'.format(programs32bit))
print('')
print('64-bit programs:\n{}'.format(programs64bit))
Many thanks to #eryksun for the clues and many implementation strategy suggestions.

How to determine if win32api.ShellExecute was successful using hinstance?

I've been looking around for an answer to my original issue.. how do i determine (programmatically) that my win32api.ShellExecute statement executed successfully, and if a successful execution occurs, execute an os.remove() statement.
Researching I found out that the ShellExecute() call returns the HINSTANCE. Further digging I found that ShellExecute() will return an HINSTANCE > 32 if it was successful. My problem/question now is, how do i use it to control the rest of my program's flow? I tried using an if HINSTANCE> 32: statement to control the next part, but I get a NameError: name 'hinstance' is not defined message. Normally this wouldn't confuse me because it means i need to define the variable 'hinstance' before referencing it; however, because i thought ShellExecute is supposed to return HINSTANCE, i thought that makes it available for use?
Here is my full code where i am trying to implement this. Note that in my print_file() def i am assigning hinstance to the full win32api.ShellExecute() command in attempt to capture the hinstance along with explicitly returning it at the end of the function.. this isn't working either.
import win32print
import win32api
from os.path import isfile, join
import glob
import os
import time
source_path = "c:\\temp\\source\\"
def main():
printer_name = win32print.GetDefaultPrinter()
while True:
file_queue = [f for f in glob.glob("%s\\*.txt" % source_path) if isfile(f)]
if len(file_queue) > 0:
for i in file_queue:
print_file(i, printer_name)
if hinstance > 32:
time.sleep(.25)
delete_file(i)
print "Filename: %r has printed" % i
print
time.sleep(.25)
print
else:
print "No files to print. Will retry in 15 seconds"
time.sleep(15)
def print_file(pfile, printer):
hinstance = win32api.ShellExecute(
0,
"print",
'%s' % pfile,
'/d:"%s"' % printer,
".",
0
)
return hinstance
def delete_file(f):
os.remove(f)
print f, "was deleted!"
def alert(email):
pass
main()
With ShellExecute, you will never know when the printing is complete, it depends on the size of the file and whether the printer driver buffers the contents (the printer might be waiting for you to fill the paper tray, for example).
According to this SO answer, it looks like subprocess.call() is a better solution, since it waits for the command to complete, only in this case you would need to read the registry to obtain the exe associated with the file.
ShellExecuteEx is available from pywin32, you can do something like:
import win32com.shell.shell as shell
param = '/d:"%s"' % printer
shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC, lpVerb='print', lpFile=pfile, lpParameters=param)
EDIT: code for waiting on the handle from ShellExecuteEx()
import win32com.shell.shell as shell
import win32event
#fMask = SEE_MASK_NOASYNC(0x00000100) = 256 + SEE_MASK_NOCLOSEPROCESS(0x00000040) = 64
dict = shell.ShellExecuteEx(fMask = 256 + 64, lpFile='Notepad.exe', lpParameters='Notes.txt')
hh = dict['hProcess']
print hh
ret = win32event.WaitForSingleObject(hh, -1)
print ret
The return value of ShellExecute is what you need to test. You return that from print_file, but you then ignore it. You need to capture it and check that.
hinstance = print_file(i, printer_name)
if hinstance > 32:
....
However, having your print_file function leak implementation detail like an HINSTANCE seems bad. I think you would be better to check the return value of ShellExecute directly at the point of use. So try to move the > 32 check inside print_file.
Note that ShellExecute has very weak error reporting. If you want proper error reporting then you should use ShellExecuteEx instead.
Your delete/sleep loop is very brittle indeed. I'm not quite sure I can recommend anything better since I'm not sure what you are trying to achieve. However, expect to run into trouble with that part of your program.

Change wallpaper in Python for user while being system

what I am trying to do is change the desktop wallpaper in windows.
To do that, I use the following code:
import ctypes
import Image
pathToBmp = "PATH TO BMP FILE"
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToBmp , 0)
this works when I run the .py file, this works when I convert it using py2exe and run the exe under the current user, but when I run the exe as SYSTEM, the current user background does not change.
This ofcourse was to be expected. But I don't know how to solve it.
By the way, it does not matter if any of your solutions changes the current user background or all the users' backgrounds.
Thank you for your time.
How about creating a value key in the registry at:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
This will change the background when ever the user login.
To try it, write this script, name it for example SetDesktopBackground.py, any where you like:
#!python
from ctypes import *
from os import path
SPI_SETDESKWALLPAPER = 0x14
SPIF_UPDATEINIFILE = 0x1
lpszImage = path.join(path.dirname(path.realpath(__file__)), 'your_image.jpg')
SystemParametersInfo = windll.user32.SystemParametersInfoA
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, lpszImage, SPIF_UPDATEINIFILE)
Dont forgot to put some image, your_image.jpg, in the same directory. Then open the registery editor:
Start > Search > type regedit.exe
Then go to the path:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
Right click and choose New > String Value and type any name you like for this value.
Right click on this new value and choose Modify, in the Data Value field write:
"C:\Python26\pythonw.exe" "C:\Path\To\SetDesktopBackground.py"
To test it, logout and login again. The background should change when ever this user login.
That was the manual way to do it, you can use _winreg in your application to create the value during the installation:
#!python
from _winreg import *
from sys import executable
from os import path
subkey = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run'
script = 'C:\\Path\\To\\SetDesktopBackground.py'
pythonw = path.join(path.dirname(executable), 'pythonw.exe')
hKey = OpenKey(HKEY_CURRENT_USER, subkey, 0, KEY_SET_VALUE)
SetValueEx(hKey, 'MyApp', 0, REG_SZ, '"{0}" "{1}"'.format(pythonw, script))
CloseKey(hKey)

Categories

Resources