10

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")
hopper
  • 13,060
  • 7
  • 49
  • 53
smurf
  • 271
  • 2
  • 3
  • 12

4 Answers4

22

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 6
    +1 for not simply copy-pasting from MSDN. But I think `GetPrivateProfileString()` actually reads from an application's private .ini file. The one you're talking about that reads from the common Win.ini file is `GetProfileString()`. – Praetorian Sep 07 '11 at 15:23
  • 1
    @Praetorian Thanks for that. My memory is hazy. It was a long time ago! I've updated the answer so that hopefully it is more accurate. – David Heffernan Sep 07 '11 at 15:30
  • 2
    +1 Thanks very much for your time and patience @David Heffernan!! – smurf Sep 07 '11 at 15:42
  • 5
    I don't agree that the function is solely a relic of 16-bit Windows, nor that it's a wrinkly beast with hard to understand oddities. It's quite simple to understand, actually. I wouldn't recommend using it over better methods, but it's still a supported member of the Win32 API and still functions exactly as described in the documentation. It has some benefits over the truly wrinkly beast known as the registry, which is really not much more than a shiny version of win.ini. Reality is, .ini files were just a simple version of .config files favored by .net today. – Carey Gregory Sep 08 '11 at 05:18
  • 3
    What makes GetPrivateProfileString an incredibly wrinkly beast with oddities? I'm genuinely interested. And for most apps, I don't see how the performance characteristics would matter... App configuration usually isn't the bottleneck... (Obviously, INI files themselves have many flaws as outlined by Raymond Chen in http://blogs.msdn.com/b/oldnewthing/archive/2007/11/26/6523907.aspx - but these seem like INI format flaws and not wrinkles with the GetPrivateProfileString implementation.) – James Johnston Sep 19 '12 at 15:34
  • 1
    And with the resurgence of `app.config`, `web.config`, and `appsettings.json`, people have rediscovered some of the virtues of `.ini` files; while not caring about the downsides no granular security and DoS. But it turns out we just don't *care* about those things. – Ian Boyd Jul 28 '17 at 13:57
  • I don't agree. It is not important for .ini file to be in system directory. It can be in application (exe) directory or any other directory. And .ini file format is not bad - it is very similar to .conf file in Linux/Unix world. Whether function is for compatibility is disputable question. It exists and works. Even with Unicode files! – i486 Aug 08 '23 at 14:41
2

It's for reading from .ini files. It's an old win16 API. You shouldn't use it.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
1

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
);
Macmade
  • 52,708
  • 13
  • 106
  • 123
1

This retrieves configuration information from an .ini file

antlersoft
  • 14,636
  • 4
  • 35
  • 55