Click to See Complete Forum and Search --> : Problem in reading INI File


Chakradhar
August 1st, 1999, 11:31 PM
I have declared the following API in a standard module.
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public 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

My SACM.INI File is like this
[LISTPRICE]
Sample="LPValue"

In form load i have written some thing like this.

i = WritePrivateProfileString("LISTPRICE", "TRY", "100", App.Path & "\SACM.ini")

lnglen = GetPrivateProfileString("LISTPRICE", "Sample", "", strPath, 7, App.Path & "\SACM.ini")

msgbox strpath

The first statement is going through fine. I am able to write into INI file.
The second statement for reading INI file is failing. The variable strPath is showing any value.


please help me to solve this problem.

chakradhar

Software Engineer
IT Solutions India
Banagalore

Ravi Kiran
August 2nd, 1999, 02:15 AM
Because the fn GetPrivateProfilestring tries to write into the memory pointed by lpString and there is no memory currently pointed by lpString.

Also remember VB strings are not simple array of chars.

You should modify your form_load code like this:


i = WritePrivateProfileString("LISTPRICE", "TRY", "100", App.Path & "\SACM.ini")

strPath = Space(256) ' Hope strpath is Dimed in the beginning!!
lnglen = GetPrivateProfileString("LISTPRICE", "Sample", "", strPath, 256, App.Path & "\SACM.ini")
if lnglen > 0 then
strpath = left$(strpath, lnglen)
end if
msgbox strpath

Ravi Kiran

ps: Also, instead of "" you can use VbNullString. They say it has some performance advantages!?!

Lothar Haensler
August 2nd, 1999, 02:17 AM
you didn't post the declaration of strPath.
It needs to be dimmed accordingly as in.


dim strPath as string * 256
..GetPrivateProfileString(...strPath, len(strpath)...