Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use functions of a dll in another dll "on the fly"

How to use functions of a dll in another dll "on the fly"

Scheduled Pinned Locked Moved C#
visual-studiohelptutorial
6 Posts 4 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AtaChris
    wrote on last edited by
    #1

    Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.

    L L J 3 Replies Last reply
    0
    • A AtaChris

      Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That sounds the wrong way round to me. The base (not master) dll should not have, or even need, any knowledge of extensions. The point of an extension class is to offer extra or enhanced functionality to an application. So the only process that needs to know whether the extensions are present is the application that wants to use its features.

      A 1 Reply Last reply
      0
      • L Lost User

        That sounds the wrong way round to me. The base (not master) dll should not have, or even need, any knowledge of extensions. The point of an extension class is to offer extra or enhanced functionality to an application. So the only process that needs to know whether the extensions are present is the application that wants to use its features.

        A Offline
        A Offline
        AtaChris
        wrote on last edited by
        #3

        The "Master" dll (or base dll) is the application in this case. It is a plugin dll for a software where i do not have the source cose and also extends the functionality of that software. So you could consider the base dll as the application.

        L 1 Reply Last reply
        0
        • A AtaChris

          The "Master" dll (or base dll) is the application in this case. It is a plugin dll for a software where i do not have the source cose and also extends the functionality of that software. So you could consider the base dll as the application.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          That is not something I have ever had to work on.

          1 Reply Last reply
          0
          • A AtaChris

            Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, I have an appliction that loads extensions from a specific folder; each extension is a class that derives from a base class DTF_ComponentBase, which happens to be a UserControl as all extensions need a very similar dialog window. Each extension is built into a separate DLL file. This is the heart of the code that loads those DLL files:

            		private readonly List DTFlist=new List();
            			...
            		// scan DTFAddins folder
            		DTFlist.Clear();
            		string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            		//folder += @"\\DTFAddins";
            		log("folder=" + folder);
            		Directory.CreateDirectory(folder);
            		string\[\] dlls = Directory.GetFiles(folder, "\*.dll");
            		Exception firstException = null;
            		List errors = new List();
            		foreach (string dll in dlls) {
            			if (dll.Contains("LPplatformDLL.dll")) continue;
            			try {
            				log("File: " + dll);
            				if (dll.Contains("DTF\_ComponentBase.dll")) continue;
            				Assembly asm2 = Assembly.LoadFile(dll);
            				Type\[\] types = asm2.GetTypes();
            				// find all classes implementing DTF\_ComponentBase
            				foreach (Type type in types) {
            					//log("    Type=" + type.FullName);
            					if (type.IsSubclassOf(typeof(UserControl))) {
            						UserControl uc = (UserControl)Activator.CreateInstance(type);
            						if (uc is DTF\_ComponentBase comp && !comp.Skip) {
            							DTFlist.Add(comp);
            							break;
            						}
            					}
            				}
            			} catch (Exception exc) {
            				if (firstException == null) firstException = exc;
            				errors.Add("Error loading "+dll+"; "+exc.Message);
            			}
            		}
            

            At the end DTFlist contains all the extensions that were found and instantiated; a foreach loop can then be used to issue commands such as Start, Stop, ... whatever the base class provides. Hope this helps. :)

            Luc Pattyn [My Articles] The Windows 11 taskbar is a disgrace; a third-party add-on is needed to reverse the deterioration. I decline such a downgrade.

            1 Reply Last reply
            0
            • A AtaChris

              Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Presumably you have a well designed understanding of what the additional functionality is in general. There are a number of design patterns that might be useful in structuring the code. - Chain of responsibility - Fly weight - Decorator - Composite - Template As suggested other response you use reflection to load classes dynamically. Those will need either an interface or base class for your code to interact with it. You will need to at least consider dependencies (other dlls required by the dll that is loaded) in that they must be located somewhere. Either load those also dynamically or insure that the application can find them.

              AtaChris wrote:

              with reduced functionality

              That is a design and business consideration which cannot be addressed generically. For example perhaps you want to switch out your database driver. But the application cannot operate without any database. Same thing with supporting multiple card card processor interfaces. If you expect two but only find one then that is ok. But if you find none then the application probably cannot continue. There are ways around failure cases but they add significant complexity and even business risk.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups