The code was wrote a half years ago. I wanted to implement https://github.com/bb107/MemoryModulePP at that time. It uses a lot of hacking techniques to load a DLL from memory, and works like magic sometimes (every system API works on the returned handle, just like it is returned by LoadLibrary). However, it crash on some DLLs during my test. So I only implemented the original MemoryModule.
I just notice that MemoryModulePP fixed some bug in recent. Maybe it is worth testing again if I have time.
Great library, and I read from the introduction that:
So that the we can embed all DLLs into the main EXE file.
Is there any example to show how to do it?
It will be very convenient to deploy a single exe files with embedded dlls.
You can just staticRead files in order to load them into a string on compile-time and have them embedded into your program.
If you look at the example in the linked repository it appears like he has a staticReadDll that does this and probably a bit more work for DLLs specifically.
The basic example is the first code at the github homepage:
import memlib
const dll = staticReadDll("sqlite3_64.dll")
proc libversion(): cstring {.cdecl, memlib: dll, importc: "sqlite3_libversion".}
echo libversion()
Yes, the sqlite3_64.dll was already embeded into the main EXE.
If you doubt about it, it is naturally because everything looks so simple. You can test it by deleting or renaming c:/nim/bin/sqlite3_64.dll (or your path in the nim directory), then run the compiled EXE again.
A more gentle way than deletion is to use a batch file like embedded_ssl_run.cmd, It reset the path environment variable so that your exe cannot find the DLL anymore.
More examaples are in the https://github.com/khchen/memlib/tree/master/examples directory.
Does it support and emulate DllMain(DLL_THREAD_ATTACH)? It's been years since I've done stuff in Windows, but when I did - there were a few libraries that failed to be loaded from memory because they depended on this feature:
No. MemoryModule does not support DLL_THREAD_ATTACH. See Limitations in https://www.codeproject.com/Tips/430684/Loading-Win-DLLs-manually-without-LoadLibrary.
It seems MemoryModulePP I mentioned in second post can support that.
nice module
a feature request: embed encrypted dll
Since we have nimcrypto, it is not very difficult.
Here is an example: https://github.com/icyguider/nimcrypt/blob/main/nimcrypt.nim