Can RegOpenKeyEx/RegCreateKeyEx return NULL as a valid HKEY value?
No, a valid opened HKEY is never NULL.
On the other hand, the value of the returned HKEY is indeterminate if these functions fail. The Win32 API documentation does not say one way or the other if the HKEY gets set to NULL on failure, so you can't rely solely on checking the returned HKEY for NULL vs non-NULL when calling these functions, eg:
HKEY hKey; // uninitialized
RegOpenKeyEx(..., &hKey, ...);
if (hKey) {
// use hKey as needed - undefined behavior!
RegCloseKey(hKey);
}
The functions return 0 on success and non-zero on failure. The caller must look at that to determine whether the HKEY is valid or not, eg:
HKEY hKey;
LONG res = RegOpenKeyEx(..., &hKey, ...);
if (res == ERROR_SUCCESS) {
// use hKey as needed - OK!
RegCloseKey(hKey);
}
And AFAICS, QSettings does exactly that, explicitly settings its HKEY variables to NULL when explicitly detecting failures, which is perfectly fine.
If NULL is a possible valid value for HKEY
NULL is not a valid opened value for these functions to output, thus NULL is available for the application to set its HKEYs to when representing unopened keys. Just like with most other Win32 handle/pointer types.
then QSettings's implementation under Windows has bug.
I'm not seeing any such bug in the code you linked to. All uses of RegOpeKeyEx()/RegCreateKeyEx() are correct, they check the error code before using the returned HKEY, HKEY variables are being set to NULL for unopened keys, and no read/write operations are being performed on a NULL HKEY. So where is the bug?