RegCreateKeyTransacted.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import win32api, win32con, win32transaction
  2. keyname='Pywin32 test transacted registry functions'
  3. subkeyname='test transacted subkey'
  4. classname='Transacted Class'
  5. trans=win32transaction.CreateTransaction(Description='test RegCreateKeyTransacted')
  6. key, disp=win32api.RegCreateKeyEx(win32con.HKEY_CURRENT_USER, keyname,
  7. samDesired=win32con.KEY_ALL_ACCESS, Class=classname)
  8. ## clean up any existing keys
  9. for subk in win32api.RegEnumKeyExW(key):
  10. win32api.RegDeleteKey(key, subk[0])
  11. ## reopen key in transacted mode
  12. transacted_key=win32api.RegOpenKeyTransacted(Key=win32con.HKEY_CURRENT_USER, SubKey=keyname,
  13. Transaction=trans, samDesired=win32con.KEY_ALL_ACCESS)
  14. subkey, disp=win32api.RegCreateKeyEx(transacted_key, subkeyname, Transaction=trans,
  15. samDesired=win32con.KEY_ALL_ACCESS, Class=classname)
  16. ## Newly created key should not be visible from non-transacted handle
  17. subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)]
  18. assert subkeyname not in subkeys
  19. transacted_subkeys=[s[0] for s in win32api.RegEnumKeyExW(transacted_key)]
  20. assert subkeyname in transacted_subkeys
  21. ## Key should be visible to non-transacted handle after commit
  22. win32transaction.CommitTransaction(trans)
  23. subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)]
  24. assert subkeyname in subkeys
  25. ## test transacted delete
  26. del_trans=win32transaction.CreateTransaction(Description='test RegDeleteKeyTransacted')
  27. win32api.RegDeleteKeyEx(key, subkeyname, Transaction=del_trans)
  28. ## subkey should still show up for non-transacted handle
  29. subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)]
  30. assert subkeyname in subkeys
  31. ## ... and should be gone after commit
  32. win32transaction.CommitTransaction(del_trans)
  33. subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)]
  34. assert subkeyname not in subkeys
  35. win32api.RegDeleteKey(win32con.HKEY_CURRENT_USER, keyname)