即时加载插件(DLL) [英] Loading Plugins (DLLs) on-the-fly
问题描述
是否有一种方法可以在D中动态地从DLL动态加载和调用函数?我希望我的程序能够在启动时也可能在运行时加载插件。
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.
推荐答案
它取决于您想要获得的动态。如果要动态加载dll并运行一些预定义的函数,那么Wei Li提供了一个非常好的包装器此处。借助模板的强大功能,它使您可以执行以下操作:
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);
代码为D1。对于D2,您必须将 char []
替换为 string
,使用 toStringz()
并可能删除 scope
。 编辑: 我的D2端口此代码可能对发现此问题的其他人有用。
The code is D1. For D2, you have to replace char[]
with string
, use toStringz()
and possibly remove scope
. my D2 port of this code might be useful to others finding this question.
这篇关于即时加载插件(DLL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!