DllImport Without Attribute
-
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.
-
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.
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 theDllImportAttribute
metadata to link against the native API at runtime. If you're looking for something likeLoadLibrary
andGetProcAddress
, then P/Invoke those methods and use them accordingly. You should be able to declare a delegate that theGetProcAddress
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
-
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.
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