diff options
Diffstat (limited to 'PC/winreg.c')
-rw-r--r-- | PC/winreg.c | 86 |
1 files changed, 49 insertions, 37 deletions
diff --git a/PC/winreg.c b/PC/winreg.c index 240ca69..3a5c8a6 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -760,26 +760,27 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) case REG_SZ: case REG_EXPAND_SZ: { - if (value == Py_None) - *retDataSize = 1; - else { - if (!PyUnicode_Check(value)) - return FALSE; - *retDataSize = Py_SAFE_DOWNCAST( - 2 + PyUnicode_GET_DATA_SIZE(value), - size_t, DWORD); - } - *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize); - if (*retDataBuf==NULL){ - PyErr_NoMemory(); - return FALSE; - } - if (value == Py_None) - wcscpy((wchar_t *)*retDataBuf, L""); - else - wcscpy((wchar_t *)*retDataBuf, - PyUnicode_AS_UNICODE(value)); - break; + if (value != Py_None) { + Py_ssize_t len; + if (!PyUnicode_Check(value)) + return FALSE; + *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len); + if (*retDataBuf == NULL) + return FALSE; + *retDataSize = Py_SAFE_DOWNCAST( + (len + 1) * sizeof(wchar_t), + Py_ssize_t, DWORD); + } + else { + *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1); + if (*retDataBuf == NULL) { + PyErr_NoMemory(); + return FALSE; + } + ((wchar_t *)*retDataBuf)[0] = L'\0'; + *retDataSize = 1 * sizeof(wchar_t); + } + break; } case REG_MULTI_SZ: { @@ -796,10 +797,16 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) for (j = 0; j < i; j++) { PyObject *t; + wchar_t *wstr; + Py_ssize_t len; + t = PyList_GET_ITEM(value, j); if (!PyUnicode_Check(t)) return FALSE; - size += Py_SAFE_DOWNCAST(2 + PyUnicode_GET_DATA_SIZE(t), + wstr = PyUnicode_AsUnicodeAndSize(t, &len); + if (wstr == NULL) + return FALSE; + size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t), size_t, DWORD); } @@ -815,10 +822,15 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) for (j = 0; j < i; j++) { PyObject *t; + wchar_t *wstr; + Py_ssize_t len; + t = PyList_GET_ITEM(value, j); - wcscpy(P, PyUnicode_AS_UNICODE(t)); - P += 1 + wcslen( - PyUnicode_AS_UNICODE(t)); + wstr = PyUnicode_AsUnicodeAndSize(t, &len); + if (wstr == NULL) + return FALSE; + wcscpy(P, wstr); + P += (len + 1); } /* And doubly-terminate the list... */ *P = '\0'; @@ -882,7 +894,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) retDataSize -= 2; if (retDataSize <= 0) data = L""; - obData = PyUnicode_FromUnicode(data, retDataSize/2); + obData = PyUnicode_FromWideChar(data, retDataSize/2); break; } case REG_MULTI_SZ: @@ -913,7 +925,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) } PyList_SetItem(obData, index, - PyUnicode_FromUnicode(str[index], len)); + PyUnicode_FromWideChar(str[index], len)); } free(str); @@ -1054,7 +1066,7 @@ PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) /* Only available on 64bit platforms, so we must load it dynamically. */ - hMod = GetModuleHandle("advapi32.dll"); + hMod = GetModuleHandleW(L"advapi32.dll"); if (hMod) pfn = (RDKEFunc)GetProcAddress(hMod, "RegDeleteKeyExW"); @@ -1123,7 +1135,7 @@ PyEnumKey(PyObject *self, PyObject *args) if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); - retStr = PyUnicode_FromUnicode(tmpbuf, len); + retStr = PyUnicode_FromWideChar(tmpbuf, len); return retStr; /* can be NULL */ } @@ -1215,8 +1227,8 @@ PyEnumValue(PyObject *self, PyObject *args) static PyObject * PyExpandEnvironmentStrings(PyObject *self, PyObject *args) { - Py_UNICODE *retValue = NULL; - Py_UNICODE *src; + wchar_t *retValue = NULL; + wchar_t *src; DWORD retValueSize; DWORD rc; PyObject *o; @@ -1229,7 +1241,7 @@ PyExpandEnvironmentStrings(PyObject *self, PyObject *args) return PyErr_SetFromWindowsErrWithFunction(retValueSize, "ExpandEnvironmentStrings"); } - retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE)); + retValue = (wchar_t *)PyMem_Malloc(retValueSize * sizeof(wchar_t)); if (retValue == NULL) { return PyErr_NoMemory(); } @@ -1240,7 +1252,7 @@ PyExpandEnvironmentStrings(PyObject *self, PyObject *args) return PyErr_SetFromWindowsErrWithFunction(retValueSize, "ExpandEnvironmentStrings"); } - o = PyUnicode_FromUnicode(retValue, wcslen(retValue)); + o = PyUnicode_FromWideChar(retValue, wcslen(retValue)); PyMem_Free(retValue); return o; } @@ -1394,7 +1406,7 @@ PyQueryValue(PyObject *self, PyObject *args) "RegQueryValue"); } - retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf)); + retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf)); PyMem_Free(retBuf); return retStr; } @@ -1525,7 +1537,7 @@ PySetValueEx(PyObject *self, PyObject *args) { HKEY hKey; PyObject *obKey; - Py_UNICODE *valueName; + wchar_t *valueName; PyObject *obRes; PyObject *value; BYTE *data; @@ -1578,7 +1590,7 @@ PyDisableReflectionKey(PyObject *self, PyObject *args) /* Only available on 64bit platforms, so we must load it dynamically.*/ - hMod = GetModuleHandle("advapi32.dll"); + hMod = GetModuleHandleW(L"advapi32.dll"); if (hMod) pfn = (RDRKFunc)GetProcAddress(hMod, "RegDisableReflectionKey"); @@ -1614,7 +1626,7 @@ PyEnableReflectionKey(PyObject *self, PyObject *args) /* Only available on 64bit platforms, so we must load it dynamically.*/ - hMod = GetModuleHandle("advapi32.dll"); + hMod = GetModuleHandleW(L"advapi32.dll"); if (hMod) pfn = (RERKFunc)GetProcAddress(hMod, "RegEnableReflectionKey"); @@ -1651,7 +1663,7 @@ PyQueryReflectionKey(PyObject *self, PyObject *args) /* Only available on 64bit platforms, so we must load it dynamically.*/ - hMod = GetModuleHandle("advapi32.dll"); + hMod = GetModuleHandleW(L"advapi32.dll"); if (hMod) pfn = (RQRKFunc)GetProcAddress(hMod, "RegQueryReflectionKey"); |