Search Tools Links Login

Use REAL memory to store strings. (CopyMemory i.e RtlMoveMemory, LocalAlloc, LocalFree API)


Visual Basic 6, or VB Classic

Use your machines real memory to store large strings instead of varibles that run down your programs resources.

Original Author: Andrew Heinlein (Mouse)

Returns

fun stuff

Side Effects

because you are allocating real memory, your program may crash in DEBUG mode. it's rare though.

API Declarations

'Put this in a MODULE
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal uBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Const LPTR = (&H0 Or &H40)

Code

'Put this code in the SAME MODULE as the API ABOVE
'if you would like to download a working example this code go here:
'http://www.theblackhand.net/mouse/RealMemory.zip
Public Function malloc(Strin As String) As Long
Dim PointerA As Long, lSize As Long

lSize = LenB(Strin) 'Length of string in bytes.

'Allocate the memory needed and returns a pointer to that memory
PointerA = LocalAlloc(LPTR, lSize + 4)
If PointerA <> 0 Then
  'Final allocation
  CopyMemory ByVal PointerA, lSize, 4
  If lSize > 0 Then
   'copy the string to that allocated memory.
   CopyMemory ByVal PointerA + 4, ByVal StrPtr(Strin), lSize
  End If
End If
'return the pointer to the string stored memory
malloc = PointerA
End Function
Public Function RetMemory(PointerA As Long) As String
Dim lSize As Long, sThis As String
If PointerA = 0 Then
  GetMemory = ""
Else
  'get the size of the string stored at pointer "PointerA"
  CopyMemory lSize, ByVal PointerA, 4
  If lSize > 0 Then
   'buffer a varible
   sThis = String(lSize 2, 0)
   'retrive the data at the address of "PointerA"
   CopyMemory ByVal StrPtr(sThis), ByVal PointerA + 4, lSize
   'return the buffer
   RetMemory = sThis
  End If
End If
End Function
Public Sub FreeMemory(PointerA As Long)
'frees up the memory at the address of "PointerA"
LocalFree PointerA
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 153 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.