Hi All,
After more fooling around, I becomed puzzled. I tried on other keys but it seemed not to work... This is weird
I have tried the snippet posted by Benji on other keys and it works correctly. Reading the specification of the function RegSaveKey I have found something that might solve the problem: "If the file already exists, the function fails.". Don't know but maybe you forgot to delete the file...

Regards,
ZaiRoN
Posted on 2003-04-10 06:11:39 by ZaiRoN

Hi All,
I have tried the snippet posted by Benji on other keys and it works correctly. Reading the specification of the function RegSaveKey I have found something that might solve the problem: "If the file already exists, the function fails.". Don't know but maybe you forgot to delete the file...

Regards,
ZaiRoN


I thought about telling him the same, but RegSaveKey returns an error if the file already exists, and he said it was returning no error (ERROR_SUCCESS)... It's a good thing to note, though
Posted on 2003-04-10 10:22:14 by Knightmare
Here is a brief summary of notes:

1) HKEY_CURRENT_USER\Software\Microsoft\Keyboard is only an example, you can try other key too, and the result is the same :-(
2) Both functions (RegOpenKeyEx and RegSaveKey) return ERROR_SUCCESS, that is there are no errors.
3) The file mykey.reg doesn't already exist, it is created after calling RegSaveKey.

After more and more trials with debuggers like softice and ollydbg I suspect that the problem resides in RegOpenKeyEx. The RegSaveKey function creates the file, thus I conclude that the handle of open key is incorrect, therefore the function RegOpenKeyEx isn't able to get the handle or to store it into phkResult.
Another solution I thought was based on this note "The RegSaveKey function saves only nonvolatile keys. It does not save volatile keys. A key is made volatile or nonvolatile at its creation; see RegCreateKeyEx.". So I tried to use RegCreateKeyEx instead of RegOpenKeyEx, but unfortunately it didn't work just the same :-(
The problem is real weird!!!
Posted on 2003-04-11 01:12:28 by Benji

If HKEY_CLASSES_ROOT, HKEY_LOCAL_MACHINE, or HKEY_USERS is specified as the value of the hKey parameter, this function returns ERROR_INVALID_PARAMETER.

- So perhaps it doesn't work with keys below these hives, even if you've opened them yourself? It ought to have failed then, though, I guess.


The calling process must have the SE_BACKUP_NAME privilege enabled.

Perhaps that is the real problem? You might have to get this privilege before you can save the key? (Even for admin processes - just like you can't ExitWindowsEx on NT before you've gotten the correct privilege)
Posted on 2003-04-11 02:08:20 by f0dder
Okay, found the 15 minutes it took to write this with the help of PlatformSDK :).

Note that all the privilege mucking around is only to be done for NT - the APIs don't exist on 9x. So you need to detect if the current running windows version is 9x or NT.

If your original problems where with 9x, I dunno what was wrong. But at least I present a solution that works fine with win2ksp3.
Posted on 2003-04-11 02:58:26 by f0dder
When using this across WIN32 platforms you run into a security issues on the NT platforms, when using KEY_ALL_ACCESS. If you don't need to go through the extra coding, simple use KEY_READ instead. Which requires less security to execute.

I read this somewhere on M$DN.

Regards, P1
Posted on 2003-04-11 08:52:42 by Pone