Click to See Complete Forum and Search --> : Question with GetPrivateProfileString


AndyK
January 13th, 2000, 02:09 PM
I use it to retrieve value from .ini file but when on Form_load it reads the value, then it delete it so basically all the data from .ini file will be lost if I'll open then close my program and a second later open it again.. I read from INI when form loads and write to it when for unloads, but how can I prevent data (that was read from ini) from being deleted, I just want to read from INI and leave INI alone, but on form unload it will just overwrite existed value, without losing any data from INI

Thank You

Aaron Young
January 13th, 2000, 02:14 PM
GetPrivateProfileString doesn't delete value that it reads, you must be resetting/passing blank values to the WritePrivateProfileString API, check the Values you are passing to this API in your Forms Unload Event.

Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com

AndyK
January 13th, 2000, 03:17 PM
But how can I fix the code so data inside .ini will remain after GetPrivateProfileString was used

Chris Eastwood
January 14th, 2000, 03:00 AM
Like Aaron said, the GetPrivateProfileString doesn't remove items from your ini file - Are you sure that something else isn't removing them (Notepad etc ?)

Try it with this simple form :


option Explicit
'
private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" (byval lpApplicationName as string, _
byval lpKeyName as Any, byval lpDefault as string, _
byval lpReturnedString as string, byval nSize as Long, _
byval lpFileName as string) as Long
'
private Sub Form_Load()
Dim sBuffer as string
Dim lRet as Long
Dim lSize as Long
'
lSize = 255
sBuffer = string$(lSize, vbNullChar)
lRet = GetPrivateProfileString("me", "TEST", "empty !", sBuffer, _
lSize, "c:\test.ini")
If lRet > 0 then MsgBox Left$(sBuffer, lRet)
'
End Sub




And this ini file in c:\test.ini :

[ME]
TEST=Chris Was Here

- run the program and then close the form - you should see that the ini file hasn't been changed once the program has finished.


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb