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. Importing a DLL at runtime

Importing a DLL at runtime

Scheduled Pinned Locked Moved C#
help
6 Posts 3 Posters 0 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.
  • T Offline
    T Offline
    totig
    wrote on last edited by
    #1

    Hi, I have the situation where I need to create a wrapper for an unmanaged dll. My only problem is that all examples are shown with the dll specified before hand. I would like to specify the dll to use at runtime. If this is possible, please let me know. My reason for this is I have about 10 dll's which are would all have the same signatures, but are designed to access differenct machines. So basically if I am not able to pass in the dll to use when needed, I am going to be stuck having 10 different classes all to do a similar thing. Thanks

    T 1 Reply Last reply
    0
    • T totig

      Hi, I have the situation where I need to create a wrapper for an unmanaged dll. My only problem is that all examples are shown with the dll specified before hand. I would like to specify the dll to use at runtime. If this is possible, please let me know. My reason for this is I have about 10 dll's which are would all have the same signatures, but are designed to access differenct machines. So basically if I am not able to pass in the dll to use when needed, I am going to be stuck having 10 different classes all to do a similar thing. Thanks

      T Offline
      T Offline
      totig
      wrote on last edited by
      #2

      Hi, just to add a bit more to this, the following code would work fine, except that I have to mark dllLocation as const. If I change it, it does not work, and this is exactly what I need. So any help would be appreciated. const string dllLocation = "test.dll"; [DllImportAttribute(dllLocation)] public static extern void SetServerName (string pServerName);

      M L 2 Replies Last reply
      0
      • T totig

        Hi, just to add a bit more to this, the following code would work fine, except that I have to mark dllLocation as const. If I change it, it does not work, and this is exactly what I need. So any help would be appreciated. const string dllLocation = "test.dll"; [DllImportAttribute(dllLocation)] public static extern void SetServerName (string pServerName);

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Hi! At least the name of the DLL has to be constant, if you use this approach. If the path of the dll is unknown at compile time, then you can still change the current directory to the DLL's path at runtime and then use the DLL function, this will work. Regards, mav

        T 1 Reply Last reply
        0
        • M mav northwind

          Hi! At least the name of the DLL has to be constant, if you use this approach. If the path of the dll is unknown at compile time, then you can still change the current directory to the DLL's path at runtime and then use the DLL function, this will work. Regards, mav

          T Offline
          T Offline
          totig
          wrote on last edited by
          #4

          Hi, its never known at runtime what the path will be. You mentioned though 'using this approach'. Could you let me know some other approaches I could use if this one does not help me?

          M 1 Reply Last reply
          0
          • T totig

            Hi, just to add a bit more to this, the following code would work fine, except that I have to mark dllLocation as const. If I change it, it does not work, and this is exactly what I need. So any help would be appreciated. const string dllLocation = "test.dll"; [DllImportAttribute(dllLocation)] public static extern void SetServerName (string pServerName);

            L Offline
            L Offline
            Luis Alonso Ramos
            wrote on last edited by
            #5

            I don't think it's possible to do it dynamically using .NET. What you could do is make an unmanaged DLL that works as a stub, which is always the same. Then you link to that stub DLL from your .NET code. That DLL determines the path of the DLL to load, and uses LoadLibrary and GetProcAddress to link to it. -- LuisR


            Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

            The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

            1 Reply Last reply
            0
            • T totig

              Hi, its never known at runtime what the path will be. You mentioned though 'using this approach'. Could you let me know some other approaches I could use if this one does not help me?

              M Offline
              M Offline
              mav northwind
              wrote on last edited by
              #6

              If everything else fails you can P/invoke LoadLibrary and then try to call the functions in the DLL somehow. I think I saw an article here somewhere doing just this, but I could be wrong. Good luck, mav

              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