Go VC in about Globalalloc,globallock,globalunlock

Source: Internet
Author: User
Tags function prototype

The GlobalAlloc function is called to allocate a chunk of memory that returns the allocated memory handle.
Call the GlobalLock function to lock a block of memory that takes a memory handle as a parameter and then returns a pointer to the locked block of memory. You can use this pointer to read and write memory.
Call the GlobalUnlock function to unlock previously locked memory, which invalidates the pointer to the memory block.
Call the GlobalFree function to free the memory block. You must pass the function a memory handle.


Function Prototypes:
GlobalAlloc (UINT uflag,dword dwbytes);
Allocating a global memory block
return value
Long, which returns the global memory handle. Zero indicates failure. Will set GetLastError
Parameter table
Parameter type and description
Uflags Long, a constant flag that defines the allocated memory type, as follows:
Gmem_fixed allocate a block of fixed memory
Gmem_moveable allocate a removable memory block
Gmem_discardable allocate a disposable memory block
Gmem_nocompact Heap does not accumulate during this function call
No memory blocks are discarded during gmem_nodiscard function calls
Gmem_zeroinit the newly allocated memory blocks are all initialized to 0
Dwbytes Long, number of characters to assign
If gmem_fixed is specified, the return value is the actual memory address to use-the pointer (GlobalLock returns the same value)-so there is no need to perform a globallock/globalunlock operation when using a fixed block of memory
Because WIN32 employs advanced memory management scenarios, there is no benefit in using removable blocks of memory
The memory block allocated with this function is allowed within 8-bit bounds

Function Description: Locks a global Memory object, returns a pointer to the first byte of the object
Function Prototypes:
LPVOID GlobalLock (Hglobal hmem)
Hmem: The handle of the global memory object. This handle is obtained by GlobalAlloc or GlobalRealloc.
return value:
The call succeeded, returning a pointer to the first byte of the object
Call failed, return NULL, can use GetLastError to get error message
Once the GlobalLock has been called to lock a block of memory, be sure to call GlobalUnlock to unlock it.

Function Description: Unlocking a locked global memory object
Function prototype: BOOL GlobalUnlock (Hglobal hmem);
Parameters: Hmem: Handle to Global Memory object
return value:
Non 0 value, the specified memory object is still in a locked state
0, the function execution error, you can use GetLastError to get the error message, if return no_error, it means that the memory object has been unlocked
Note: This function is actually to reduce the lock counter of the memory object by one, if the counter is not 0, it means to execute too many GlobalLock functions to lock the memory object and need the corresponding number of globalunlock functions to unlock.
If the error code returned by the GetLastError function is error_not_locked, it means that it is unlocked or unlocked.

Malloc Memory
Hmem = GlobalAlloc (gmem_moveable | Gmem_ddeshare, nsize);
Lock Memory
Pmem = (BYTE *) GlobalLock (HMEM);
Unlock Memory
GlobalUnlock (HMEM);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.