Registry Function What Am I doing Wrong?

So I'm trying for practice to use a registry function to change the REG_SZ wallpaper key to a string that points to an image on my desktop. I read Microsoft's web site on how to use the function and such etc. I've tried to use it but it won't work even though it compiles. IT doesn't change the Wallpaper registry key.

What am I doing wrong below? I'm sure it's something stupid. This c++ is hard for me lol sometimes I feel like giving up ugh. What does it take to learn all this stuff?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <windows.h>
#include <WinReg.h>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <new>
#include <string>
using namespace std;
int main() 
{
    
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_ALL_ACCESS, &hKey)== ERROR_SUCCESS);

{
                                    
                                printf("Opened the key");
                                    
               unsigned char paper[] = "C:\\Users\\austin\\Dekstop\\happy.png";
                 
                RegSetValueEx(hKey, TEXT("Wallpaper"),0,REG_SZ,(LPBYTE) paper,sizeof(paper));
                
                RegCloseKey(hKey);
}
                 

               
system("PAUSE");


return 0;
}





Last edited on
Semicolon, line 15.
While I appreciate you spotting that I removed that semi-colon the code when it is ran does NOT change the wallpaper string in the registry. Unfortunately. What's up? Any ideas?
This is probably because you compiled with Unicode enabled, but you're trying to write a narrow string literal into the registry.

TCHAR paper[] = TEXT("C:\\Users\\austin\\Dekstop\\happy.png");
should fix this.
GetLastError() may help in finding the error too.

This is probably because you compiled with Unicode enabled, but you're trying to write a narrow string literal into the registry.


Thanks for you help. I will have you know that all of a sudden it just worked (perhaps when I compiled in Visual studio vs dev cpp). Then it did change the registry lol. Can you explain what you mean though Unicode narrow string literal etc?

On line 21, you are making a string of chars, when you should be making one of TCHARs, and surrounding the literal with TEXT().
On line 21, you are making a string of chars, when you should be making one of TCHARs, and surrounding the literal with TEXT().



Gotcha that worked thanks for ya'alls help :)
Topic archived. No new replies allowed.