views:

1416

answers:

1

Hello,

I've got most of the code for writing a value to the windows registry, however when I change the path to a dummy key and value that I've set up for testing it fails. My code is below:

    HKEY hKey;
    LPCTSTR sk = TEXT("SOFTWARE\TestSoftware");

    LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS , &hKey);

    if (openRes==ERROR_SUCCESS) {
     printf("Success opening key.");
    } else {
     printf("Error opening key.");
    }

    LPCTSTR value = TEXT("TestSoftwareKey");
    LPCTSTR data = "TestData\0";

    LONG setRes = RegSetValueEx (hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data)+1);

    if (setRes == ERROR_SUCCESS) {
     printf("Success writing to Registry.");
    } else {
     printf("Error writing to Registry.");
    }

    LONG closeOut = RegCloseKey(hKey);

    if (closeOut == ERROR_SUCCESS) {
     printf("Success closing key.");
    } else {
     printf("Error closing key.");
    }

All three tests yield error statuses.

The part that confuses me is that I was able to run this code when pointing it at other portions of the registry. Any ideas?

thanks, brian

+6  A: 

I feel silly. The solution is that need to properly escape the slash in the string as follows:

LPCTSTR sk = TEXT("SOFTWARE\\TestSoftware");

Hopefully someone finds this useful...

sweeney