473,770 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic loading of dlls

Hi guys

I'm writing a console app in C# for which i want to enable the user to have
several "plug-ins". The app has a switch to set the plug-in to use, and at
runtime the required dll is loaded and executed.

I had thought I could do this with a C# class compiled, and just load the
relevant dll file for the class we want, and call it as normal. Of course
this won't work because C# class files need to be compiled into the app?
Correct?

That being the case, how do I load "proper" DLL files at runtime and call
functions from within them?

Secondly, how do I write "proper" DLLs in C# ?

Cheers

Dan
Nov 17 '05 #1
3 5127
Take a look at the Assembly.LoadAs sembly method and the Reflection
namespace.

"dhnriversi de" <da*@musoswire. com> wrote in message
news:67******** *************** ***********@mic rosoft.com...
Hi guys

I'm writing a console app in C# for which i want to enable the user to
have
several "plug-ins". The app has a switch to set the plug-in to use, and at
runtime the required dll is loaded and executed.

I had thought I could do this with a C# class compiled, and just load the
relevant dll file for the class we want, and call it as normal. Of course
this won't work because C# class files need to be compiled into the app?
Correct?

That being the case, how do I load "proper" DLL files at runtime and call
functions from within them?

Secondly, how do I write "proper" DLLs in C# ?

Cheers

Dan

Nov 17 '05 #2
Hi Dan,
I am not sure what you mean by "proper" dll but below is an example of how
you can make a plugin architecture.

I have two assemblies (i.e. dlls) one called PluginConsole which will load
the external dynamic assemblies and contains the main program and an
interface called IPlugin which describes the methods the plugin should
implement. The other assembly is called SomeLibrary which contains a class
called PlugIn1 which implement the IPlugin interface.

//IPlugin.cs inside PluginConsole.d ll
using System;

namespace PluginConsole
{
public interface IPlugin
{
void DoSomething();
}
}
The class in a different dll which implements this interface
//Plugin1.cs inside SomeLibrary.dll
using System;
using PluginConsole;

namespace PluginConsole
{
class PlugnIn1 : PluginConsole.I Plugin
{
public Plugin1(){}

public void DoSomething()
{
Console.WriteLi ne("Plugin1 doing something");
}
}
}
The main plugin application will load the SomeLibrary.dll and then create an
instance of Plugin1. In order to do this you either need to add the
SomeLibrary assembly to the GAC or just copy it the the bin/debug (assuming
that is where you are running from) inside the PluginConsole project so that
the PluginConsole can load the assembly using the friendly name (i.e. the
name of the DLL without the .dll postfix).
//Program.cs inside PluginConsole.d ll
using System;
using System.Reflecti on;

namespace PluginConsole
{
class Program
{
static void Main(string[] args)
{
IPlugin plugin;
Type iPluginInterfac e;

//Load the plugin assembly, using the friendly name
//i.e. the name of the dll without .dll
Assembly dynamicAssembly = Assembly.Load(" SomeLibrary");

//Get all of the types in the assembly
Type[] types = dynamicAssembly .GetTypes();

//loop through all the types looking for one that implements
//the IPlugin interface
foreach (Type type in types)
{
//See if this type implements the interface
iPluginInterfac e = type.GetInterfa ce("IPlugin");

if (iPluginInterfa ce != null)
{
//Need to create an instance of the type -> this
implementation
//below is using the empty constructor so you must have
one.
//This is creating an instance of PlugIn1 then we cast
it to
//IPlugin so that we know how to use it.
plugin = (IPlugin)Activa tor.CreateInsta nce(type, null);

//Can call the interface methods on the type
plugin.DoSometh ing();
}
}
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org

"dhnriversi de" wrote:
Hi guys

I'm writing a console app in C# for which i want to enable the user to have
several "plug-ins". The app has a switch to set the plug-in to use, and at
runtime the required dll is loaded and executed.

I had thought I could do this with a C# class compiled, and just load the
relevant dll file for the class we want, and call it as normal. Of course
this won't work because C# class files need to be compiled into the app?
Correct?

That being the case, how do I load "proper" DLL files at runtime and call
functions from within them?

Secondly, how do I write "proper" DLLs in C# ?

Cheers

Dan

Nov 17 '05 #3
Actually there are two separate problems. Discovering the classes that
implement the interface and creating an instance on demand. You can
dynamically load plug ins from a "plug in" folder:

http://www.geocities.com/Jeff_Louie/OOP/oop13.htm

You can create instances of plugins dynamically at run time on demand
using a
type based dynamic class factory:

http://www.geocities.com/Jeff_Louie/OOP/oop18.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
13291
by: Nancy | last post by:
Hi, When I start my PC(winXP Pro), it always says: Unknown(): Unable toload dynamic library './php_msql.dll' - The specified module could not be found. Then my Apache servre starts, load php module and works well. I copied php_msql.dll to my system32 directory. Who knows what's wrong? Thanks a lot?
9
4493
by: Ender | last post by:
I have an application that I would like third party developers to be able to create Plug-ins that will be dynamically loaded into our application to extend functionality. I have utilized the "Let Users Add Functionality to Your .NET Applications with Macros and Plug-Ins" article at MSDN for the dynamic loading of DLLs http://msdn.microsoft.com/msdnmag/issues/03/10/Plug-Ins/default.aspx
0
1618
by: Oenone | last post by:
I have created a number of "plug-in" DLLs for my ASP.NET application which are dynamically loaded at run-time. This is done by locating the DLL file on disk and loading it using the Assembly.LoadFrom(Filename) function. It's important that the DLLs are shadowed by ASP.NET so that they can be easily updated. To get this to work I have placed the DLLs into the bin\ directory inside my web application. This seems to work fine. However, to...
4
4450
by: yhebib | last post by:
Hello All, I've been browsing and reading all articles I could find on technet ,msdn and other knowledgeable sources to understand the issue I'm dealing with. However, I did not find so far how to fix that. Investigations are still under progress and I hope you'll be able to give hints or feedback that will drive me to the solution. The applications I'm working on until the next release that will soon
6
6879
by: Dan Dorey | last post by:
I actually have two questions here, but I'll start by giving an outline of what I'm trying to do. I'm building an app with a simple plugin architecture (all in the same app domain). I have each plugin in a separate sub-dir inside a "Plugins" directory. Each plugin consists of at least two DLLs (a controller and a driver). However, most drivers also rely on other external DLLs. In one particular case this dependency is to a C++ DLL...
9
2986
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
6
5621
by: Even | last post by:
Hi all, As far as I know, relative address will be assigned by program at link time.Right? Ok, here we go. So if we use a static library, it will give the program an relative address(i mean the function address) at link time. Also the code of the library will load into the program. Go on. Now if we use a dynamic library, what it will give the program?
2
1713
by: =?Utf-8?B?SmF0aW5kZXI=?= | last post by:
Hi, I have written dlls in c# that are being used by other applications. My application works fine if I add these dlls to executable folder,but when I place these dlls in another folder my application crashes. Is there any way to load these dlls from folder other than executable folder. If yes, How will I do that.
8
3137
by: =?Utf-8?B?TWFyaw==?= | last post by:
We've got a wierd failure happening on just one machine. One part of our product uses a 3rd party search implementation (dtSearch). DtSearch has a native core (dten600.dll), late-bound, and a managed wrapper (dtSearchNetApi2.dll). For reasons unknown our build and msi packaging process includes dtSearchNetApi2.dll but not dten600.dll in all packages, as well as a couple of assemblies that reference it, even though they are not used by...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.