Code Monkey home page Code Monkey logo

delphi_memorymodule's Introduction

MemoryModule — loading DLL from memory (Delphi adaptation)

This code is Delphi translation of MemoryModule.c file by Joachim Bauch with addition of two helper units that enable using MM engine completely transparently.

Resource loading and exe loading, custom functions, user data not implemented yet.

Tested under RAD Studio XE2 and XE6 32/64-bit, Lazarus 32-bit. Demo project included.

Features in brief

With the MemoryModule engine you can store all required DLLs inside your binary to keep it standalone. Additional hook units allow transparent using of MM engine thus allowing switching MM/WinAPI loading as well as enabling 3rd party dynamic-load DLL interfaces that are unaware of MM (tested with Interbase Express components and Firebird client library).

In other words, you can do things like this

try
  ms := TMemoryStream.Create;
  ms.LoadFromFile(ParamStr(1));
  ms.Position := 0;
  lib := MemoryLoadLibary(ms.Memory);
  ms.Free;
  if lib = nil then Exit;

  func := TNativeUIntFunc(MemoryGetProcAddress(lib, PAnsiChar(AnsiString(ParamStr(2)))));
  if @func = nil then Exit;
  
  WriteLn(func);
finally
  MemoryFreeLibrary(lib);
end;

or even like this

if not InstallHook(@GetLibPtrProc) then
begin
  Writeln('Error installing hook');
  Exit;
end;

try
  ms := TMemoryStream.Create;
  ms.LoadFromFile(ParamStr(1));
  ms.Position := 0;
  lib := Pointer(LoadLibrary(PChar(ParamStr(1))));
  ms.Free;
  if lib = nil then Exit;

  func := TNativeUIntFunc(GetProcAddress(HMODULE(lib), PAnsiChar(AnsiString(ParamStr(2)))));
  if @func = nil then Exit;
  
  WriteLn(func);
finally
  FreeLibrary(HMODULE(lib));
  UninstallHook;
end;

See demo project for samples. Good testing sample of parameters is %WinDir%\System32\KernelBase.dll and GetCurrentThread. Note that kernel32.dll and some of the other Windows libraries couldn't be loaded with MM.

References

delphi_memorymodule's People

Contributors

fr0st-brutal avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.