VB icon

Global Memory

Email
Submitted on: 10/18/2017 8:54:15 PM
By: Nicholas Forystek  
Level: Advanced
User Rating: By 1 Users
Compatibility: VB 6.0
Views: 2449
author picture
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Global Memory
' Description: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.
' By: Nicholas Forystek
'**************************************

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


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/15/2017 6:08:17 AMRde

Hi Nick
It is interesting that you have found the pointer return from GlobalLock is the same value as the mem handle passed in.
C programing casts different 32-bit longs for different usage, a pointer is distinctly different from a handle.
You do not write to a handle,but to a pointer. It is interesting that they are both the same value, but the pointer should be unlocked and the handle free'd. It is for the purpose of distinguishing them. Happy coding :)
(If this comment was disrespectful, please report it.)

 
7/15/2017 6:16:14 AMRde

P.S.
I would not trust that they will always be the same on all systems. I would still code pMem = GlobalLock(hMem)
If pMem <> 0 then unlock
Happy coding :)
(If this comment was disrespectful, please report it.)

 
7/15/2017 7:13:58 AMRde

Hi again Nich

I looked into this in MSDN and found this:

Memory objects allocated with the GMEM_FIXED flag always have a lock count of zero. For these objects, the value of the returned pointer is equal to the value of the specified handle.

Happy coding :)
(If this comment was disrespectful, please report it.)

 
7/15/2017 7:16:56 AMRde

The fact that it is still the same value with movable mem is a little weird. I would still follow my second comment.
Happy coding :)
(If this comment was disrespectful, please report it.)

 
7/15/2017 11:05:19 AMRde

This had me curious, so I looked further and found this in the MSDN documentation:

GMEM_FIXED - Allocates fixed memory. The return value is a pointer.

GMEM_MOVEABLE - Allocates movable memory. In Win32, memory blocks are never moved in physical memory, but they can be moved within the default heap. The return value is a handle to the memory object. To translate the handle into a pointer, use the GlobalLock function.

Note movable 'memory blocks are never moved in physical memory', which explains your results.

HOWEVER, I tested my ClipText project with uses GMEM_MOVEABLE and GMEM_DDESHARE flags together, and the value of the hMem and pMem values are not the same!

Hope this helps :)
(If this comment was disrespectful, please report it.)

 
7/15/2017 11:11:05 AMRde

I guess I have missed the point of your use of the GPTR flag,which must be making this work for you.

I will leave you're post alone now!
(If this comment was disrespectful, please report it.)

 
7/15/2017 1:50:52 PMRde

GPTR is just shorthand for GMEM_FIXED | GMEM_ZEROINIT (&H0 Or &H40 == &H40),
so we know from my comment above returns a global pointer, zero initializing the memory.

While GHND combines GMEM_MOVEABLE | GMEM_ZEROINIT (&H2 Or &H40 == &H42),
returning a global handle that is NOT a pointer to the memory.

Your code GMEM_MOVEABLE And VarPtr(hMem) almost always produces a zero result.
VarPtr(hMem) returns the address of the VB variable hMem, its value cannot be known beforehand.
Is this a typo?
(If this comment was disrespectful, please report it.)

 
7/24/2017 5:16:24 PMNicholas Forystek

I did not find or go based on real documentation as well, but in my belief the flags are descriptive in the values as well, for instance no 1, 2, then 4, but also no 1, 2 then 3 exists as flags for what I believe is the both literal and numerical descriptive of GPTR, being global pointer. Absent of a actual pointer or GPTR you'll allocate normal memory, providing either is providing simply a pointer. I noticed another recent occurrence when allocating further, changing the first size, the 64, to 4 and the second size, the 128 changed to 8, the handle doesn't change at all through the whole. This is possible because of sequential memory specification of a list of pointers, it's not similar changing the second value 128 to 12/16.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.