News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

GetPrivateProfileString and related

Started by Jackal, September 02, 2006, 09:11:55 PM

Previous topic - Next topic

Jackal

My questions is not with this function itself however it is related. My question is I was wondering if there is a way to see how many keys there are in a section. I didnt see a function for this but in my program i can save a list and then later load the list. I perfer to do it in a file rather than the registry. My keys are save as numbers starting at 0 and increasing. If i had the total number of keys i could simply use a loop and read each key. I figure that if there is no way to get the number of keys i can read each key until one fails and i get the default value but i dont see this as being very reliable. Anyways thx in advance for any help.

sinsi

You can use GetPrivateProfileString to get a list of key names:
QuoteIf lpKeyName is NULL, the function copies all key names in the specified section to the supplied buffer.
Then step through the buffer and count them or read their values.

P.S. Watch out, from what I remember if your .INI file is longer than 32kb windows will only read the first 32kb and ignore the rest.
Light travels faster than sound, that's why some people seem bright until you hear them.

Jackal

Ok well since there is no telling really how long this can get my best bet is to loop through until i get the default value returned. I would hate to see it get to 32kb but there may be times that it does. I guess if the user wants to edit the file and mess with it then they should be able to deal with it not working. Thanks for the input tho.. I didnt realize it did that.

Mark Jones

Jackal, you could avoid using API's altogether and parse the code yourself. Think about what you want to do...

1. Load a chunk of the file (say 32kb at a time) into ram
2. Identify the "]" character, because this signals the start of keys
3. While not "[" character, count the number of "=" characters
4. If not EOF, goto step 1 (and read another 32kb...)

I can help you with every step of the code but maybe you'd like to try it yourself first. It would be a great learning excercise. See http://www.masm32.com/board/index.php?topic=1073.0 for further hints.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Jackal

I thought about doing that but I am not sure if this can happen or not but what if there is a "=" sign one of the keys. I know my list may contain them but I am unsure whether the writeprofilestring will write them and how they would appaer although i would image that this would screw up the count.