GlobalFree

 

Description

The GlobalFree function frees the specified global memory object and invalidates its handle.

Syntax

HGLOBAL GlobalFree( HGLOBAL hMem )

Parameters

hMem
[input] Handle to the global memory object.

Return

If the function succeeds, the return value is NULL.

If the function fails, the return value is equal to a handle to the global memory object.

Examples

EX1

void GlobalFree_Ex1()
{
        string strTemp = "This is a test";
        int nMemSize = strTemp.GetLength()+1 ;
        
        HANDLE hData = GlobalAlloc(GHND,nMemSize);

        if(!hData)
        {
                out_str("failed to alloc memory\n");
                return ;
        }
        
         DWORD dSize = GlobalSize(hData);
         if(dSize != nMemSize)
         {
                out_str("memory is not enough\n");
                return ;
         }
         
         LPSTR lpData = (LPSTR)GlobalLock(hData);    
     lstrcpyn(lpData, strTemp, nMemSize);
     GlobalUnlock(hData);
     
     out_str(lpData);
     
     if(NULL != GlobalFree(hData))
     {
        out_str("failed to free the allocated memory\n");
        return ;
     }
     else
     {
        lpData = NULL; 
        out_str("success to free the allocated memory");
     }        
}

Remark

See Also

GlobalReAlloc, GlobalReAlloc, GlobalSize, GlobalLock, GlobalUnlock

header to Include

origin.h

Reference