JustLooking
JustLooking

Reputation: 2486

GetPrivateProfileString without Trim?

Being that this application has evolved over the years, there are still some INI files. I have a class that reads entries using GetPrivateProfileString.

At the top of the class we see this:

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
        string key, string def, StringBuilder retVal,
        int size, string filePath);

And it looks like there is a public method that looks something like this:

    public string IniReadValue(string Section, string Key)
    {
        // If string greater than 254 characters (255th spot is null-terminator),
        // string will be truncated.
        const int capacity = 255;
        StringBuilder temp = new StringBuilder(capacity);
        int i = GetPrivateProfileString(Section, Key, "", temp,
                                        capacity, this.m_Path);
        return temp.ToString();
    }

I recently noticed that GetPrivateProfileString trims it's data. Therefore, if my INI file has an entry like this:

SomeData= Notice the three trailing spaces at front and back of this sentence.

It will retrieve it like (notice that it's trimmed to the left and right - ignore quotes):

"Notice the three trailing spaces at front and back of this sentence."

I don't want it to Trim. Is that out of my control? INI files aren't allowed to have spaces after the equal sign (e.g. SomeData=)?

Upvotes: 2

Views: 2672

Answers (2)

Healer
Healer

Reputation: 290

You can use quotation marks to express your content, when read the content into a string, you can easily to parse the content you want. like this: key = " content "

and you can add some code in Function IniReadValue.

Or You can put/get the message use base64 string, like this: some-key = your-content-in-base64-string and many char issues would not be your problem. But this way is not good for read.

Upvotes: 2

Jakob Möllås
Jakob Möllås

Reputation: 4369

As pointed out in the comments, that is how the API works. If you can live with that, you can at least save some DllImport work by using for example this library/wrapper (includes source, just one file):

IniReader

Upvotes: 2

Related Questions