Pass alloc/free functions to MemoryLoadLibraryEx#33
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is driven by a problem I solved recently. And MemoryModule helped me a lot. It works great, it's simple, it's well written and does what it should. So thanks for the great library!
The problem I was solving - I have some legacy x86 code that I want to call from an executable that I'm writing. When I say "legacy" - I mean I only have binaries. The challenge is that the code is not relocatable. That's a bit of a problem on a modern Windows. I came to the conclusion that it's virtually impossible to consistently get the base address that I want at runtime.
With some compiler/linker magic I was able to reserve a section in the image at the offset I want. However, this does not play well with the current interface of MemoryModule. The
MemoryLoadLibraryfunction wants to allocate the memory itself and would not work with my memory chunk.What I did was pass
allocandfreefunctions toMemoryLoadLibraryExand use those instead. The default implementations just callVirtualAlloc/VirtualFree. My implementation sets memory permissions on the pre-allocated memory. Here is a rough example of what I'm doing:I hope it's not an issue that this changes the interface of
MemoryLoadLibraryEx. My rationale behind it is that this is not a public API. At least I didn't find any references to it in the documentation at http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/TL;DR
This pull request allows for
MemoryLoadLibraryExto work with memory allocations different from calls toVirtualAlloc.