1

Is there any provision in WinAPI or otherwise for using ini files (or similar style config files) without having to use LPCWSTRs for most things?

My app is using single width ASCII strings throughout, and I've just got round to reading the ini file. Unicode strings are proving to be difficult to deal with and convert between.

If I can't find something fairly simple I think I will just use fstream and be done with it.

Matthew Wilkins
  • 1,335
  • 2
  • 12
  • 12
  • 6
    Just about every function in the WIN32 API have two versions: One for narrow character and one for wide character. I'll bet the ini-file functions are the same. – Some programmer dude Jul 31 '14 at 12:38
  • Wich charset? Do you mean no UTF-16 when you say no Unicode, or do you also want to avoid UTF-8? – Deduplicator Jul 31 '14 at 12:39
  • 2
    Call `GetPrivateProfileStringA`. Note the `A` at the end, for "ASCII". – Cody Gray - on strike Jul 31 '14 at 12:41
  • 1
    possible duplicate of [What encoding Win32 API functions expect?](http://stackoverflow.com/questions/4143110/what-encoding-win32-api-functions-expect) – Cody Gray - on strike Jul 31 '14 at 12:43
  • Just switch your app to use Unicode. – Jonathan Potter Jul 31 '14 at 12:47
  • @CodyGray Yep, that's what I was looking for, thanks. Also I believe this is a duplicate, so please feel free to close this (I'd do it myself but not enough rep). – Matthew Wilkins Jul 31 '14 at 12:47
  • 2
    Do *not* use the legacy INI api functions, they are merely available to port an ancient codebase. They are extraordinarily expensive, ~50 msec to load a single INI parameter. And they do *not* support Unicode, even though the function signatures suggests they do. The INI file content itself is always read assuming the default system code page, like it was done 25 years ago. – Hans Passant Jul 31 '14 at 12:47
  • 1
    @HansPassant I'm not using the registry to make a small, portable utility which only has to load a couple of parameters during a typical use. – Matthew Wilkins Jul 31 '14 at 12:50
  • @MatthewWilkins It's only you that is talking about the registry. Hans is just telling you to deal with your ini files using a proper mechanism. A simple websearch will reveal many C++ libraries that give good support for ini file hanlding. – David Heffernan Jul 31 '14 at 15:37
  • 1
    @CodyGray: the 'A' in text-based API functions means **ANSI**, not **ASCII**. As in, those functions rely on the local OS's default ANSI codepage to process 8bit data. – Remy Lebeau Jul 31 '14 at 18:30
  • Yeah, I know what it means. I thought it was a useful mnemonic in this case. It doesn't really matter, all you're getting is the appropriate parameter in the function signature. The functions don't support Unicode anyway. This is why I post comments; posting answers is far too tiring. – Cody Gray - on strike Aug 01 '14 at 05:35

1 Answers1

0

.INI files are very old stuff. They were existing decades before the Unicode was introduced. They are simple ASCII files. Tons of applications (including mine) are working with them using simple ASCII Api like GetPrivateProfileString.

If your application uses Unicode default, you can write explicitly GetPrivateProfileStringA. This will force all its params to be simple strings.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51