Querying Memory Object Information

DosQueryMem is used to determine the allocation state and access protection for a specified memory object. The application can query an entire memory object or a range of pages within an object.

The following code fragment uses DosQueryMem to ensure that memory is committed before the application attempts to use the memory:

    #define  INCL_DOSMEMMGR   /* Memory Manager values  */
    #include <os2.h>
    #define HF_STDOUT 1       /* Standard output handle */

    PBYTE   pb;         /* Base address of an allocated object */
    ULONG   ulSize,
            ulFlags,
            ulWritten;
    APIRET  ulrc;       /* Return Code                         */

    ulSize = 4096;

    ulrc = DosAllocMem((PVOID *)&pb,
                       16384,
                       PAG_COMMIT |
                       PAG_WRITE);

    ulrc = DosQueryMem(pb,
                       &ulSize,
                       &ulFlags);     /* Queries first 4096 bytes */

    if (ulFlags & PAG_COMMIT) {     /* If memory is committed, use it */
        ulrc = DosWrite(HF_STDOUT,
                        "\r\n 4KB is committed.\r\n",
                        21,
                        &ulWritten);
    }


[Back: Establishing Access Protection]
[Next: Freeing Memory]