Valhalla Legends Forums Archive | Visual Basic Programming | Read/write ini

AuthorMessageTime
Forged
I am using the read/write ini I stole from the released sb, and I've come across the problem that it doesn't allow the value of the filename be anything other than Constant.  I wanted an option to allow users to load any of several profiles, but it doesn't work.  I honestlly have no understanding of how the function works which is why I have yet to change it.  If anyone could help me in explaining to me how it works or a way to make it allow input variables work as the file name I would be very grateful.

[code]
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
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 strCurrentUsername As String
    Public ProfileRequest As Boolean

'Read/WriteIni code thanks to ickis
Public Sub WriteINI(wiSection$, wiKey$, wiValue$, wiFile$)
    wiFile$ = (App.Path & "\" & wiFile$)
    WritePrivateProfileString wiSection, wiKey, wiValue, wiFile
End Sub

Public Function ReadINI$(riSection$, riKey$, riFile$)
    Dim sRiBuffer$
    Dim sRiValue$
    Dim sRiLong$
   
    riFile$ = (App.Path & "\" & riFile$)
    If Dir(riFile$) <> "" Then
        sRiBuffer = String(255, vbNull)
        sRiLong = GetPrivateProfileString(riSection, riKey, Chr(1), sRiBuffer, 255, riFile)
        If Left(sRiBuffer, 1) <> Chr(1) Then
            sRiValue = Left(sRiBuffer, sRiLong)
            ReadINI = sRiValue
        End If
    Else
        ReadINI = ""
    End If
End Function
[/code]

May 18, 2005, 3:28 AM
UserLoser.
Filename doesn't have to be constant.. If either function fails, see Err.LastDllError and look it up
May 18, 2005, 11:50 AM
Forged
Works:
[code]
strusername = ReadINI(f, "Username", p)
[/code]
When p is declared as a constant

Works:
[code]
strusername = ReadINI(f, "Username", "config.ini")
[/code]

Does not work:
[code]
dim conf as string
conf = "config.ini"
strusername = ReadINI(f, "Username", conf)
[/code]

It debugs to where  it checks to see if the file exist, and I get the error 'Bad File name' 
May 19, 2005, 3:51 AM
HdxBmx27
You sould get an obhject error.. your not using quotes.
~-~(HDX)~-~
May 19, 2005, 4:44 AM
Forged
I am using quotes, I just wan't paying attention and didn't add them when I was typing it into the forum.

I get runtime error # 52
May 19, 2005, 4:56 AM
PiaNKA
You may not realize this, but Visual Basic does not default all file paths to where the binary (executable) is. You need to do:

[code]
strusername = ReadINI(f, "Username", App.Path & "\Config.ini")
[/code]

You sent me an IM about this at like 1:00 this morning and I had no clue what was going on, but this was on my browser when I got back so I looked at it. Hope it works.

Edit: My bad, I didn't notice that was in the function, ignore everything above said.
May 21, 2005, 12:39 PM
PiaNKA
My apologies for the double post. Anyways, I threw this into a module in VB and tested it. You're having a reference error which is likely because you're changing the value of an argument without taking the value of the argument and putting it into another variable. You CAN do this assuming you don't use ByRef (default for arguments) and put 'ByVal' before all of the arguments you have:

[code]
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
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 strCurrentUsername As String
    Public ProfileRequest As Boolean

'Read/WriteIni code thanks to ickis
Public Sub WriteINI(ByVal wiSection$, ByVal wiKey$, ByVal wiValue$, ByVal wiFile$)
    wiFile$ = (App.Path & "\" & wiFile$)
    WritePrivateProfileString wiSection, wiKey, wiValue, wiFile
End Sub

Public Function ReadINI$(ByVal riSection$, ByVal riKey$, ByVal riFile$)
    Dim sRiBuffer$
    Dim sRiValue$
    Dim sRiLong$
 
    riFile$ = (App.Path & "\" & riFile$)
    If Dir(riFile$) <> "" Then
        sRiBuffer = String(255, vbNull)
        sRiLong = GetPrivateProfileString(riSection, riKey, Chr(1), sRiBuffer, 255, riFile)
        If Left(sRiBuffer, 1) <> Chr(1) Then
            sRiValue = Left(sRiBuffer, sRiLong)
            ReadINI = sRiValue
        End If
    Else
        ReadINI = ""
    End If
End Function
[/code]

Anyways, this works.
May 21, 2005, 12:47 PM
Forged
Thank you sir.
May 21, 2005, 3:48 PM

Search