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. DllImport Without Attribute

DllImport Without Attribute

Scheduled Pinned Locked Moved C#
csharpc++question
3 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.
  • W Offline
    W Offline
    Werdna
    wrote on last edited by
    #1

    I need to use functionality from another dll, but I don't know the dll name and/or entry point at compile time. It will be available at runtime. I'm sure the compiler is doing something to the dllimport attribute to make it happen, so I was wondering if it's possible to do it at runtime. Also, the dll is taking as one of it's attributes class that was written in c++ and is part of that dll. Is it possible to somehow instantiate this class in C#, populate it's fields and pass it to dll? Thanks.

    H J 2 Replies Last reply
    0
    • W Werdna

      I need to use functionality from another dll, but I don't know the dll name and/or entry point at compile time. It will be available at runtime. I'm sure the compiler is doing something to the dllimport attribute to make it happen, so I was wondering if it's possible to do it at runtime. Also, the dll is taking as one of it's attributes class that was written in c++ and is part of that dll. Is it possible to somehow instantiate this class in C#, populate it's fields and pass it to dll? Thanks.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      No, the compiler does link it, the runtime does. All the compiler does is attribute the declaration with the information you provide in the DllImportAttribute for an extern'd method. The runtime (CLR) uses the DllImportAttribute metadata to link against the native API at runtime. If you're looking for something like LoadLibrary and GetProcAddress, then P/Invoke those methods and use them accordingly. You should be able to declare a delegate that the GetProcAddress P/Invoked method would return, but I've never tried this. Seems like with some fanagling you should be able to get this to work. Otherwise, create a native DLL that you would ship with your assembly (in a resolvable path, of course) that takes a string and some params or something then does all the loading and resolving itself.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • W Werdna

        I need to use functionality from another dll, but I don't know the dll name and/or entry point at compile time. It will be available at runtime. I'm sure the compiler is doing something to the dllimport attribute to make it happen, so I was wondering if it's possible to do it at runtime. Also, the dll is taking as one of it's attributes class that was written in c++ and is part of that dll. Is it possible to somehow instantiate this class in C#, populate it's fields and pass it to dll? Thanks.

        J Offline
        J Offline
        Jeremy Kimball
        wrote on last edited by
        #3

        Well, this all depends on the functionality you're using. I think Heath's just about right, you'd have to use PInvoke to load up and determine the starting proc address, but my gut is telling me that you'd probably also need to monkey around with some dynamic method invocation (dig through the System.Reflection namespace). Are you using a COM/ActiveX component located in this DLL? Then definately the Reflection stuff: you'd need to get a dynamic Type reference, then call "InvokeMember" on it. Is it just a chunk of procedural code within the DLL? Your only recourse may be to do the entire thing using PInvoke'd API calls: LoadLibrary, GetProcAddress, and more likely than not, CallProcEx32W. Have fun :) Jeremy Kimball

        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