#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Portland, Oregon
    Posts
    7
    Rep Power
    0

    Accessing Windows 7 Registry VirtualStore


    Hello,

    I've searched the python sites and help, library's and all the forums I could find, but haven't seen any mention of this. This forum seemed like a good place to ask this.

    I am on an HP laptop Intel Core 2 Duo, running Windows 7 Pro SP1 32Bit. I am using Python 2.7.3.

    I have an application I built that ran fine on Windows XP, but now fails on Windows 7. The place I'm encountering the problem is where I try to read a key from the registry. I believe it's because of the Virtualization of the registry on Windows 7. This key is created by another app that I'm trying to co-ordinate with. On Windows XP the Registry key was:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Interface Software\ConnMgr]
    "DB Path"="C:\\Documents and Settings\\All Users\\Application Data\\<path to a data file>"

    When this app is installed on Windows 7, the key is directed to the registry Virtual Store at:
    [HKEY_CURRENT_USER\Software\Classes\VirtualStore\MACHINE\SOFTWARE\Interface Software\ConnMgr]
    "DB Path"="C:\\ProgramData\\EnvisionWare\\<path to a data file>"

    So far that is what I think I'd expected on Windows 7 and the virtualization of the registry.

    The code fragment that is reading the registry is:
    -----
    from _winreg import *


    ConnKey = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Interface Software\ConnMgr', 0, KEY_READ)
    ConnValue = QueryValueEx(ConnKey, "DB Path")
    EWDataSource = os.path.split(str(ConnValue[0]))
    ------
    The OpenKey fails with the message: WindowsError: (2, 'The system cannot find the file specified"). I believe this is because the key does not exist at the path [HKEY_LOCAL_MACHINE\SOFTWARE\Interface Software\ConnMgr].

    After all this, the question is: Why isn't the OpenKey call being redirected to the VirtualStore? What can I change in the program, ACLs or other to make it be redirected?

    Any help would be appreciated.
    Thanks,
    John
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Portland, Oregon
    Posts
    7
    Rep Power
    0
    Can anyone point me in the direction where I might find information on how Python deals with Windows 7 and the registry VirtualStore?

    Everything I find so far about windows is the program must be running with elevated privileges. But the program isn't triggering a UAC prompt. It's also possible the _winreg library is doing something wonky I don't expect, but I can't tell that.

    Also, is there information on how I can control the permissions when running the program in IDLE and when I create an executable with py2exe?

    Thanks for your help,
    John
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Portland, Oregon
    Posts
    7
    Rep Power
    0
    Hello,

    OK, this is getting serious... According to what I understand about Win 7 registry and VirtualStore, this should not be happening. (Or I don't understand why it's happening.)

    After some more testing I have the following routine:

    Code:
    def initEnv(pConfig):
        """ Initialize the working environment as needed.
        """
        logger.info('-> initEnv Enter')
        rtnStatus = 0
    
        try:
            newPath = pConfig.get('ConvertParms', 'currdir')+ '\\tmp'
            logger.debug('-- New tmp path: %s',newPath)
            if not os.path.exists(newPath):
                os.mkdir(newPath)
    
            try:
                ConnKey = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Interface Software\ConnMgr', 0, KEY_READ)
    #            ConnKey = OpenKey(HKEY_CURRENT_USER, r'Software\Classes\VirtualStore\MACHINE\SOFTWARE\Interface Software\ConnMgr', 0, KEY_READ)
            except OSError as err:
                logger.error('Failed to retrieve DB Path. Err: %s',err)
                rtnStatus = -1
            if not rtnStatus:
                ConnValue = QueryValueEx(ConnKey, "DB Path")
                EWDataSource = os.path.split(str(ConnValue[0]))
                logger.debug('-- EWDataSource: %s',EWDataSource)
                pConfig.set('ConvertParms', 'infout',str(ConnValue[0]))
    
                CloseKey(ConnKey)
    
        except OSError as err:
            logger.error('Failed to Initialize Environment. Error: %s', err)
            return 1
        except Exception as err:
            logger.error('Unexpected Exception found. Error: %s',err)
        finally:
            logger.debug('-- InitEnv: ConvertParms: %s', pConfig.items('ConvertParms'))
    
        return rtnStatus
    Note I have two statements with OpenKey. One that accesses the HKLM\SOFTWARE branch and one that accesses the VirtualStore path explicitly. I also manually created the key and value in the HKLM hive so the program would not get the error: [Error 2] The system cannot find the file specified

    I have a standard user that is running this module so in theory this user should NOT have elevated permissions to access HKLM and when I try to access the HKLM I should be transparently getting the VS version. Here's what I find.

    1. With mode KEY_READ, I can retrieve the HKLM key. This is expected.
    2. With mode KEY_ALL_ACCESS, I get the error: [Error 5] Access is denied. What I don't get is the value in the VS as I expect.
    3. When I run the OpenKey('HKEY_CURRENT_USER... I can access the key with KEY_READ or KEY_ALL_ACCESS.

    So, what am I overlooking, or is _winreg accessing the registry in a way that is unvirtualizing it? How do I make Python access it virtualized? As a windows app, I should not be needing to access the VS explictly. That should be completely hidden from a windows app with un-elevated privileges. Also I am not getting the UAC prompt so Windows is not complaining.

    Any help will be greatly appreciated by many I'm sure.

    Thanks,
    John

IMN logo majestic logo threadwatch logo seochat tools logo