• Welcome to Valhalla Legends Archive.
 

Read/write ini

Started by Forged, May 17, 2005, 10:28 PM

Previous topic - Next topic

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.


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


QuoteI wish my grass was Goth so it would cut itself

UserLoser.

Filename doesn't have to be constant.. If either function fails, see Err.LastDllError and look it up

Forged

#2
Works:

strusername = ReadINI(f, "Username", p)

When p is declared as a constant

Works:

strusername = ReadINI(f, "Username", "config.ini")


Does not work:

dim conf as string
conf = "config.ini"
strusername = ReadINI(f, "Username", conf)


It debugs to where  it checks to see if the file exist, and I get the error 'Bad File name' 
QuoteI wish my grass was Goth so it would cut itself

Hdx

You sould get an obhject error.. your not using quotes.
~-~(HDX)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

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
QuoteI wish my grass was Goth so it would cut itself

pianka

#5
You may not realize this, but Visual Basic does not default all file paths to where the binary (executable) is. You need to do:


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


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.

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:


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


Anyways, this works.

Forged

QuoteI wish my grass was Goth so it would cut itself