Importing a DLL at runtime
-
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
-
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
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);
-
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);
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
-
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
-
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);
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
andGetProcAddress
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
-
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?
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