Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WritePrivateProfileSection & WritePrivateProfileString/Int

Status
Not open for further replies.

huBBLe

Programmer
May 15, 2001
50
SG
Hello,

i want to write a whole section with multiple keys and values all at once with one call (is this actually possible??) using "WritePrivateProfileSection". however, i can't seem to get the correct format that i want.

according to a book, it says "If you want to provide a list of keys and their values, pass the list to the function as the lpString argument with each item in the list delimited with a Null character."

let's say i have section and i some 3 "key = value" pairs, how do i send the list to the function so that it appears as below?

[Section]
key1 = value1
key2 = value2
key3 = value3
:
:
:

I am able to do this using the "WritePrivateProfileString/Int" function but i have to call once for every key-value pair, which i so inefficient.

Thanks for the help
 
Why don't you use the following built-in functions:

GetSetting
SaveSetting
GetAllSettings
DeleteSetting

You will then no longer need to keep the INI file, all this settings will be written in the Windows Registry. And I assure you, they are fast.

Hope this helps,s-) Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Hi BurtanI,

circumstances has it that I need to use the ini files, any ideas?

Thanks 4 the suggestion
 
I use INI file frequentlly from VC and VB.
I assure you that there isn't any other API for the optimization that you want.I have thought of this myself at beginings.

Sorry to tell you this,:-I

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
i = WritePrivateProfileSection("Section", "val1=1" & Chr$(0) & "val1=1" & Chr$(0) & "val2=2" & Chr$(0) & "val3=3" & Chr$(0), lpFileName)

writes
[Section]
val1=1
val1=1
val2=2
val3=3

It actually replaces the whole section.

The read version reads the whole section into a string with 0's splitting the lines.

Peter Meachem
peter@accuflight.com
 
I've thought of this method but i find it pretty silly to be putting in the "=" sign myself, but i guess I'll just have to do it this way cos I don't think there is any other way.

This brings up another question: Does that mean you will have to find some way to parse the string when you use GetPrivateProfileSection?

If so, any idea how I can go abt doing it?

Thank You
 
First of all I have to admire Peter's workaround,

For your need you should use the built-in split function which will return array with the desired elements.

Hope this helps,s-) Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Well if you don't put the equals in how are we supposed to know where the key ends and the value starts.

I'm sure you can manage the parse by yourself.

Use
i2=instr(i1,thestring,chr$(0))

in a loop. Start i1,i2 = 1 and loop until i2 = 0. i1 wil be the start of the section and i2 will be the end, or thereabouts. After extracting the string, make i1=i2.

You might have to fiddle with the above to skip the 0's.
Peter Meachem
peter@accuflight.com
 
It's not a workaround, it's the Windows api. All sorts of useful bits in there for those who hunt around a bit. Peter Meachem
peter@accuflight.com
 
Thanks for all the help guys.

I have managed to write to my ini file successfully. However, when I tried to use GetPrivateProfileSection to try and retrieve the value, it doesn't work anymore.

You see, I used:

'********************************
Dim lonStatus as Long
Dim returnedResult as String

lonStatus = GetPrivateProfileSection(.....)

If lonStatus = 0
MsgBox("Error")
else
gstrKeyValue = returnedResult
'********************************

to check if the calling of the function works, but it always seem to return a value of 0, thereby indicating that there's some error while calling.

I've checked the parameter types, passing ByVal, declarations, etc...but nothing seemed to work! I can Write but I can't Get now.

Any other possible reasons that might cause an error while calling the API function?

Oh yes, one more thing. I'm quite confused over the parameter "nSize". We are suppose to state the size of the buffer for the returned string rite? What is the unit of measure here? Is it byte, char, or whatever...?
 
Yes you must have nsize set. It's the number of bytes in the string you are using.

I use

lpReturnString = Space$(128)
iSize = Len(lpReturnString)
lpAppName = "LD"
lpFileName = App.path & "\LD.ini"

iValid = GetPrivateProfileSection("Section", ByVal lpReturnString, iSize, lpFileName)

the result is Left(lpReturnString, iValid)

If size is 0, then ivalid is too.

Bit of a snag having to know the size of the string before you read it isn't it.

Peter Meachem
peter@accuflight.com
 
1) What is the significance of the return value? What does it tell you? eg. I keep getting a "0", what does it mean? The function failed?

2) What is the maximum size that u can set?

3) Do you need to set the exact size or just an approximation will do?

 
I have worked with APIs from VB many times, one thing I know for sure - you have to set the size of the buffer (only in this cases) to be lees or equal with the size of the string you expect.
Otherwise you will get either an fatal error either an chopped string.
Yes, there is a max value of 32767 for the nSize for platforms like Win 95/97/Me.

Declare the function like:

Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

You shouldn't get any errors. I tried myself and it worked.

Hope this helps,
Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
It finally worked!!

The main line is:
lpReturnString = Space$(128)

I had only declared :
Dim lpReturnString As String

and passed lpReturnString as a parameter straight. Don't know why this is so but that's how it is!

Thanks for all the help guys! Really appreciate it!
:)
 
Sorry guys...hit another problem again.

This problem has got something to do with setting the size of the buffer. Now, both BurtanI and petermeachem pointed out that you will have to know the size of the returned string befor you can set the buffer. I did not understand the significance of it until now.

The problem I have now is that there is no way to know the size of the returned String!My application allows the user to save the data from the textfields, option boxes, comboboxes, etc... Some of these fields may have already been filled up and so they will only contain a Key and no Value associated with it. Therefore, when retrieving, the size of the returned string may vary, depending on how much the user has saved.

Does that mean I can't do things this way anymore? Any way I can get round this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top