LocalLock Function

The LocalLock function retrieves a pointer to the first byte of a local memory object. This is often used when you need direct access to a memory block allocated with LocalAlloc.

Syntax

LPVOID LocalLock(
    HLOCAL hMem
);

Parameters

Return Value

If the function succeeds, the return value is a pointer to the memory block. If it fails, the return value is NULL. Use GetLastError for extended error information.

Remarks

After you obtain a pointer with LocalLock, you must eventually call LocalUnlock to unlock the memory block. Locking a memory block that is already locked increments an internal lock count; you must call LocalUnlock the same number of times to fully release it.

Example

#include <windows.h>
#include <stdio.h>

int main(void) {
    HLOCAL hMem = LocalAlloc(LMEM_MOVEABLE, 256);
    if (!hMem) return 1;

    LPVOID p = LocalLock(hMem);
    if (!p) return 1;

    strcpy((char*)p, "Hello, LocalLock!");
    printf("%s\n", (char*)p);

    LocalUnlock(hMem);
    LocalFree(hMem);
    return 0;
}

Related Functions

Comments