术语 | globalalloc |
释义 | GlobalAlloc 语法: C++ HGLOBAL WINAPI GlobalAlloc( __in UINT uFlags, __in SIZE_T dwBytes ); GlobalAlloc函数 分配指定的字节数从堆中。 注意:全局函数有更大的开销,并提供比其它内存管理功能较少的功能。新的应用程序应该使用堆,除非文档指出全局函数应该使用功能。有关更多信息,请参阅全局和局部的功能。 参数 uFlags [in] 内存分配的属性。如果指定为零,则默认为GMEM_FIXED。此参数可以是一个或多个下列值除特别指出的是不相容的组合。 ValueMeaning 腺体 0x0042Combines GMEM_MOVEABLE和GMEM_ZEROINIT。 GMEM_FIXED 0x0000Allocates固定的内存。返回值是一个指针。 GMEM_MOVEABLE 0x0002Allocates移动内存。内存块是从来没有搬迁的物理内存中,但它们可以在默认堆感动。 返回值是一个句柄内存对象。为了把该处理成一个指针,使用GlobalLock函数。 此值不能结合GMEM_FIXED。 GMEM_ZEROINIT 0x0040Initializes内存内容为零。 GPTR 0x0040Combines GMEM_FIXED和GMEM_ZEROINIT。 下面的值是过时的,但与16位Windows兼容。它们将被忽略。 GMEM_DDESHARE GMEM_DISCARDABLE GMEM_LOWER GMEM_NOCOMPACT GMEM_NODISCARD GMEM_NOT_BANKED GMEM_NOTIFY GMEM_SHARE dwBytes [in] 的字节数分配。如果这个参数是零,uFlags参数指定GMEM_MOVEABLE,该函数返回的句柄内存对象标记为丢弃。 返回值 如果函数成功,返回值是一个句柄到新分配的内存对象。 如果函数失败,返回值为NULL。为了获得更多错误信息,调用GetLastError。 备注 Windows的内存管理不提供单独的本地堆和全局堆。因此,GlobalAlloc和LocalAlloc功能基本相同。 可移动内存标志腺体和GMEM_MOVABLE增加不必要的开销,并要求锁定安全使用。他们应该避免,除非文件明确规定,他们应该被使用。 新的应用程序应该使用堆函数来分配和管理内存,除非该文件明确指出,全局函数应该被使用。例如,全球功能仍然使用动态数据交换(DDE),剪贴板功能和对象的OLE数据。 最少的内存量GlobalAlloc函数,如果成功,它分配的要求。如果实际拨款额大于所要求的数额,这个过程可以使用全部款项。要确定分配的实际字节数,使用GlobalSize功能。 如果堆没有提供足够的自由空间来满足要求,GlobalAlloc返回NULL。因为NULL是用来表示错误,虚拟地址零从未拨出。它因此,很难探测到的一个NULL指针的使用。 分配的内存具有这种功能的保证是一个8字节对齐。若要执行动态生成的代码,使用VirtualAlloc函数分配内存和VirtualProtect职能,授予PAGE_EXECUTE访问。 要释放内存,使用GlobalFree函数。这是不安全的自由与GlobalAlloc分配的内存使用LocalFree。 实例 下面的代码显示了GlobalAlloc和GlobalFree简单使用。 #include #include void _cdecl main() { PSECURITY_DESCRIPTOR pSD; pSD = (PSECURITY_DESCRIPTOR) GlobalAlloc( GMEM_FIXED, sizeof(PSECURITY_DESCRIPTOR)); // Handle error condition if( pSD == NULL ) { printf("GlobalAlloc failed (%d)\\n", GetLastError()); return; } //see how much memory was allocated printf("GlobalAlloc allocated %d bytes\\n", GlobalSize(pSD)); // Use the memory allocated // Free the memory when finished with it GlobalFree(pSD); } 要求: 最低支持:client-Windows 2000专业版 最低支持server-Windows 2000服务器 HeaderWinbase.h(头文件:winuser.h) LibraryKernel32.lib DLLKernel32.dll 参见 全局和局部功能 GlobalDiscard GlobalFree GlobalLock GlobalSize 堆函数 内存管理功能 如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com) 生成日期:2009年8月27日 ==英文原文==GlobalAlloc Function Allocates the specified number of bytes from the heap. Note The global functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a global function should be used. For more information, see Global and Local Functions . Syntax C++ HGLOBAL WINAPI GlobalAlloc( __in UINT uFlags, __in SIZE_T dwBytes ); Parameters uFlags [in] The 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. ValueMeaning GHND 0x0042Combines GMEM_MOVEABLE and GMEM_ZEROINIT. GMEM_FIXED 0x0000Allocates fixed memory. The return value is a pointer. GMEM_MOVEABLE 0x0002Allocates movable memory. 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. This value cannot be combined with GMEM_FIXED. GMEM_ZEROINIT 0x0040Initializes memory contents to zero. GPTR 0x0040Combines GMEM_FIXED and GMEM_ZEROINIT. The following values are obsolete, but are provided for compatibility with 16-bit Windows. They are ignored. GMEM_DDESHARE GMEM_DISCARDABLE GMEM_LOWER GMEM_NOCOMPACT GMEM_NODISCARD GMEM_NOT_BANKED GMEM_NOTIFY GMEM_SHARE dwBytes [in] The 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 Value 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. To get extended error information, call GetLastError . Remarks Windows memory management does not provide a separate local heap and global heap. Therefore, the GlobalAlloc and LocalAlloc functions are essentially the same. The movable-memory flags GHND and GMEM_MOVABLE add unnecessary overhead and require locking to be used safely. They should be avoided unless documentation specifically states that they should be used. New applications should use the heap functions to allocate and manage memory unless the documentation specifically states that a global function should be used. For example, the global functions are still used with Dynamic Data Exchange (DDE), the clipboard functions, and OLE data objects. If the GlobalAlloc 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. If the heap does not contain sufficient free space to satisfy the request, GlobalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer. Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary. To execute dynamically generated code, use the VirtualAlloc function to allocate memory and the VirtualProtect function to grant PAGE_EXECUTE access. To free the memory, use the GlobalFree function. It is not safe to free memory allocated with GlobalAlloc using LocalFree . Examples The following code shows a simple use of GlobalAlloc and GlobalFree. #include #include void _cdecl main() { PSECURITY_DESCRIPTOR pSD; pSD = (PSECURITY_DESCRIPTOR) GlobalAlloc( GMEM_FIXED, sizeof(PSECURITY_DESCRIPTOR)); // Handle error condition if( pSD == NULL ) { printf("GlobalAlloc failed (%d)\\n", GetLastError()); return; } //see how much memory was allocated printf("GlobalAlloc allocated %d bytes\\n", GlobalSize(pSD)); // Use the memory allocated // Free the memory when finished with it GlobalFree(pSD); } Requirements Minimum supported clientWindows 2000 Professional Minimum supported serverWindows 2000 Server HeaderWinbase.h (include Windows.h) LibraryKernel32.lib DLLKernel32.dll See Also Global and Local Functions GlobalDiscard GlobalFree GlobalLock GlobalSize Heap Functions Memory Management Functions Send comments about this topic to Microsoft Build date: 8/27/2009 ==原始网址==http://msdn.microsoft.com/en-us/library/aa366574(VS.85).aspx\n |
随便看 |
|
windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。