Win32: determine size of passed char[]?

Mar 5, 2019 at 2:47pm
Hi,

I'm programming a 3rd party extension for an application.
The application writes to .ini files (key/value pairs) and I need to read out these entries.

So I'm using

1
2
char buf[4096];
GetPrivateProfileString("sectionName", NULL, "", buf, sizeof(buf), iniPath);


which gets me all key names in the specified section (as per Win doc, if keyName is NULL).
This works fine, but the problem I'm having is I don't know how to best determine the size of the passed char array.
The application could (potentially) have zero entries there or say 50.

Is there something I can pass which can grow dynamically instead of char[] to avoid buffer overflow, or alternatively is there a way to get the number of entries beforehand, or any other approach?

Thanks.
Mar 5, 2019 at 3:03pm
Do you realize that that function is very outdated, meant for Win16?
https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getprivateprofilestring

If you pass the correct value for nSize then you won't overflow your string, however you may not retrieve all of the strings if there is not enough room.

Mar 5, 2019 at 3:25pm
Yes, I know it's outdated.
But as the application is using .ini files to store information (also to make it portable I guess), that's not on my part to decide (I only do an extension for it, not the application itself).


If you pass the correct value for nSize then you won't overflow your string, however you may not retrieve all of the strings if there is not enough room.


Ok, thanks.

Last edited on Mar 5, 2019 at 3:27pm
Mar 7, 2019 at 5:39am
Your buffer value of 4096 is correct. Keep it.

[edit]
I would frankly be shocked if anything in even a modern INI file was too large for that.
Last edited on Mar 7, 2019 at 5:41am
Mar 7, 2019 at 5:16pm
or alternatively is there a way to get the number of entries beforehand, or any other approach?

ini files are just text files, you can open them with standard c++ text file commands if you are truly concerned, and check it, if a line or file is too big, fail out.
Topic archived. No new replies allowed.