Search code examples
c++winapitypesregistry

RegSetValueEx with RRF_RT_REG_SZ makes REG_EXPAND_SZ in the registry?


The title pretty much says it all, when I try to use RegSetValueEx to make a REG_SZ size entry, I get a REG_EXPAND_SZ when I look at the registry.

Here is the code I'm using

const char REG_KEY_PATH[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
const char REG_VALUE_NAME[] = "Scroff";

// Get where the app lives
char appLoc[MAX_PATH];
GetModuleFileNameA(NULL, appLoc, MAX_PATH);

// Create the key value
HKEY regKey = 0;
RegCreateKeyExA(HKEY_LOCAL_MACHINE, REG_KEY_PATH, 0, NULL, 
                REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | KEY_WOW64_64KEY, 
                NULL, &regKey, NULL);

RegSetValueExA(regKey, REG_VALUE_NAME, 0, RRF_RT_REG_SZ, (unsigned char*)appLoc, strlen(appLoc));

The code works, but instead of the registry having a REG_SZ type, it's REG_EXPAND_SZ. Why would this be, and how can I fix it?

I am running on Windows 10, using VS 2015 Enterprise.

Thanks.


Solution

  • You need to pass the REG_SZ flag as the 4th parameter of RegSetValueEx() to create a REG_SZ value. RRF_RT_XXX flags are only valid with RegGetValue().

    Your code is creating a REG_EXPAND_SZ value because the value of the RRF_RT_REG_SZ flag is 2, which is coincidentally the same value as the REG_EXPAND_SZ flag.