Send data back to .exe from dll

I am developing a plug-in application and should be able to send data (strings, arrays) back to my main EXE when something happens. I'm not quite sure how to do this, I thought about creating a thread in the main application that will constantly call the method in the DLL with the data request, but it seems very inefficient if there is any kind of event listener that I could use for this ?

Thanks.

+2
source share
5 answers

As part of the loading mechanism for plug-in DLL files, you can pass a pointer / delegate / event to a class / object that the DLL can use to inform the main application of any events that the plug-in generates.

+2
source

Instead of polling a DLL, you should consider using the Inversion of Control form.

It can be as simple as exposing an event in a dll that your exe subscribes to or passes an object (or interface) to a DLL that it can use to call methods to notify your executable, etc. There are many options here, and its hard to find out the best without additional information about your architecture.

+1
source

Can you use delegates / events in your dll and subscribe to your exe?

0
source

Let some baselines be given ...

  • Exe launches
  • EXE loads DLLs containing plugins
  • EXE creates an instance of the type (plugin)
  • The plugin begins to wait for the event.
  • Exe is waiting
  • An external event (in another thread) is marked by the plugin instance
  • EXE event notification

If so, the easiest way is to define an event in your plugin.

public interface IPlugin { public event EventHandler SomethingHappened; public void StartWatchingForSomething(); } where the code would be something like... public static void Main() { foreach (var plugin in LoadAllPluginTypes()) // IoC container, MEF, something { plugin.SomethingHappened += SomethingHappenedEventHandler; plugin.StartWatchingForSomething(); } public void SomethingHappenedEventHandler(object sender, EventArgs e) { //derp } } 

Note that event handlers will run in the same thread as the notification. For example, if your plugin responds to file system events (via FileSystemWatcher), event handlers will run in the same thread as the thread executing the code "defined in the DLL".

If your EXE is a winforms or WPF project, you will need to run Invoke or Dispatcher.Invoke to get the UI thread before updating any visual controls.

0
source

If it is a managed DLL (C #, VB, CLI / C ++ with ref classes)

Link to DLL in project links.

Right-click on the project β†’ Add Link β†’ Browse β†’ Select File.

After that, you should get the API and use it in the usual C # way.

Namespaces declared in the DLL and all objects are available.

0
source

All Articles