简体   繁体   中英

Call function from DLL

It is well known that you can have functions in a dll, reference the DLL and then call the functions from your main executable. I like to know if the reverse way is also possible? So I like to call a function in the main executable from the dll, without having the actual function that should be called inside the dll. Reason: I am working on a pluginsystem.

You're sort of comparing apples and oranges: referencing a dll by the build system is completely different from a plugin system where everything happens at runtime. Typically a plugin system where you would want to call some functions from the plugin host (your exe) would be like this (simplified):

//in a common project
//functions from the host that will be callable by the plugin
public interface PluginHost
{
  void Foo();
}

//the plugin
public interface Plugin
{
  void DoSomething( PluginHost host );
}

//in the exe
class ThePluginHost : PluginHost
{
  //implement Foo
}

//in the plugin
class ThePlugin : Plugin
{
  //implement DoSomething,
  //has access to exe methods through PluginHost
}

//now al that's left is loading the plugin dll dynamically,
//and creating a Plugin object from it.
//Can be done using Prism/MEF etc, that's too broad of a scope for this answer
PluginHost host = new ThePluginHost();
Plugin plugin = CreatePluginInstance( "/path/to/dll" );
plugin.DoSomething( host );

是的,可以在项目中添加可执行文件作为参考,并且可以像从引用的dll调用函数一样使用它们

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM