0

I am hoping this is a simple question but I have not been able to find the solution in my searching. I have a C# application that needs to load data from several DLLs. Each DLL is guaranteed to have the same function foo(). But I want these DLLs to be plug and play at run time. The way I usually handle DLLs (where I know the name) is using:

[DllImport("my_dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int foo();

How do I do this with a dynamic string for a file name instead (e.g., "my_dll_3934.dll")? Also, there may be more than 1 dll that must be loaded that matches the same signature (e.g., "my_dll_3934.dll" and "my_dll_3935.dll").

The DLLs that will be used are generated by me but I want the end-user to just drop the DLL in as updates/new dlls become available without updating the application. I will be doing appropriate error checking and exception handling.

Thank you in advance.

JLB
  • 35
  • 5
  • 2
    Take a look at this Q&A...I think that the one that haves 11 votes would fill your requirements :) http://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime – Hackerman Sep 29 '16 at 17:12
  • 1
    I don't have a code example immediately, so I'll post this as a comment rather than a solution, but I think you'll need to explore LoadLibrary() and then GetProcAddress() to accomplish this in a dynamic scenario such as that you describe. You can pass a string to LoadLibrary, get a handle to the module, then use GetProcAddress to find the function. – David W Sep 29 '16 at 17:13
  • Also do not forget that if that answer helps you, give it un up vote! – Hackerman Sep 29 '16 at 17:14
  • Thank you @Hackerman. I've done the library handle/proc address on the C++ side but I couldn't seem to get it to work on C# side. The answer you suggested showed me what I needed. I have upvoted his response. – JLB Sep 29 '16 at 19:15
  • I'm glad to help @JLB – Hackerman Sep 29 '16 at 19:22

1 Answers1

0

Answer to my question can be found here:

How can I specify a [DllImport] path at runtime?

in the comment by user Ran. Thanks to Hackerman for pointing me to the correct solution.

Community
  • 1
  • 1
JLB
  • 35
  • 5