Standard case
The return value from GetPrivateProfileString
is the number of characters copied to the buffer, not including the null terminator.
Therefore, you could start with (say) a buffer of 100 _TCHAR
s and check the return value. If it’s 99, then either you exactly guessed the size of the string or (more likely) your buffer is too small, so enlarge it and try again.
“Enumerating” case
The above applies to the standard case of retrieving one string value from an .ini
file. If you are instead passing NULL
as either the lpAppName
or lpKeyName
parameters, in order to enumerate all available values, and you have provided too small of a buffer, the return value will be two less than the buffer size.
Allocation Strategy
You’re going to have to dynamically allocate the buffer. So you’ll probably use std::auto_ptr
or std::unique_ptr
, or maybe a std::vector<_TCHAR>
that you can resize()
as needed. If you don’t know in advance how big the strings will be, I’d recommend starting with something like 250 _TCHAR
s and doubling the size each time you find out the buffer is too small. In practice, I’d bet 250 is enough 99.9999% of the time.
Alternatives
XML file stored under %APPDATA%
; JSON file stored under %APPDATA%
, the Registry…