00 Votes

Windows API: GetProfileString and GetPrivateProfileString clears quotes

Question by Guest | 2012-11-26 at 06:25

I am using the Windows API functions GetPrivateProfileString, GetProfilString, WritePrivateProfileString and WriteProfilString to store strings in an INI file.

However, often, it comes to the problem that single and also double quotes disappear and can not be read correctly, although the strings are correctly stored in the INI file and can be looked up there with all quotation marks properly.

On a forum, I read that the quotes must be doubled to make it work. But if I do this, another strange behaviour occurs: while for some quotes the problem is solved, the other quotes that have been retrieved correctly before, are read out two times, which is of course not desirable. Is there a solution?

ReplyPositiveNegative
4Best Answer4 Votes

If a string is enclosed with single or double quotes in an INI file, GetProfileString returns the string unquoted.

Example:

[section]
key1 = string       -> returns string
key2 = "string"     -> returns string
key3 = 'string'     -> returns string
key4 = "            -> returns "
key5 = string"      -> returns string"
key6 = "string      -> returns "string
key7 = str"ing      -> returns str"ing
key8 = "string'     -> returns "string'

Although in the INI file, the variants in key1, key2 and key3 are differing and notated with and without quotation marks, GetProfileString delivers the same result for each line. The situation is different in the examples key4 to key8 where the quotes are not enclosing the string.

The Solution

Before writing the string with WriteProfileString, check whether the string has quotation marks of the same type at its front and back. If so, double these quotes while remaining the others, otherwise, leave the string unchanged.
2012-11-27 at 14:13

ReplyPositive Negative
Reply

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.