mbl
mbl

Reputation: 925

How to correctly call the HeapSummary API in Win32?

I would like to call the HeapSummary function from the Win32 API:

https://learn.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapsummary

There seems to be missing a list of the possible options for the dwFlags parameter. I tried with no flags but GetLastError() returns The parameter is incorrect error message.

    HEAP_SUMMARY heap_summary;
    HeapSummary(GetProcessHeap(), 0, &heap_summary); // Error: The parameter is incorrect

Upvotes: 1

Views: 419

Answers (1)

Zeus
Zeus

Reputation: 3890

As @Jonathan Potter says, We should set the cb member to sizeof(HEAP_SUMMARY).

I create a sample to find the reason:

#include <windows.h>
#include <heapapi.h>

int main(int argc, const char* argv[])
{
    HEAP_SUMMARY heap_summary;
    memset(&heap_summary, 0, sizeof(heap_summary));
    HANDLE h = GetProcessHeap();
    HeapSummary(h, 0, &heap_summary); // Error: The parameter is incorrect
    DWORD err = GetLastError();
    return 0;
}

Then step into HeapSummary in Disassembly: enter image description here

You can see that it compares the passed pointer with 14h (20), and push 57h (error 87) if it is not equal.

So we need to pass in sizeof(HEAP_SUMMARY) to the cb member of HEAP_SUMMARY.

Regarding the problem described in the document for cb, I will report it to Microsoft for answers.

Upvotes: 3

Related Questions