Beispiel #1
0
def OpenKey(space, w_key, w_sub_key, reserved=0, access=rwinreg.KEY_READ):
    """
key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.

key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that identifies the sub_key to open
res is a reserved integer, and must be zero.  Default is zero.
sam is an integer that specifies an access mask that describes the desired
 security access for the key.  Default is KEY_READ

The result is a new handle to the specified key
If the function fails, an EnvironmentError exception is raised."""
    hkey = hkey_w(w_key, space)
    utf8 = space.utf8_w(w_sub_key)
    state = space.fromcache(CodecState)
    errh = state.encode_error_handler
    subkeyW = utf8_encode_utf_16(utf8 + '\x00',
                                 'strict',
                                 errh,
                                 allow_surrogates=False)
    with rffi.scoped_nonmovingbuffer(subkeyW) as subkeyP0:
        subkeyP = rffi.cast(rffi.CWCHARP, rffi.ptradd(subkeyP0, 2))
        with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey:
            ret = rwinreg.RegOpenKeyExW(hkey, subkeyP, reserved, access,
                                        rethkey)
            if ret != 0:
                raiseWindowsError(space, ret, 'RegOpenKeyEx')
            return W_HKEY(space, rethkey[0])
Beispiel #2
0
def ConnectRegistry(space, w_machine, w_hkey):
    """
key = ConnectRegistry(computer_name, key)

Establishes a connection to a predefined registry handle on another computer.

computer_name is the name of the remote computer, of the form \\\\computername.
 If None, the local computer is used.
key is the predefined handle to connect to.

The return value is the handle of the opened key.
If the function fails, an EnvironmentError exception is raised."""
    hkey = hkey_w(w_hkey, space)
    if space.is_none(w_machine):
        with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey:
            ret = rwinreg.RegConnectRegistryW(None, hkey, rethkey)
            if ret != 0:
                raiseWindowsError(space, ret, 'RegConnectRegistry')
            return W_HKEY(space, rethkey[0])
    else:
        utf8 = space.utf8_w(w_machine)
        state = space.fromcache(CodecState)
        errh = state.encode_error_handler
        machineW = utf8_encode_utf_16(utf8 + '\x00',
                                      'strict',
                                      errh,
                                      allow_surrogates=False)
        with rffi.scoped_nonmovingbuffer(machineW) as machineP0:
            machineP = rffi.cast(rwin32.LPWSTR, rffi.ptradd(machineP0, 2))
            with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey:
                ret = rwinreg.RegConnectRegistryW(machineP, hkey, rethkey)
            if ret != 0:
                raiseWindowsError(space, ret, 'RegConnectRegistry')
            return W_HKEY(space, rethkey[0])
Beispiel #3
0
def SetValue(space, w_hkey, w_subkey, typ, w_value):
    """
SetValue(key, sub_key, type, value) - Associates a value with a specified key.

key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that names the subkey with which the value is associated.
type is an integer that specifies the type of the data.  Currently this
 must be REG_SZ, meaning only strings are supported.
value is a string that specifies the new value.

If the key specified by the sub_key parameter does not exist, the SetValue
function creates it.

Value lengths are limited by available memory. Long values (more than
2048 bytes) should be stored as files with the filenames stored in
the configuration registry.  This helps the registry perform efficiently.

The key identified by the key parameter must have been opened with
KEY_SET_VALUE access."""
    if typ != rwinreg.REG_SZ:
        raise oefmt(space.w_ValueError, "Type must be winreg.REG_SZ")
    hkey = hkey_w(w_hkey, space)
    state = space.fromcache(CodecState)
    errh = state.encode_error_handler
    utf8 = space.utf8_w(w_subkey)
    subkeyW = utf8_encode_utf_16(utf8 + '\x00',
                                 'strict',
                                 errh,
                                 allow_surrogates=False)
    utf8 = space.utf8_w(w_value)
    valueW = utf8_encode_utf_16(utf8 + '\x00',
                                'strict',
                                errh,
                                allow_surrogates=False)
    valueL = space.len_w(w_value)

    # Add an offset to remove the BOM from the native utf16 wstr
    with rffi.scoped_nonmovingbuffer(subkeyW) as subkeyP0:
        subkeyP = rffi.cast(rffi.CWCHARP, rffi.ptradd(subkeyP0, 2))
        with rffi.scoped_nonmovingbuffer(valueW) as valueP0:
            valueP = rffi.cast(rffi.CWCHARP, rffi.ptradd(valueP0, 2))
            ret = rwinreg.RegSetValueW(hkey, subkeyP, rwinreg.REG_SZ, valueP,
                                       valueL)
            if ret != 0:
                raiseWindowsError(space, ret, 'RegSetValue')
Beispiel #4
0
    def _unwrap_object(self, space, w_value):
        utf8, length = space.utf8_len_w(space.unicode_from_object(w_value))
        if length != 1:
            raise oefmt(space.w_ValueError,
                        "char16_t expected, got string of size %d", length)

        utf16 = utf8_encode_utf_16(utf8, 'strict')
        rawstr = rffi.str2charp(utf16)
        value = rffi.cast(self.c_ptrtype,
                          lltype.direct_ptradd(rawstr, 2))[0]  # adjust BOM
        lltype.free(rawstr, flavor='raw')
        return value
Beispiel #5
0
    def write_w(self, space, w_data):
        if self.handle == rwin32.INVALID_HANDLE_VALUE:
            raise err_closed(space)

        if not self.writable:
            raise err_mode(space, "writing")

        utf8 = space.utf8_w(w_data)
        if not len(utf8):
            return space.newint(0)

        # TODO: break up the encoding into chunks to save memory
        state = space.fromcache(CodecState)
        errh = state.encode_error_handler
        utf16 = utf8_encode_utf_16(utf8,
                                   'strict',
                                   errh,
                                   allow_surrogates=False)
        wlen = len(utf16) // 2

        with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as n:
            with rffi.scoped_nonmovingbuffer(utf16) as dataptr:
                # skip BOM, start at 1
                offset = 1
                while offset < wlen:
                    res = rwin32.WriteConsoleW(
                        self.handle,
                        rffi.cast(rwin32.LPVOID,
                                  rffi.ptradd(dataptr, offset * 2)),
                        wlen - offset, n, rffi.cast(rwin32.LPVOID, 0))
                    if not res:
                        err = rwin32.GetLastError_saved()
                        raise OperationError(space.w_WindowsError,
                                             space.newint(err))
                    nwrote = intmask(n[0])
                    offset += nwrote
                return space.newint(offset - 1)