I have come across the term GetPrivateProfileString
in a C++ program. Could anyone give me a simple explanation of the use of this function?
The code on my page is:
GetPrivateProfileString("files", "directory", "/mediadb/files/", directory, os.path.getsize(directory), "apache")
GetPrivateProfileString()
reads values from .ini files.
Way back when, in the days of 16-bit Windows, it was the way to read and write application configuration data. Back then applications stored their configuration in a shared .ini file that lived in the system directory, called win.ini. Bad times!
To read from win.ini you called GetProfileString()
. The private in GetPrivateProfileString()
is indicative of the fact that this wonderful function allowed you to access an .ini file other than win.ini, i.e. one private to your application. If I recall correctly (and my memory is hazy), most applications carried on using win.ini for years and years after it was officially frowned upon to do so.
It so happens that GetPrivateProfileString()
is an incredibly wrinkly beast with terrible performance characteristics and hard to understand oddities. I personally avoid it like the plague and if I have to process .ini files I use bespoke code to do so.
Raymond Chen has a nice article about why .ini files were deprecated in favour of the registry.
It's for reading from .ini files. It's an old win16 API. You shouldn't use it.
From MSDN:
Retrieves a string from the specified section in an initialization file.
Note
This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry.
Syntax
DWORD WINAPI GetPrivateProfileString(
__in LPCTSTR lpAppName,
__in LPCTSTR lpKeyName,
__in LPCTSTR lpDefault,
__out LPTSTR lpReturnedString,
__in DWORD nSize,
__in LPCTSTR lpFileName
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With