Find WDFMEMORY object given underlying buffer

Is there a way to find the memory object given the underlying buffer? WdfMemoryGetBuffer() provides buffer from WDFMEMORY object, is there a way to do the reverse?

Thank you
Madhavi

At least when i initially implemented it, if memory allocation is less than PAGE_SIZE (roughly), the underlying buffer is allocated as a part of the underlying framework object. Essentially the start of the buffer is ALIGN_UP(object pointer + sizeof(Fx memory object)). The WDF source helps here, you probably want to look at FxMemoryBuffer::GetBuffer, https://github.com/microsoft/Windows-Driver-Frameworks/blob/master/src/framework/shared/core/fxmemorybuffer.cpp, so the underlying buffer is computed

return ((PUCHAR) this) + COMPUTE_RAW_OBJECT_SIZE(sizeof(*this));

So if you have the underling memory pointer, subtract COMPUTE_RAW_OBJECT_SIZE from it, pass that value to !wdfkd.wdfobject and it will return the WDFMEMORY handle value.

Of course, that assumes it was allocated by a KMDF driver…