8

How can I correctly reference a C# DLL that I don't have physical access to? (I actually have the DLL, just not all it's dependencies.)

Background:

On the target computer, there is a program and many interdependent dll files installed in C:\FancyProgram.

I need to write a simple application that will be deployed on the target computer to C:\SimpleProgram.

This simple program will need to make some calls into one of the dll files under C:\FancyProgram. It is a .net managed DLL

On my development machine, I do not have a copy of FancyProgram. I do have that one DLL file, but I do not have any of it's many dependencies. I also can not bundle that DLL into SimpleProgram.

What is the correct method to use and reference this DLL in my Visual Studio project such that my SimpleProgram will correctly compile but load the DLL at runtime from the correct location?

Thank you

nonot1
  • 2,788
  • 4
  • 25
  • 41
  • 2
    Managed (.NET) or unmanaged dll? – Adam Robinson Feb 10 '11 at 16:59
  • Managed. (updated. Thank you.) – nonot1 Feb 10 '11 at 17:01
  • From the correct location will also be a problem in the managed case because from C:\SmpleProgram no dll's will be loaded from C:\FancyProgram. Or are the dependencies GAC-ed? Never tried if a symbolic link will do the trick though (Yeah: http://weblogs.asp.net/israelio/archive/2011/01/10/how-to-overcome-the-clr-fusion-limitation.aspx) – rene Feb 10 '11 at 17:03
  • I have no clue how FancyProgram manages it's own DLL loading. – nonot1 Feb 10 '11 at 17:11
  • If you need to make calls into one of the dll files then you will have to reference it, I assume this Simple Program is also C#, there is no way around this. You need to get the dll that you need from Fancy Program all you have to do is copy and paste. If the dll in question was written the correct way it won't depend on anything else, if it wasn't written correctly you obviously have no choice in the matter. Your question really makes no sense, how can you expect to use a dll that you cannot bundle with the program that will use that one dll file? – Security Hound Feb 10 '11 at 17:12

4 Answers4

8

My recommendation is to create a Facades for the functionality you want to use from that dll. And don't use (reference) it directly - resolve and load it dynamically:
C# - Correct Way to Load Assembly, Find Class and Call Run() Method
Load Assembly at runtime and create class instance

Community
  • 1
  • 1
zihotki
  • 5,201
  • 22
  • 26
3

.Net will do late binding anyway. As long as you don't reference the .dll in any way until you actually mean to load it this will work.

Simply encapsulate all references (fields, properties, methods, instances) into a spearate class or project and make an instance only when you have to. You can then try-catch the load error. See Visual Studio Output window when your app is run, it will tell you when its is attempting to load what .dll.

You may also want to look at these events to make your app handle errors gracefully:

AppDomain.CurrentDomain.AssemblyLoad += ...;
AppDomain.CurrentDomain.AssemblyResolve += ...;
AppDomain.CurrentDomain.UnhandledException += ...;
AppDomain.CurrentDomain.FirstChanceException += ...;

You may also want to take the MEF approach. It is a framework for doing late loading/binding.

Tedd Hansen
  • 12,074
  • 14
  • 61
  • 97
1

You might want to look at the details of LoadLibrary, GetProcAddress and Marshal.GetDelegateForFunctionPointer.

I would also build dll for testing locally, with the same interface as your external dll. How much functionality yo put in there depends on the complexity of the interface and your SimpleProgram.

There were some excellent answers to my old question about importing external dlls.

Community
  • 1
  • 1
Mark Booth
  • 7,605
  • 2
  • 68
  • 92
0

Uhhhh, what are you going to do about testing it? Assuming you've figured that out you need to make sure that either the .dll is in the GAC and reference it that way (ideally) or the .dll needs to be in the same place on all computers. Add the .dll in your references and mark it as Copy Local: false, so you don't deploy it. Good luck.

Gardner
  • 939
  • 7
  • 6
  • Testing will be done on a target machine. (It's just a small part of code). The DLL will be in the exact specified location on all target machines. How do I tell my program to load it at runtime from a specific location? – nonot1 Feb 10 '11 at 17:09
  • Knowing that, I'd go with zihotki's suggestion. – Gardner Feb 10 '11 at 17:19