2.1.19.4 GlobalAlloc
Description
Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
Syntax
HGLOBAL GlobalAlloc( UINT uFlags, DWORD dwBytes )
Parameters
- uFlags
- [input] Memory allocation attributes. If zero is specified, the default is GMEM_FIXED.
- This parameter can be one or more of the following values, except for the incompatible combinations that are specifically noted.
- dwBytes
- [input] Number of bytes to allocate. If this parameter is zero and the uFlags parameter specifies GMEM_MOVEABLE, the function returns a handle to a memory object that is marked as discarded.
Return
If the function succeeds, the return value is a handle to the newly allocated memory object.
If the function fails, the return value is NULL.
Examples
EX1
int GlobalAlloc_ex1()
{
string strTemp = "Test GlobalAlloc!";
int nMemSize = strTemp.GetLength() + 1;
HANDLE hData = GlobalAlloc(GHND,nMemSize);
if( !hData )
{
out_str("failed!");
return 0;
}
DWORD dSize = GlobalSize(hData);
if( dSize != nMemSize )
{
out_str("failed!");
}
LPSTR lpData = (LPSTR)GlobalLock(hData);
lstrcpyn(lpData, strTemp, nMemSize);
GlobalUnlock(hData);
if( NULL == GlobalReAlloc(hData, nMemSize*2, GMEM_MOVEABLE) )
{
out_str("failed");
return 0;
}
else
{
if( nMemSize*2 != GlobalSize(hData))
{
out_str("failed!");
}
}
if( NULL != GlobalFree(hData) )
{
out_str("failed!");
}
return 1;
}
Remark
Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
All memory is created with execute access; no special function is required to execute dynamically generated code.
If this function succeeds, it allocates at least the amount of memory requested.
If the actual amount allocated is greater than the amount requested, the process can use the entire amount.
To determine the actual number of bytes allocated, use the GlobalSize function.
To free the memory, use the GlobalFree function.
See Also
GlobalReAlloc, GlobalFree, GlobalSize, GlobalLock, GlobalUnlock
Header to Include
origin.h
Reference
|