WriteProfileString

2.x

  BOOL WriteProfileString(lpszSection, lpszEntry, lpszString)    
  LPCSTR lpszSection; /* address of section */
  LPCSTR lpszEntry; /* address of entry */
  LPCSTR lpszString; /* address of string to write */

The WriteProfileString function copies a string into the specified section of the Windows initialization file, WIN.INI.

Parameters

lpszSection

Points to a null-terminated string that specifies the section to which the string is to be copied. If the section does not exist, it is created. The name of the section is case-independent; the string may be any combination of uppercase and lowercase letters.

lpszEntry

Points to the null-terminated string containing the entry to be associated with the string. If the entry does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.

lpszString

Points to the null-terminated string to be written to the file. If this parameter is NULL, the entry specified by the lpszEntry parameter is deleted.

Return Value

The return value is nonzero if the function is successful. Otherwise, it is zero.

Comments

Windows keeps a cached version of WIN.INI to improve performance. If all three parameters are NULL, Windows flushes the cache.

Sections in the WIN.INI initialization file have the following form:

[section]
entry=string
.
.
.

Example

The following example calls the GetWindowRect function to retrieve the dimensions of the current window, converts the dimensions of a string, and writes the string to WIN.INI by using the WriteProfileString function. The next time the application is run, it could call the GetProfileString function to read the string, convert it to numbers, and pass the numbers as parameters to the CreateWindow function, thereby creating the window again with the same dimensions it had when the application terminated.

RECT rect;
BOOL fSuccess;
char szBuf[20];

GetWindowRect(hwnd, &rect);

sprintf(szBuf, "%u %u %u %u",
    rect.left, rect.right - rect.left,
    rect.top, rect.bottom - rect.top);

fSuccess = WriteProfileString("MySection",
        "Window dimensions", szBuf);

if (fSuccess)
    MessageBox(hwnd, "String added successfully",
            "WriteProfileString", MB_OK);
else
    MessageBox(hwnd, "String could not be added",
            "WriteProfileString", MB_ICONSTOP);

See Also

GetProfileString, WritePrivateProfileString