import os
import ctypes
add_or_delete='a'#a to add,d to delete 
dll_file_or_path_dll="test.dll"
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)
with disable_file_system_redirection():
    if add_or_delete=='a':
        print os.popen('REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v APPInit_DLLs /t REG_SZ /d %s /f'%dll_file_or_path_dll).read()
        print os.popen('REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v LoadAppInit_DLLs /t REG_DWORD /d 1 /f').read()
        print os.popen('REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v RequiredSignedAppInit_DLLs /t REG_DWORD /d 0 /f').read()
    else:
        print os.popen('REG DELETE "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v APPInit_DLLs /f').read()
        print os.popen('REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v LoadAppInit_DLLs /t REG_DWORD /d 0 /f').read()
        print os.popen('REG DELETE "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" /v RequiredSignedAppInit_DLLs /f').read()