1

Im trying to create a new key in HKCU with help of this answer from cplusplus:

HKEY hkey; RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hkey); const LPCSTR program = "program"; const wchar_t* path = L"(C:\\Users\\Desktop\\wallpaper.jpg)"; RegSetValueEx(hkey, LPCWSTR(program), 0, REG_SZ, (BYTE*)path, wcslen(path)); RegCloseKey(hkey); 

But it creates a key with a weird name and incomplete data. This is the img of the result. My second attempt was this so question:

HKEY key; const char* path = "C:\\Users\\Desktop\\wallpaper.jpg"; RegOpenKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &key); RegSetValueEx(key, TEXT("value_name"), 0, REG_SZ, LPBYTE(path), sizeof path * sizeof(char)); RegCloseKey(key); 

It creates a key with correct name but a weird data, img. What is the reason of creating a key with such characters or incomplete data ? How should i resolve this ? The key's data is going to be a string. For example:

Name | Type | Data KeyName | REG_SZ | c:\user\... 

UPDATE:[Solved]

By changing Character Set in project properties to Use Multi-Byte Character SetAnd this one will do the trick:

HKEY key; const char* path = R"("C:\Users\Desktop\program.bat")"; RegOpenKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &key); RegSetValueEx(key, TEXT("value_name"), 0, REG_SZ, LPBYTE(path), strlen(path)); RegCloseKey(key); 

I have edited the question for many times, Its much clearer and its not duplicate now. Can you guys reopen it and upvote the question? I have reached my question limit in my 2nd question. Its so cruel.

4
  • Ok i found it. I changed Character Set in project properties to Use Multi-Byte Character Set. Commented Jul 21, 2018 at 9:55
  • The weird character problem is solved now, But the incomplete data(it is not setting complete path) and quoted string data(how you add quotes?) problem still persist. Commented Jul 21, 2018 at 13:05
  • 1
    If you need to cast your strings to get the compiler to accept them, you're doing something wrong. Commented Jul 21, 2018 at 13:11
  • 1
    You are mixing ANSI and Unicode in the same code, and doing so incorrectly. Don't do that. Pick one or the other. And you are misusing sizeof. Your last parameter of RegSetValueEx() is wrong, read the documentation. const char *path = ...; RegSetValueExA(key, "value_name", 0, REG_SZ, (BYTE*)path, strlen(path)); or const wchar_t* path = ...; RegSetValueExW(hkey, L"value_name", 0, REG_SZ, (BYTE*)path, wcslen(path) * sizeof(wchar_t)); And you should not be using RegOpenKey() at all, use RegOpenKeyEx() instead Commented Jul 21, 2018 at 17:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.