Valhalla Legends Forums Archive | Visual Basic Programming | Help With GetPrivateProfileSection

AuthorMessageTime
Dyndrilliac
I was wondering if anyone could help me figure out how to add an entire list of items in a file to a combobox using additem/getprivateprofilesection. Basically I want to add the entire file to the combo box after it's been downloaded(which I have got so far) then im going to have the program delete the file containing the list.

Sample code would be appreciated.
April 6, 2004, 11:18 PM
Adron
It is an ini file? You'll have to make a loop... It seems like basic coding, nothing difficult. Why not try and post what you get?
April 6, 2004, 11:28 PM
Dyndrilliac
I was going to put the list in a .dat file which would work exactly the same as a .ini file.

The thing is I've never used GetPrivateProfileSection, and I was thinking this might be useful to ahieve my goal, I have however used GetPrivateProfileString.

I will try and see what I come up with.
April 6, 2004, 11:34 PM
Dyndrilliac
Ok. This is what I did.

Functions
[code]Public Function GetInternetFile(Inet1 As Inet, myURL As String, DestDIR As String) As Boolean
Dim myData() As Byte
If Inet1.StillExecuting = True Then Exit Function
myData() = Inet1.OpenURL(myURL, icByteArray)

For x = Len(myURL) To 1 Step -1
If Left$(Right$(myURL, x), 1) = "/" Then RealFile$ = Right$(myURL, x - 1)
Next x
myFile$ = RealFile$
Open myFile$ For Binary Access Write As #1
Put #1, , myData()
Close #1

GetInternetFile = True
Exit Function
End Function

Function GetList(Filename As String, AppName As String) As String
Dim sFile As String
Dim sDefault As String
Dim lSize As Integer
Dim l As Long
Dim sUser As String

sUser = Space$(128)
lSize = Len(sUser)
sFile = App.Path & "\" & Filename & ".dat"
sDefault = ""

l = GetPrivateProfileSection(AppName, sUser, lSize, sFile)

sUser = Mid(sUser, 1, InStr(sUser, Chr(0)) - 1)
GetList = sUser
End Function

Public Function DeleteFile(Filename As String)
Kill Filename
End Function[/code]

Call
[code] GetInternetFile Me.Inet, "http://66.177.173.240/bot/Accounts.dat", "\"
Me.LogonID.AddItem GetList("Accounts", "Accounts")[/code]

On Success
[code]DeleteFile App.Path & "\Accounts.dat"[/code]

I made sure everything exists, variables declared, no typos, etc - Doess't work.
April 7, 2004, 12:49 AM
Noodlez
GetPrivateProfileSection works for INI files, unless your .dat file is formatted like an INI, that could be one reason for it not working.

Why don't you just load all of the contents of "account.dat"

Or, show us the format of "accounts.dat" and someone can propose a method of extracting your list.
April 7, 2004, 5:09 AM
Dyndrilliac
My dat file's contents look like this:[quote][Accounts]
Admin
Guest[/quote]
April 7, 2004, 10:17 PM
Stealth
[quote author=Dyndrilliac link=board=31;threadid=6188;start=0#msg53905 date=1081376229]
My dat file's contents look like this:[quote][Accounts]
Admin
Guest[/quote]
[/quote]

GetPrivateProfileString files are formatted as such:

[SectionHeader]
Option=Value
A=B
Whatever=Something Else

You could solve this a couple ways.

Reformat the .dat file so that it fits the format above. You could say:

[Accounts]
Accounts=Admin;Guest;Whatever

Then Split() the string by delimiter.

Or, you could do:

[Accounts]
Count=1 // 0-based
Account0=Admin
Account1=Guest

in which case you would read the Count variable and use it in a loop to draw each account out of the file individually.
April 7, 2004, 10:50 PM
Dyndrilliac
Oooooooh I'm so stupid -.- Thanks stealth, that's a really good idea, I can use a Do Until loop with a counter, thanks.
April 7, 2004, 10:58 PM
Adron
And I looked into your code. This is what I got after changing it around a bit:

[code]
Option Explicit
Private 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
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

Public Function GetInternetFile(Inet1 As Inet, myURL As String, DestDIR As String) As Boolean
Dim myData As String
Dim x As Integer
Dim realfile$
Dim myfile$

If Inet1.StillExecuting = True Then Exit Function
myData = Inet1.OpenURL(myURL, icString)

Debug.Print "downloaded: " & myData

For x = Len(myURL) To 1 Step -1
If Left$(Right$(myURL, x), 1) = "/" Then realfile$ = Right$(myURL, x - 1)
Next x
myfile$ = realfile$
Open myfile$ For Output As #1
Print #1, myData;
Close #1

GetInternetFile = True
Exit Function
End Function

Function GetList(Filename As String, AppName As String) As String
Dim sFile As String
Dim sDefault As String
Dim lSize As Integer
Dim l As Long
Dim sUser As String

sUser = Space$(4096)
lSize = Len(sUser)
sFile = App.Path & "\" & Filename & ".dat"
sDefault = ""

l = GetPrivateProfileSection(AppName, sUser, lSize, sFile)
sUser = Left(sUser, l)

GetList = sUser
End Function

Private Sub Command1_Click()
GetInternetFile Me.Inet, "http://www.valhallalegends.com/pub/Accounts.dat", "\"

Dim ar() As String
Dim ix As Integer
Dim list As String
list = GetList("Accounts", "Accounts")
ar = Split(list, Chr(0))
For ix = LBound(ar) To UBound(ar) - 1
Me.LogonID.AddItem ar(ix)
Next ix
End Sub

[/code]
April 8, 2004, 12:27 AM
Dyndrilliac
I ended up doing this(Works perfectly for those of you who want to use it):

Declares/Functions
[code]Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Function GetStuff(Filename As String, SectionName As String, Data As String) As String
Dim sFile As String
Dim sDefault As String
Dim lSize As Integer
Dim l As Long
Dim sUser As String
sUser = Space$(128)
lSize = Len(sUser)
sFile = App.Path & "\" & Filename
sDefault = ""
l = GetPrivateProfileString(SectionName, Data, sDefault, sUser, lSize, sFile)
sUser = Mid(sUser, 1, InStr(sUser, Chr(0)) - 1)
GetStuff = sUser
End Function

Public Function DeleteFile(Filename As String)
Kill Filename
End Function

Public Function GetInternetFile(Inet1 As Inet, myURL As String, DestDIR As String) As Boolean
Dim myData() As Byte
If Inet1.StillExecuting = True Then Exit Function
myData() = Inet1.OpenURL(myURL, icByteArray)

For x = Len(myURL) To 1 Step -1
If Left$(Right$(myURL, x), 1) = "/" Then RealFile$ = Right$(myURL, x - 1)
Next x
myFile$ = RealFile$
Open myFile$ For Binary Access Write As #1
Put #1, , myData()
Close #1

GetInternetFile = True
Exit Function
End Function[/code]

Call
[code]Dim Count1 As Integer
Dim Count2 As Integer

GetInternetFile Me.Inet, "http://66.177.173.240/bot/Accounts.dat", "\"

Count2 = GetStuff("Accounts.dat", "Main", "Count")

Do Until Count1 > Count2
Count1 = Count1 + 1
If Not GetStuff("Accounts.dat", "Main", "Account" & Count1) = vbNullString Then
Me.LogonID.AddItem GetStuff("Accounts.dat", "Main", "Account" & Count1)
End If
Loop

DeleteFile App.Path & "\Accounts.dat"[/code]
April 8, 2004, 1:27 AM
Grok
[me=Grok]shoots Dryndrilliac for using #1[/me]
April 8, 2004, 3:18 AM

Search