question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GetPrivateProfileSection results are truncated

See original GitHub issue

Describe the bug GetPrivateProfileSection is used to return a section from an INI file.

Consider the following example

[Unicode]
Unicode=yes
[Version]
signature="$CHICAGO$"
Revision=1

I can call the GetPrivateProfileSection function to return the contents of say the Version section. The API returns the results in the following format

signature="$CHICAGO$"\0Revision=1\0\0

When the StringBuilder used in the PInvoke declaration encounters the first null character, it stops building the string, and the rest of the result is discarded.

What code is involved

string inf = "C:\temp\my.ini"
StringBuilder sectionData = new StringBuilder(32000);

var bytesFilled = Kernel32.GetPrivateProfileSection("Version", sectionData, 32000, inf);

// sectionData will now hold only the first element

Expected behavior All items should be available. Perhaps an array of strings is more appopriate.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
dahallcommented, Oct 18, 2020

Would a wrapper class be a valuable addition to do something like the following?

var ini = new InitializationFile(@"C:\temp\my.ini");
var versionSettings = ini.Sections["Version"];
foreach (var keyValuePair in versionSettings)
   Console.WriteLine($"{keyValuePair.Key}={keyValuePair.Value}");
versionSettings["MyNewKey"] = "some value";
0reactions
dahallcommented, Oct 22, 2020

Glad it is useful. I laid in bed stewing on the idea and it came together quickly the next morning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GetPrivateProfileString problem - truncates result - vbCity
The contents of the ini file is dynamically populated by a select statement in the database. All is working fine, except that for...
Read more >
GetPrivateProfileString without Trim?
I have a class that reads entries using GetPrivateProfileString. ... string will be truncated. const int capacity = 255; StringBuilder temp ...
Read more >
How to fix truncated PowerShell output, even when I've ...
Pipe output to Format-Table commandlet, e.g. as follows: Get-ChildItem 'E:\' -Force -Recurse | Select-Object FullName | Format-Table - ...
Read more >
GetPrivateProfileStringW function (winbase.h) - Win32 apps
The GetPrivateProfileStringW (Unicode) function (winbase.h) retrieves a string from the specified section in an initialization file.
Read more >
Thread: [RESOLVED] INI Getprofilesection
I use the following code to read the keys and values under Run section. VB Code: GetPrivateProfileSection "Run", retstring, strbuffer, "win.ini".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found