• Welcome to Valhalla Legends Archive.
 

Help With GetPrivateProfileSection

Started by Dyndrilliac, April 06, 2004, 06:18 PM

Previous topic - Next topic

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

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?

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Dyndrilliac

Ok. This is what I did.

Functions
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


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


On Success
DeleteFile App.Path & "\Accounts.dat"

I made sure everything exists, variables declared, no typos, etc - Doess't work.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

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.

Dyndrilliac

My dat file's contents look like this:
Quote[Accounts]
Admin
Guest
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Stealth

Quote from: Dyndrilliac on April 07, 2004, 05:17 PM
My dat file's contents look like this:
Quote[Accounts]
Admin
Guest

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.
- Stealth
Author of StealthBot

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Adron

#8
And I looked into your code. This is what I got after changing it around a bit:


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


Dyndrilliac

I ended up doing this(Works perfectly for those of you who want to use it):

Declares/Functions
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


Call
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"
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Grok

* Grok shoots Dryndrilliac for using #1