Issue while Writing\Updating .ini files using RegSetValueEx\WritePrivateProfileString

Sreekanth N Kartha 0 Reputation points
2024-12-17T05:51:39.31+00:00

I’m encountering an issue while updating an INI file. Initially, I attempted to write the value to the registry and then update the INI file using the RegSetValueEx() WinAPI. Although the function executes without returning any errors, the value is not being updated in the registry as expected.

To address this, I switched to using the WritePrivateProfileString() WinAPI. However, a similar issue occurs: the function runs without errors, but the desired output is not achieved. Notably, these issues don’t happen consistently.

Is there a fix or solution for these two problems?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,700 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,804 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 46,191 Reputation points
    2024-12-17T10:13:17.1366667+00:00

    I started with the Microsoft sample code and made some changes as follows -

    #include <windows.h> 
    #include <tchar.h>
    #include <stdio.h> 
    
    int main()
    {
        TCHAR   inBuf[80];
        HKEY   hKey1, hKey2;
        DWORD  dwDisposition;
        LONG   lRetCode;
        TCHAR   szData[] = TEXT("!USR:TestApp\\Section1"); // Added ! prefix by RLWA32
        TCHAR  szPath[] = TEXT("C:\\Program Files (x86)\\abc\\xyz\\appname.ini"); //Added by RLWA32
    
        // Create the .ini file key. 
        lRetCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
            TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping\\appname.ini"),
            0,
            NULL,
            REG_OPTION_NON_VOLATILE,
            KEY_WRITE,
            NULL,
            &hKey1,
            &dwDisposition);
    
        if (lRetCode != ERROR_SUCCESS)
        {
            printf("Error in creating appname.ini key (%d).\n", lRetCode);
            return (0);
        }
    
        // Set a section value 
        lRetCode = RegSetValueEx(hKey1,
            TEXT("Section1"),
            0,
            REG_SZ,
            (BYTE*)szData,
            sizeof(szData));
    
        if (lRetCode != ERROR_SUCCESS)
        {
            printf("Error in setting Section1 value\n");
            // Close the key
            lRetCode = RegCloseKey(hKey1);
            if (lRetCode != ERROR_SUCCESS)
            {
                printf("Error in RegCloseKey (%d).\n", lRetCode);
                return (0);
            }
        }
    
        // Create an TestApp key 
        lRetCode = RegCreateKeyEx(HKEY_CURRENT_USER,
            TEXT("TestApp"),
            0,
            NULL,
            REG_OPTION_NON_VOLATILE,
            KEY_WRITE,
            NULL,
            &hKey2,
            &dwDisposition);
    
        if (lRetCode != ERROR_SUCCESS)
        {
            printf("Error in creating TestApp key (%d).\n", lRetCode);
    
            // Close the key
            lRetCode = RegCloseKey(hKey2);
            if (lRetCode != ERROR_SUCCESS)
            {
                printf("Error in RegCloseKey (%d).\n", lRetCode);
                return (0);
            }
        }
    
        // Force the system to read the mapping into shared memory 
        // so that future invocations of the application will see it 
        // without the user having to reboot the system 
        WritePrivateProfileStringW(NULL, NULL, NULL, L"appname.ini");
    
        // Write some added values 
        WritePrivateProfileString(TEXT("Section1"),
            TEXT("FirstKey"),
            TEXT("It all worked out OK."),
            szPath); // RLWA32 change
        WritePrivateProfileString(TEXT("Section1"),
            TEXT("SecondKey"),
            TEXT("By golly, it works!"),
            szPath); // RLWA32 change
        WritePrivateProfileString(TEXT("Section1"),
            TEXT("ThirdKey"),
            TEXT("Another test..."),
            szPath); // RLWA32 change
    
        // Test 
        GetPrivateProfileString(TEXT("Section1"),
            TEXT("FirstKey"),
            TEXT("Error: GPPS failed"),
            inBuf,
            80,
            szPath);
        _tprintf(TEXT("Key: %s\n"), inBuf);
    
        // Close the keys
        lRetCode = RegCloseKey(hKey1);
        if (lRetCode != ERROR_SUCCESS)
        {
            printf("Error in RegCloseKey (%d).\n", lRetCode);
            return(0);
        }
    
        lRetCode = RegCloseKey(hKey2);
        if (lRetCode != ERROR_SUCCESS)
        {
            printf("Error in RegCloseKey (%d).\n", lRetCode);
            return(0);
        }
    
        return(1);
    }
    

    The path for the ini file under the Program Files(x86) folder existed before the code was run.

    HKLM registry

    HKLMini

    HKCU registry

    HKCUini

    ini file created

    inifilecreated

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.