This disables the Windows File System Redirector.
When a 32 bit program runs on a 64 bit operating system the paths to C:/Windows/System32 automatically get redirected to the 32 bit version (C:/Windows/SysWow64), if you really do need to access the contents of System32, you need to disable the file system redirector first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import ctypes
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
#Example usage
import os
path = 'C:\\Windows\\System32\\msg.exe'
print os.path.exists(path)
with disable_file_system_redirection():
print os.path.exists(path)
print os.path.exists(path)
|
Hi zxw,
I would like to say thank you very much. Your post solve my problem (cannot call 64bit commands in subprocess).
Thanks again!