词条 | Globallock |
释义 | 说明锁定内存中指定的内存块,并返回一个地址值,令其指向内存块的起始处。除非用 GlobalUnlock 函数将内存块解锁,否则地址会一直保持有效。Windows 为每个内存对象都维持着一个锁定计数。对这个函数的每次调用都应有一个对应的 GlobalUnlock 调用 返回值 Long,如成功,返回内存块的地址;如出错,或者这是一个已被丢弃的“可丢弃”内存块,则返回零。会设置GetLastError 参数表 参数 类型及说明hMem Long,指定一个全局内存块的句柄。 一般情况下我们在编程的时候,给应用程序分配的内存都是可以移动的或者是可以丢弃的,这样能使有限的内存资源充分利用,所以,在某一个时候我们分配的那块内存的地址是不确定的,因为他是可以移动的,所以得先锁定那块内存块,这儿应用程序需要调用API函数GlobalLock函数来锁定句柄。如下: lpMem=GlobalLock(hMem); 这样应用程序才能存取这块内存。 The GlobalLock function locks a global memory object and returns a pointer to the first byte of the object's memory block. GlobalLock函数将一个全局内存对象锁定,并返回该块内存首字节指针 Note The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions. 注意 全局内存管理函数比其他内存管理函数慢,并且不能提供更多的特征。因此,新应用程序应该使用堆函数,同时考虑到兼容性,全局函数仍然用于DDE和剪切板函数。 LPVOID GlobalLock( HGLOBAL hMem // handle to global memory object ); Parameters hMem [in] Handle to the global memory object. This handle is returned by either the GlobalAlloc or GlobalReAlloc function. 参数hMem [输入参数]全局内存对象句柄。该句柄由函数GlobalAlloc或GlobalReAlloc返回。 Return Values If the function succeeds, the return value is a pointer to the first byte of the memory block. If the function fails, the return value is NULL. To get extended error information, call GetLastError. 返回值如果函数执行成功,返回值就是指向内存对象首字节指针,否则返回NULL。调用GetLastError可以得到更多错误信息. Remarks The internal data structures for each memory object include a lock count that is initially zero. For movable memory objects, GlobalLock increments the count by one, and the GlobalUnlock function decrements the count by one. For each call that a process makes to GlobalLock for an object, it must eventually call GlobalUnlock. Locked memory will not be moved or discarded, unless the memory object is reallocated by using the GlobalReAlloc function. The memory block of a locked memory object remains locked until its lock count is decremented to zero, at which time it can be moved or discarded. 备注内存对象的内部数据结构域如锁记数将会初始化为0。对于可移动内存对象,GlobalLock会将计数器加1,而GlobalUnlock函数将计数器减1。对于GlobalLock的每一次调用,都必须相应的调用一次GlobalUnlock函数。除非使用GlobalReAlloc函数对内存对象进行重新分配,否则被锁定的内存空间不能够被移动或释放。直到计数器为0时被锁定的内存块才会可以被移动或者释放。 Memory objects allocated with GMEM_FIXED 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. 以GMEM_FIXED属性开辟的内存块空间的计数器始终为0,对于这样的对象,函数的返回指针值即为该句柄值。 If the specified memory block has been discarded or if the memory block has a zero-byte size, this function returns NULL. 如果指定的内存块已经被释放或者大小为0字节,该函数将返回NULL。 Discarded objects always have a lock count of zero. 释放后的对象的计数器为0。 |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。