VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Creates a linked list using API calls for Quick and easy storage of large amounts of string/textual

by Dave Cotton (1 Submission)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 3rd May 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Creates a linked list using API calls for Quick and easy storage of large amounts of string/textual data in VB dynamically. No problems with

API Declarations


'--------------------------------------
'Win32 API Constants
'--------------------------------------
Const GHND = &H42
'Same as combining GMEM_MOVEABLE with GMEM_ZEROINIT.
Const GMEM_DDESHARE = &H2000
'Optimize the allocated memory for use in DDE conversations.
Const GMEM_DISCARDABLE = &H100
'Allocate discardable memory. (Cannot be combined with GMEM_FIXED.)
Const GMEM_FIXED = &H0
'Allocate fixed memory. The function's return value is a pointer to the beginning of the memory block. (Cannot be combined with GMEM_DISCARDABLE or GMEM_MOVEABLE.)
Const GMEM_MOVEABLE = &H2
'Allocate moveable memory. The memory block's lock count is initialized at 0 (unlocked). The function's return value is a handle to the beginning of the memory block. (Cannot be combined with GMEM_FIXED.)
Const GMEM_NOCOMPACT = &H10
'Do not compact any memory or discard any discardable memory to allocate the requested block.
Const GMEM_NODISCARD = &H20
'Do not discard any discardable memory to allocate the requested block.
Const GMEM_SHARE = &H2000
'Same as GMEM_DDESHARE.
Const GMEM_ZEROINIT = &H40
'Initialize the contents of the memory block to 0.
Const GPTR = &H40
'Same as combining GMEM_FIXED with GMEM_ZEROINIT.

'List item looks like this:
'Size |Next |Data
'------|------|------
'4Bytes|4Bytes|nBytes
'
'Where Size is the size of the data EXCLUDING the Size and Next bytes.
'Next is a pointer to the next item in the list (or 0 if it is the last item).
'Data - the string to be stored. This is stored bytewise as ANSI text.
Const szPTR = 4
Const szSZ = 4
Const ptrOffSet = 4
Const szOffSet = 0
Const txtOffSet = 8

Private pLast As Long
Private pOffLast As Long
Private pBase As Long

Private lSize As Long

'--------------------------------------
'Win32 API functions
'--------------------------------------
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long



Rate Creates a linked list using API calls for Quick and easy storage of large amounts of string/textual



'Public interface


Public Property Get Text() As String
'Recreate the unicode text from the ANSI storage
'by using StrConv on the Bytes property.
    
    Text = StrConv(Bytes, vbUnicode)

End Property

Public Property Get Bytes() As Byte()
'Retrieve a single byte array from the linked
'list by navigating through and compiling. The size
'of the data is already known handily enough.

    Dim Buff() As Byte
    ReDim Buff(lSize) As Byte
    
    Dim pNext As Long, pCurr As Long, lItemSize As Long, lTotSize As Long
    
    CopyMemory pNext, ByVal pBase + ptrOffSet, szPTR: Debug.Print "pNext is: " & pNext
    CopyMemory lItemSize, ByVal pBase + szOffSet, szSZ
    CopyMemory Buff(lTotSize), ByVal pBase + txtOffSet, lItemSize
    While pNext > 0
        lTotSize = lTotSize + lItemSize
        pCurr = pNext
        CopyMemory pNext, ByVal pCurr + ptrOffSet, szPTR
        CopyMemory lItemSize, ByVal pCurr + szOffSet, szSZ
        CopyMemory Buff(lTotSize), ByVal pCurr + txtOffSet, lItemSize
    Wend

    Bytes = Buff

End Property

Public Property Get Size() As Long
' 'nuff said.
    Size = lSize
End Property

Public Function Add(newText As String)
'Adds an item to the list. If it's the first item it
'set the pBase pointer, otherwise it creates a new block
'for the new data and modifies the last one pointed to by
'pOffLast to point to the next item.

    Dim hMem As Long, lLen As Long
    lLen = LenB(newText) / 2
    lSize = lSize + lLen
    Debug.Print pBase
    
    If pBase = 0 Then
    'First text block
        pBase = GlobalAlloc(GPTR, szSZ + szPTR + lLen)
        pLast = pBase
        pOffLast = pLast + ptrOffSet
        CopyMemory ByVal pBase + txtOffSet, ByVal StrPtr(StrConv(newText, vbFromUnicode)), lLen
        CopyMemory ByVal pBase + szOffSet, lLen, szSZ
    Else
    'Another text block
        hMem = GlobalAlloc(GPTR, szSZ + szPTR + lLen)
        CopyMemory ByVal pOffLast, hMem, szPTR
        pLast = hMem
        pOffLast = pLast + ptrOffSet
        CopyMemory ByVal hMem + txtOffSet, ByVal StrPtr(StrConv(newText, vbFromUnicode)), lLen
        CopyMemory ByVal hMem + szOffSet, lLen, szSZ
    End If
End Function


'Private functions


Private Sub Class_Initialize()
    'Nothing to do here for now
End Sub

Private Sub Class_Terminate()
'Navigates through the list and releases each
'item it passes through.

    Dim pNext As Long, pCurr As Long
    CopyMemory pNext, ByVal pBase + ptrOffSet, szPTR
    GlobalFree pBase
    While pNext > 0
        pCurr = pNext
        CopyMemory pNext, ByVal pCurr + ptrOffSet, szPTR
        GlobalFree pCurr
    Wend
End Sub


Download this snippet    Add to My Saved Code

Creates a linked list using API calls for Quick and easy storage of large amounts of string/textual Comments

No comments have been posted about Creates a linked list using API calls for Quick and easy storage of large amounts of string/textual. Why not be the first to post a comment about Creates a linked list using API calls for Quick and easy storage of large amounts of string/textual.

Post your comment

Subject:
Message:
0/1000 characters