Loading DLL from Memory
Overview
Sometimes a third-party component is available only as a DLL, but you need to produce a single executable file.
There are several reasons for that:
- You may want to conceal the fact that your application uses a particular DLL.
- You may want to protect the DLL from reverse engineering or tampering.
- You may need a portable, installation-free application without external dependencies.
Solution
BoxedApp SDK allows you to load DLLs directly from memory by creating a virtual file and placing the DLL’s contents inside it.
This makes the operating system believe the file exists physically on disk, even though it resides entirely in memory.
You can obtain the DLL data from any source:
- The Internet or local network
- A database or archive
- Application resources
- Or even generate it dynamically at runtime
After creating a virtual file, simply call LoadLibrary() — the DLL will load and function normally, even though it was never written to the filesystem.
Example
#include "BoxedAppSDK.h"
void LoadDllFromMemory(const BYTE* pData, DWORD dwSize)
{
BoxedAppSDK_Init();
// Create a virtual file in memory
HANDLE hFile = BoxedAppSDK_CreateVirtualFile(
_T("MyVirtualDll.dll"),
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
0,
NULL);
// Write DLL contents into the virtual file
DWORD written;
WriteFile(hFile, pData, dwSize, &written, NULL);
CloseHandle(hFile);
// Load the virtual DLL
HMODULE hDll = LoadLibrary(_T("MyVirtualDll.dll"));
if (hDll)
MessageBox(NULL, _T("DLL loaded successfully!"), _T("Success"), MB_OK);
}uses
Windows, BoxedAppSDK;
procedure LoadDllFromMemory(pData: Pointer; dwSize: DWORD);
var
hFile: THandle;
written: DWORD;
begin
BoxedAppSDK_Init();
// Create a virtual file in memory
hFile := BoxedAppSDK_CreateVirtualFile('MyVirtualDll.dll',
GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, 0, 0);
// Write DLL contents
WriteFile(hFile, pData^, dwSize, written, nil);
CloseHandle(hFile);
// Load the virtual DLL
LoadLibrary('MyVirtualDll.dll');
end;Benefits
- No physical DLL files are created
- Prevents DLL tampering or replacement
- Perfect for self-contained, secure, and portable executables
- Works seamlessly with
LoadLibrary,GetProcAddress, etc.