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,
. 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.
Character Setin project properties toUse Multi-Byte Character Set.data(it is not setting complete path) and quoted string data(how you add quotes?) problem still persist.sizeof. Your last parameter ofRegSetValueEx()is wrong, read the documentation.const char *path = ...; RegSetValueExA(key, "value_name", 0, REG_SZ, (BYTE*)path, strlen(path));orconst wchar_t* path = ...; RegSetValueExW(hkey, L"value_name", 0, REG_SZ, (BYTE*)path, wcslen(path) * sizeof(wchar_t));And you should not be usingRegOpenKey()at all, useRegOpenKeyEx()instead