Reference unmanaged assembly

2020-06-03 06:14发布

I'm trying to reference a managed DLL in my .NET project, without copying it into my output directory. So, when my program runs, it runs the DLL from the location it's installed - wherever that is. The problem lies in the fact that this managed DLL calls unmanaged DLLs. When I try and reference the managed DLL, it throws a FileNotFound Exception - Could not load file or assembly 'CharacterGen' or one of its dependencies. The system cannot find file specified. When I set my output to be within the same directory as the DLL I'm referencing, everything works fine. The usual solution would be to also reference the DLLs which are called from within the other assembly. However, I can't reference unmanaged assemblies from my managed program.

Is there a way to reference a managed DLL which calls unmanaged DLLs?

2条回答
够拽才男人
2楼-- · 2020-06-03 06:59

If you asking how to get visual studio to copy the unmanaged DLL to your output directory you can do this:

  1. Add the unmanaged dll as a file or linked file in your project. (right click project -> add -> existing file then either Add or Add as Link).
  2. Select the file in the solution explorer. In the properties window set build action to copy always.
查看更多
甜甜的少女心
3楼-- · 2020-06-03 07:10

If your solution has a bunch of unmanaged C++ dlls that you're building and one or two managed C# assemblies, and they all need to be in the same folder, I've found that setting all of the projects to use the same output directory to be the easiest to manage solution. -- This way, if projects get renamed later, etc., they all still end up in the right location, and if you add a new project later, you don't have to figure out what dlls need to be added in, you just need to set the OutputPath to the exact same thing as your other projects.

To achieve this I set the OutputPath property (for all Configurations and Platforms, for every project) to $(SolutionDir)\bin\$(Configuration).

Generally, I don't include $(Platform) in the path, because for the unmanaged projects it's named Win32 and for the managed it's named x86 -- but if you really need platform segregation you can do some conditional logic in the .csproj files to create a new property that has the desired value.

查看更多
登录 后发表回答