Loading Plugins (DLLs) on-the-fly

折月煮酒 提交于 2019-12-03 12:00:50

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3818229/loading-plugins-dlls-on-the-fly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!