Index: Doc/library/_winreg.rst =================================================================== --- Doc/library/_winreg.rst (revision 76365) +++ Doc/library/_winreg.rst (working copy) @@ -68,6 +68,29 @@ :exc:`WindowsError` exception is raised. +.. function:: CreateKeyEx(key, sub_key[, res=0][, sam=KEY_ALL_ACCESS]) + + Creates or opens the specified key, returning a :dfn:`handle object` + + *key* is an already open key, or one of the predefined :const:`HKEY_\*` + constants. + + *sub_key* is a string that names the key this method opens or creates. + + *res* is a reserved integer, and must be zero. The default is zero. + + *sam* is an integer that specifies an access mask that describes the desired + security access for the key. Default is :const:`KEY_ALL_ACCESS` + + If *key* is one of the predefined keys, *sub_key* may be ``None``. In that + case, the handle returned is the same key handle passed in to the function. + + If the key already exists, this function opens the existing key. + + The return value is the handle of the opened key. If the function fails, a + :exc:`WindowsError` exception is raised. + + .. function:: DeleteKey(key, sub_key) Deletes the specified key. Index: Lib/test/test_winreg.py =================================================================== --- Lib/test/test_winreg.py (revision 76365) +++ Lib/test/test_winreg.py (working copy) @@ -56,7 +56,7 @@ CloseKey(hkey) DeleteKey(root, subkey) - def WriteTestData(self, root_key): + def WriteTestData(self, root_key, CreateKey=CreateKey): # Set the default value for this key. SetValue(root_key, test_key_name, REG_SZ, "Default value") key = CreateKey(root_key, test_key_name) @@ -166,14 +166,17 @@ except WindowsError: # Use this error name this time pass - def TestAll(self, root_key): - self.WriteTestData(root_key) + def TestAll(self, root_key, create_fn=CreateKey): + self.WriteTestData(root_key, create_fn) self.ReadTestData(root_key) self.DeleteTestData(root_key) def testLocalMachineRegistryWorks(self): self.TestAll(HKEY_CURRENT_USER) + def testLocalMachineRegistryWorksWithCreateKeyEx(self): + self.TestAll(HKEY_CURRENT_USER, CreateKeyEx) + def testConnectRegistryToLocalMachineWorks(self): # perform minimal ConnectRegistry test which just invokes it h = ConnectRegistry(None, HKEY_LOCAL_MACHINE) Index: PC/_winreg.c =================================================================== --- PC/_winreg.c (revision 76365) +++ PC/_winreg.c (working copy) @@ -104,6 +104,21 @@ "The return value is the handle of the opened key.\n" "If the function fails, an exception is raised."); +PyDoc_STRVAR(CreateKeyEx_doc, +"key = CreateKeyEx(key, sub_key, res, sam) - Creates or opens the specified key.\n" +"\n" +"key is an already open key, or one of the predefined HKEY_* constants\n" +"sub_key is a string that names the key this method opens or creates.\n" +"res is a reserved integer, and must be zero. Default is zero.\n" +"sam is an integer that specifies an access mask that describes the desired\n" +" If key is one of the predefined keys, sub_key may be None. In that case,\n" +" the handle returned is the same key handle passed in to the function.\n" +"\n" +"If the key already exists, this function opens the existing key\n" +"\n" +"The return value is the handle of the opened key.\n" +"If the function fails, an exception is raised."); + PyDoc_STRVAR(DeleteKey_doc, "DeleteKey(key, sub_key) - Deletes the specified key.\n" "\n" @@ -1014,6 +1029,29 @@ } static PyObject * +PyCreateKeyEx(PyObject *self, PyObject *args) +{ + HKEY hKey; + PyObject *obKey; + char *subKey; + HKEY retKey; + int res = 0; + REGSAM sam = KEY_ALL_ACCESS; + long rc; + if (!PyArg_ParseTuple(args, "Oz|ii:CreateKeyEx", &obKey, &subKey, + &res, &sam)) + return NULL; + if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) + return NULL; + + rc = RegCreateKeyEx(hKey, subKey, res, NULL, NULL, + sam, NULL, &retKey, NULL); + if (rc != ERROR_SUCCESS) + return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); + return PyHKEY_FromHKEY(retKey); +} + +static PyObject * PyDeleteKey(PyObject *self, PyObject *args) { HKEY hKey; @@ -1576,6 +1614,7 @@ {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc}, {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc}, {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc}, + {"CreateKeyEx", PyCreateKeyEx, METH_VARARGS, CreateKeyEx_doc}, {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc}, {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc}, {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},