| 1 |
from __future__ import with_statement |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.rtyper.tool import rffi_platform as platform |
| 4 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 5 |
from rpython.rlib.rarithmetic import intmask |
| 6 |
from rpython.rlib import rwin32 |
| 7 |
|
| 8 |
eci = ExternalCompilationInfo( |
| 9 |
includes = ['windows.h', |
| 10 |
], |
| 11 |
libraries = ('Advapi32', 'kernel32') |
| 12 |
) |
| 13 |
class CConfig: |
| 14 |
_compilation_info_ = eci |
| 15 |
|
| 16 |
|
| 17 |
constant_names = ''' |
| 18 |
KEY_QUERY_VALUE KEY_SET_VALUE KEY_CREATE_SUB_KEY KEY_ENUMERATE_SUB_KEYS |
| 19 |
KEY_NOTIFY KEY_CREATE_LINK KEY_READ KEY_WRITE KEY_EXECUTE KEY_ALL_ACCESS |
| 20 |
KEY_WOW64_64KEY KEY_WOW64_32KEY REG_OPTION_RESERVED REG_OPTION_NON_VOLATILE |
| 21 |
REG_OPTION_VOLATILE REG_OPTION_CREATE_LINK REG_OPTION_BACKUP_RESTORE |
| 22 |
REG_OPTION_OPEN_LINK REG_LEGAL_OPTION REG_CREATED_NEW_KEY |
| 23 |
REG_OPENED_EXISTING_KEY REG_WHOLE_HIVE_VOLATILE REG_REFRESH_HIVE |
| 24 |
REG_NO_LAZY_FLUSH REG_NOTIFY_CHANGE_NAME REG_NOTIFY_CHANGE_ATTRIBUTES |
| 25 |
REG_NOTIFY_CHANGE_LAST_SET REG_NOTIFY_CHANGE_SECURITY REG_LEGAL_CHANGE_FILTER |
| 26 |
REG_NONE REG_SZ REG_EXPAND_SZ REG_BINARY REG_DWORD REG_DWORD_LITTLE_ENDIAN |
| 27 |
REG_DWORD_BIG_ENDIAN REG_LINK REG_MULTI_SZ REG_RESOURCE_LIST |
| 28 |
REG_FULL_RESOURCE_DESCRIPTOR REG_RESOURCE_REQUIREMENTS_LIST |
| 29 |
REG_QWORD REG_QWORD_LITTLE_ENDIAN |
| 30 |
|
| 31 |
HKEY_LOCAL_MACHINE HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER |
| 32 |
HKEY_DYN_DATA HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATA HKEY_USERS |
| 33 |
|
| 34 |
ERROR_MORE_DATA |
| 35 |
'''.split() |
| 36 |
for name in constant_names: |
| 37 |
setattr(CConfig, name, platform.DefinedConstantInteger(name)) |
| 38 |
|
| 39 |
constants = {} |
| 40 |
cConfig = platform.configure(CConfig) |
| 41 |
constants.update(cConfig) |
| 42 |
globals().update(cConfig) |
| 43 |
|
| 44 |
def external(name, args, result, **kwds): |
| 45 |
return rffi.llexternal(name, args, result, compilation_info=eci, |
| 46 |
calling_conv='win', **kwds) |
| 47 |
|
| 48 |
HKEY = rwin32.HANDLE |
| 49 |
PHKEY = rffi.CArrayPtr(HKEY) |
| 50 |
REGSAM = rwin32.DWORD |
| 51 |
|
| 52 |
def get_traits(suffix): |
| 53 |
if suffix == 'A': |
| 54 |
strp = rffi.CCHARP |
| 55 |
else: |
| 56 |
strp = rffi.CWCHARP |
| 57 |
RegSetValue = external( |
| 58 |
'RegSetValue' + suffix, |
| 59 |
[HKEY, strp, rwin32.DWORD, strp, rwin32.DWORD], |
| 60 |
rffi.LONG) |
| 61 |
|
| 62 |
RegSetValueEx = external( |
| 63 |
'RegSetValueEx' + suffix, |
| 64 |
[HKEY, strp, rwin32.DWORD, |
| 65 |
rwin32.DWORD, strp, rwin32.DWORD], |
| 66 |
rffi.LONG) |
| 67 |
|
| 68 |
RegQueryValue = external( |
| 69 |
'RegQueryValue' + suffix, |
| 70 |
[HKEY, strp, strp, rwin32.PLONG], |
| 71 |
rffi.LONG) |
| 72 |
|
| 73 |
RegQueryValueEx = external( |
| 74 |
'RegQueryValueEx' + suffix, |
| 75 |
[HKEY, strp, rwin32.LPDWORD, rwin32.LPDWORD, |
| 76 |
strp, rwin32.LPDWORD], |
| 77 |
rffi.LONG) |
| 78 |
|
| 79 |
RegCreateKey = external( |
| 80 |
'RegCreateKey' + suffix, |
| 81 |
[HKEY, strp, PHKEY], |
| 82 |
rffi.LONG) |
| 83 |
|
| 84 |
RegCreateKeyEx = external( |
| 85 |
'RegCreateKeyEx' + suffix, |
| 86 |
[HKEY, strp, rwin32.DWORD, strp, rwin32.DWORD, |
| 87 |
REGSAM, rffi.VOIDP, PHKEY, rwin32.LPDWORD], |
| 88 |
rffi.LONG) |
| 89 |
|
| 90 |
RegDeleteValue = external( |
| 91 |
'RegDeleteValue' + suffix, |
| 92 |
[HKEY, strp], |
| 93 |
rffi.LONG) |
| 94 |
|
| 95 |
RegDeleteKey = external( |
| 96 |
'RegDeleteKey' + suffix, |
| 97 |
[HKEY, strp], |
| 98 |
rffi.LONG) |
| 99 |
|
| 100 |
RegOpenKeyEx = external( |
| 101 |
'RegOpenKeyEx' + suffix, |
| 102 |
[HKEY, strp, rwin32.DWORD, REGSAM, PHKEY], |
| 103 |
rffi.LONG) |
| 104 |
|
| 105 |
RegEnumValue = external( |
| 106 |
'RegEnumValue' + suffix, |
| 107 |
[HKEY, rwin32.DWORD, strp, |
| 108 |
rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD, |
| 109 |
rffi.CCHARP, rwin32.LPDWORD], |
| 110 |
rffi.LONG) |
| 111 |
|
| 112 |
RegEnumKeyEx = external( |
| 113 |
'RegEnumKeyEx' + suffix, |
| 114 |
[HKEY, rwin32.DWORD, strp, |
| 115 |
rwin32.LPDWORD, rwin32.LPDWORD, |
| 116 |
strp, rwin32.LPDWORD, rwin32.PFILETIME], |
| 117 |
rffi.LONG) |
| 118 |
|
| 119 |
RegQueryInfoKey = external( |
| 120 |
'RegQueryInfoKey' + suffix, |
| 121 |
[HKEY, strp, rwin32.LPDWORD, rwin32.LPDWORD, |
| 122 |
rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD, |
| 123 |
rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD, |
| 124 |
rwin32.LPDWORD, rwin32.PFILETIME], |
| 125 |
rffi.LONG) |
| 126 |
|
| 127 |
RegLoadKey = external( |
| 128 |
'RegLoadKey' + suffix, |
| 129 |
[HKEY, strp, strp], |
| 130 |
rffi.LONG) |
| 131 |
|
| 132 |
RegSaveKey = external( |
| 133 |
'RegSaveKey' + suffix, |
| 134 |
[HKEY, strp, rffi.VOIDP], |
| 135 |
rffi.LONG) |
| 136 |
|
| 137 |
RegConnectRegistry = external( |
| 138 |
'RegConnectRegistry' + suffix, |
| 139 |
[strp, HKEY, PHKEY], |
| 140 |
rffi.LONG) |
| 141 |
|
| 142 |
return (RegSetValue, RegSetValueEx, RegQueryValue, RegQueryValueEx, |
| 143 |
RegCreateKey, RegCreateKeyEx, RegDeleteValue, RegDeleteKey, |
| 144 |
RegOpenKeyEx, RegEnumValue, RegEnumKeyEx, RegQueryInfoKey, |
| 145 |
RegLoadKey, RegSaveKey, RegConnectRegistry) |
| 146 |
|
| 147 |
RegSetValueW, RegSetValueExW, RegQueryValueW, RegQueryValueExW, \ |
| 148 |
RegCreateKeyW, RegCreateKeyExW, RegDeleteValueW, RegDeleteKeyW, \ |
| 149 |
RegOpenKeyExW, RegEnumValueW, RegEnumKeyExW, RegQueryInfoKeyW, \ |
| 150 |
RegLoadKeyW, RegSaveKeyW, RegConnectRegistryW = get_traits('W') |
| 151 |
|
| 152 |
RegSetValueA, RegSetValueExA, RegQueryValueA, RegQueryValueExA, \ |
| 153 |
RegCreateKeyA, RegCreateKeyExA, RegDeleteValueA, RegDeleteKeyA, \ |
| 154 |
RegOpenKeyExA, RegEnumValueA, RegEnumKeyExA, RegQueryInfoKeyA, \ |
| 155 |
RegLoadKeyA, RegSaveKeyA, RegConnectRegistryA = get_traits('A') |
| 156 |
|
| 157 |
RegCloseKey = external( |
| 158 |
'RegCloseKey', |
| 159 |
[HKEY], |
| 160 |
rffi.LONG) |
| 161 |
|
| 162 |
RegFlushKey = external( |
| 163 |
'RegFlushKey', |
| 164 |
[HKEY], |
| 165 |
rffi.LONG) |
| 166 |
|
| 167 |
_ExpandEnvironmentStringsW = external( |
| 168 |
'ExpandEnvironmentStringsW', |
| 169 |
[rffi.CWCHARP, rffi.CWCHARP, rwin32.DWORD], |
| 170 |
rwin32.DWORD, |
| 171 |
save_err=rffi.RFFI_SAVE_LASTERROR) |
| 172 |
|
| 173 |
def ExpandEnvironmentStrings(source, unicode_len): |
| 174 |
with rffi.scoped_utf82wcharp(source, unicode_len) as src_buf: |
| 175 |
size = _ExpandEnvironmentStringsW(src_buf, |
| 176 |
lltype.nullptr(rffi.CWCHARP.TO), 0) |
| 177 |
if size == 0: |
| 178 |
raise rwin32.lastSavedWindowsError("ExpandEnvironmentStrings") |
| 179 |
size = intmask(size) |
| 180 |
with rffi.scoped_alloc_unicodebuffer(size) as dest_buf: |
| 181 |
if _ExpandEnvironmentStringsW(src_buf, |
| 182 |
dest_buf.raw, size) == 0: |
| 183 |
raise rwin32.lastSavedWindowsError("ExpandEnvironmentStrings") |
| 184 |
res = dest_buf.str(size-1) |
| 185 |
return res.encode('utf8'), len(res) |