Writing string (REG_SZ) values to the registry in C++

后端 未结 3 681
天涯浪人
天涯浪人 2021-02-01 18:57

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

3条回答
  •  情深已故
    2021-02-01 19:26

    For UNICODE environment:

    //Writing to registry
    HKEY hKey;
    LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows");
    
    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("Sample");
    WCHAR path[80] = TEXT("C:\\samples\\app.exe");
    
    LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)path, sizeof(path));
    
    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.");
    }
    

提交回复
热议问题