Results 1 to 8 of 8

Thread: [RESOLVED] Reading lines(more than normal)..

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    29

    Resolved [RESOLVED] Reading lines(more than normal)..

    Ok, so I'm working on a project for a game. I've come to the point, it's time to ask my first question. I've done quite a bit but I am still a total n00b with coding so please excuse my sloppy terms and explainations.

    I've spent the past 4 hours searching and tryign different things to get my INI to work. Basically, I need my INI to have only Sections and Values. I then need to call the Section to display in a textbox on a button click, formated as it is in the INI..(just linebreaks)

    I've managed to get several methods working but only with entering Key's and I only ever a single Value to display. I'm using the GetPrivateProfileSection instead of String but nothings working any different in the past 4 hours of trying and my g/f is pissed because I've been cheating on her with VB so it's time to ask for help and get this thing finished.. I literally beat myself to death searching for a good eaxmple for my needs but nothing seemed to work out, obviously haha...


    Thanks in advance for any help offered..

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Reading lines(more than normal)..

    Here is a quick sample that reads all sections and all values into Listbox so take a look to see how it's done vs how you did.
    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 GetPrivateProfileSectionNames _
        Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" _
        (ByVal lpszReturnBuffer As String, ByVal nSize As Long, _
        ByVal lpFileName As String) As Long
    
    Private Sub Command1_Click()
    Dim strBuffer As String
    Dim arSections() As String
    Dim arValues() As String
    Dim lngLength&, i%, j%
    
        strBuffer = String$(1024, 0)
        
        'get all sections
        lngLength = GetPrivateProfileSectionNames(strBuffer, 1024, "C:\WINDOWS\ODBC.INI")
        
        strBuffer = Left$(strBuffer, lngLength)
        arSections = Split(strBuffer, vbNullChar)
        
        For i = 0 To UBound(arSections)
            List1.AddItem arSections(i)
            
            'get all keys and their values
            strBuffer = String$(1024, 0)
            lngLength = GetPrivateProfileSection(arSections(i), _
                                                 strBuffer, 1024, _
                                                 "C:\WINDOWS\ODBC.INI")
            strBuffer = Left$(strBuffer, lngLength)
            arValues = Split(strBuffer, vbNullChar)
            For j = 0 To UBound(arValues)
                List1.AddItem vbTab & arValues(j)
            Next j
        Next i
    
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    29

    Re: Reading lines(more than normal)..

    Thanks man, that's definately closer to what I was looking for/needing to get a better understanding of how it was working.

    I'm gonna play around with this for a while, but if you don't mind schoolin' me on, changing it to display in a textbox, without the SectionName(already figured this out) and only showing a specific Section, rather than all of them.

    Thanks again for that code, and for anything else you or anyone else may post..

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Reading lines(more than normal)..

    I put together this small (and very quick) demo project that does the following:

    - let you browse and select INI file
    - all sections are loaded into a combobox
    - upon selecting section from combo values are loaded into a textbox

    so take a look and let us know if that's what you wanted.
    Attached Files Attached Files

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    29

    Re: Reading lines(more than normal)..

    Thanks a ton Rhino(haha, a ton, no pun)... The way you wrote it out gave me a huge headsup on the things I was missing and doing wrong, greatly appreciated!.. I've already pulled enough from it to take out the section names being loaded at all and made button clicks display the values for a predefined Section..

    I have hit a snag though, on my largest Section (400+ lines), for some reason it cuts off less than halfway through displaying all the values under the section. I changed the box that displays it to a RTB with vertical scroll sicne I had some problems with textboxes having a limit on them in the past..

    Any ideas on what I can do to correct this issue? Other than this problem, consider this thread Resolved.

    I've gone ahead and uploaded my INI(in txt format since you cant upload ini) file incase seeing it's formatting may help.. I also put a <------Note in it to show where it stops during loading.. Nothing out of the normal compared to that around and before it so I'm thinking it must be some type of limit..
    Attached Files Attached Files
    Last edited by TwistedGA; Aug 4th, 2008 at 12:27 PM.

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Reading lines(more than normal)..

    Aahhhhh, that's my fault - buffer size was hardcoded to 1024!!! Sorry about that.
    Replace what you have with this:
    Code:
    Public Function GetValues(inifile As String, sectionname As String) As String()
    '===============================================================================
    Dim strBuffer As String
    Dim bufLength As Long
    Dim arValues() As String
    Dim lngLength As Long
    
        bufLength = 10240 '<<< increase this number if you have to
        strBuffer = String$(bufLength, 0)
        lngLength = GetPrivateProfileSection(sectionname, strBuffer, bufLength, inifile)
        strBuffer = Left$(strBuffer, lngLength)
        arValues = Split(strBuffer, vbNullChar)
        
        GetValues = arValues()
    
    End Function

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    29

    Re: Reading lines(more than normal)..

    Awsome! Thread Resolved. Thanks again man, I really appreciate your time and effort in helping a n00b.. Definately hope to one day pay it forward... Cheers!

  8. #8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width