Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Plugins (DLLs) on-the-fly

Tags:

d

Is there a way to dynamically load and call functions from DLLs dynamically in D? I'd like my program to be able to load plugins at startup and perhaps on-the-fly as well.

like image 421
Timothy Baldridge Avatar asked Sep 29 '10 01:09

Timothy Baldridge


1 Answers

It depends on how dynamic you want to get. If you want to dynamically load a dll and run some predefined functions, there is a very nice wrapper by Wei Li here. Thanks to the power of templates, it allows you to do things like these:

// define functions
alias Symbol!("MessageBoxW", int function(HWND, LPCWSTR, LPCWSTR, UINT)) mbw;
alias Symbol!("MessageBoxA", int function(HWND, LPCSTR, LPCSTR, UINT)) mba;
// load dll
auto dll = new Module!("User32.dll", mbw, mba);
// call functions
dll.MessageBoxW(null, "Hello! DLL! ", "Hello from MessageBoxW", MB_OK);
dll.MessageBoxA(null, "Hello! DLL! ", "Hello from MessageBoxA", MB_OK);

The code is D1. For D2, you have to replace char[] with string, use toStringz() and possibly remove scope. Edit: my D2 port of this code might be useful to others finding this question.

like image 192
stephan Avatar answered Dec 06 '22 12:12

stephan