This try_HKLM thing is ugly. I'd suggest having a outer loop over the two registry root keys (HKCU and HKLM) which tries to access the supplied key / value. If the access succeeds then break out of the loop.
I'm afraid I don't understand how to do it that way without duplicating the RegQueryValue part of the code.
The problem is that even if the key exists under HKCU, the value might not. This means RegQueryValueEx would fail, even though the key itself is open under HKCU. But then I have to retry under HKLM. Which means I would have to duplicate the RegQueryValueEx at least twice (if not 3 times) just to see if the value exists first. And then have to keep it in the real loop until the buffer is satisfied. Originally I had "duplicated" this with just getting the size initially, but Alexandre told me to get the string right away with a reasonable size (here MAX_PATH like the original code) so I had to devise this ugly BOOL thing to reduce the amount of code duplication and keep it to a minimum.
Any rough sketches of how it's supposed to be done without duplicating the code? Or should I duplicate it?
Also, be consistent about your brace style (I know the function is already mixed). I'd be happy if you moved all of the opening braces, in code you're rewriting, to a new line.
I actually also prefer it on its own line, but I was trying to copy the style of surrounding code so it doesn't feel out of place. I'll send it on its own line then :-)