VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Global Memory

by Nicholas Forystek (14 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Intermediate
Date Added: Mon 10th February 2020
Rating: (0 Votes)

I could not find documentation or anything on the net to which what I had a hunch was true with Global memory allocation (or even Local memory, because Global flags occur across all memory), so I just wrote this API example test to verify it was what I thought, and able you provide the pointer, as to the effect of the flags. GPTR is doing so with out the address memory predisposition, as if, indicate newly provided. So here is just a simple global memory allocation, locking, reallocation, and free of such with the cleanup. Examples of this API I've seen end up with another pointer address handle when locking occurs, I'm not sure how to handle that in freeing or unlocking. Maybe provided it's well to do so with two handle addresses also.

Rate Global Memory

'**************************************
' Name: Global Memory
'**************************************

Private Const GPTR = &H40
Private Const GHND = &H42
Private Const GMEM_FIXED = &H0
Private Const GMEM_MOVEABLE = &H2
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalReAlloc Lib "kernel32" (ByVal hMem As Long, ByVal dwBytes As Long, ByVal wFlags As Long) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (Left As Any, Pass As Any, ByVal Right As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Sub Main()
 Dim hMem As Long
 'print the size, address and value of the memory
 Debug.Print "1- (" & GlobalSize(hMem) & ":" & VarPtr(hMem) & ")=" & hMem
 'moveable container of address as the GPTR flag
 hMem = GlobalAlloc(GMEM_MOVEABLE And VarPtr(hMem), 64)
 'make sure there is no errors
 If hMem = 0 Then Debug.Print "Alloc Error"
 'print the size, address and value of the memory
 Debug.Print "2- (" & GlobalSize(hMem) & ":" & VarPtr(hMem) & ")=" & hMem
 'lock the memory scope, checking for errors
 If GlobalLock(hMem) <> hMem Then Debug.Print "Lock Error"
 'print the size, address and value of the memory
 Debug.Print "3- (" & GlobalSize(hMem) & ":" & VarPtr(hMem) & ")=" & hMem
 'reallocate the GPTR to be twice the size
 hMem = GlobalReAlloc(hMem, 128, GMEM_MOVEABLE Or GPTR)
 'print the size, address and value of the memory
 Debug.Print "4- (" & GlobalSize(hMem) & ":" & VarPtr(hMem) & ")=" & hMem
 'unlock the memory scope, checking for errors
 If GlobalUnlock(hMem) <> 1 Then Debug.Print "Unlock Error"
 'print the size, address and value of the memory
 Debug.Print "5- (" & GlobalSize(hMem) & ":" & VarPtr(hMem) & ")=" & hMem
 'free up the memory, checking for errors
 If GlobalFree(hMem) <> 0 Then Debug.Print "Free Error"
End Sub

Download this snippet    Add to My Saved Code

Global Memory Comments

No comments have been posted about Global Memory. Why not be the first to post a comment about Global Memory.

Post your comment

Subject:
Message:
0/1000 characters