Cascade copy of native dll resource through several projects in VS 2010 - c#

Hello I have a Project Solution consisting of several smaller projects. Those projects have dependencies to some others in the following way:
(1) Native unmanaged C++ dll with device control functions
Is used by…
(2) C# Project wrapping native functions to .Net
Is used by…
(3) Adapter wrapping to the special device to a more generally abstraction layer defined by a framework
Is used by…
(4) A simple example implementation tries to use this native device interface through the abstraction framework
Now the Problem: The native dll (1) is added to the .Net wrapper project (2) as "existing item" with "build event = content" and "copy to output… = always" as "Hans Passant" set it in this problem solution. The native dll is used in this wrapper via DllImportAttribute. And this works fine for this project. The dll will be copied to the output directory and can be found by the wrapper.
The wrapper (2) is used as "Reference" from the adapter-wrapper project (3) and also there the native dll (1) is copied to the output directory and can be found and used by the .Net wrapper (2).
BUT…
.. when I than add the adapter-wrapper project (3) to the simple example project (4) as an "Reference" the native dll (1) will not be copied to the output directory of this example project (4). So the dlls will not be found by the .Net wrapper (2) and cause a System.DllNotFoundException.
My question is: How can I make this work, that when I add the adapter-wrapper project (3) to another project, all dlls that are necessary and are in the output-directory (including the native dll (1)), are copied to the referencing projects output directory? And this without adding the native dll as resource to the example implementation as Marc Gravell saying.
Thanks a lot, J

Maybe I'm missing something, but what about changing the Output Path for each of the projects to build to a common "bin" folder?

Related

Best practices for copying DLLs with DllImport in Visual Studio?

I have a solution which contains 2 projects. One is a C++ wrapper for an external, third party library which is contained withing a series of Dll fies. The other is a C# project which references the C++ project via [DllImportAttribute].
What is the proper way to copy these Dlls so that they are found upon execution of the C# project? Are the typically registered with the system? Are all Dlls (both the external library and the C++ project) copied into the C# output folder?
How is this usually done? In a post-build step?
I'm sure there are a few ways to accomplish this, I just want to use the most common, trouble free approach. thanks.
Generally Windows searches a dll in the same directory of the executable file first:
https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
, so copy every dll files into this directory (the C# output folder in the case).
Based on your description, you want to call c++ dll in c# app.
I write the detailed steps in the following link, you could have a look.
how to call c++ dll in c# app

how to merge dll's into one dll

Using c# in visual studio 2012, I've created a class library FileTube.dll
It uses 3 other dll's.
I want to make a single dll to contain those thus I can publish my library via nuget.
I have tried 3 approaches all failing:
approach 1: In vistual studio, I set the "Embed Interop Assembly" to true, I get this error:
Error 2 Cannot embed interop types from assembly 'DiffieHellman.dll' because it is missing the 'GuidAttribute' attribute DiffieHellman.dll
approach 2: I used ILMergeGUI. It generated the code to use for ILMerge which fails with the error:
Object reference not set to an instance of an object.
Here is the command:
"C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe"
/ndebug
/copyattrs
/targetplatform:4.0,"C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
/out:"C:\temp\z.dll"
"C:\\FileTube\TestApp\bin\Debug\FileTube.dll"
"C:\\FileTube\TestApp\bin\Debug\DiffieHellman.dll"
"C:\\FileTube\TestApp\bin\Debug\Org.Mentalis.Security.dll"
"C:\\FileTube\TestApp\bin\Debug\Tamir.SharpSSH.dll"
approach 3:
I followed this tutorial
to use reflection to include the dll assemblies. The difference is that the tutorial has a main executable and is including dll in the executable while I'm trying to include dlls in my dll. so I added the reflection code to the class constructor of my main dll. It compiles but it would fail if I rename that external dll meaning that it's not really loading it.
Any ideas?
You have several options:
use ILMerge (free)
For howto see here and here
OR
use some tool like SmartAssembly (commercial)
it can embed and merge among other things (no need to change your source code)
OR
code that yourself in less than 10 lines (free but minimal source code change)
mark all needed dependencies as "embedded resource" - this way they are included in the EXE file... you need to setup an AssemblyResolve handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime...
(Answer copied from: How to merge multiple assemblies into one?)

c++\cli project dependent c++ dll not copied to c# output directory

I have c++ native dll, lib and headers file. After I reference lib from c++\cli project, it builds successfully, but since c++\cli project - just a wrapper, I reference it in c# project and no native dll appears in c# project output directory after build. How can I fix it? I would not like to use build event to copy dll`s manually.
Thus, I have found only two possible solutions:
post-build event in wrapper library, that copies native dll to all c# projects that require it
add native dll in c# projects directly and set build action to "Copy to output directory" (in accordance with the Lucas response)

How to build a visual studio 2010 solution into a standalone dll?

I have a visual studio 2010 project, uses static and dynamic libs (.lib and .dll), the project is compiled and built successfully as .exe both in release and debug modes, the code is c and c++.
What is the right way to compile and wrapping all the solution to one standalone .dll file.
I want to have one dll file, I'll be able to load it in other c, c++, c# project safely.
in my case I want to load it in teraterm and in other simple self developed c# application.
Visual Studio has a C++ linker option called "Link Library Dependencies": http://msdn.microsoft.com/en-us/library/024awkd1(v=vs.90).aspx
This would link the object files from all projects in the solution directly into the project output (via dependency tracking). Be careful when you use this though, if you use this in an EXE and the DLLs you're linking export symbols your EXE will also export them.
Update:
Here's more detail: What happens is that instead of linking the DLLs (this assumes you have the DLLs you'd like to link set as dependencies of your project in the solution) to produce separate binaries, the linker takes the outputs from each individual project dependency and links them into the final executable/DLL directly, thus creating a monolithic DLL.
A side effect is that (depending on the code) the overall size of the output is reduced a little, the monolithic DLL (or EXE) might be large in general, but smaller in size than the individual output files combined due to link-time optimisations. Any symbols exported by the objects linked will be exported by the final DLL, since they are marked as such in the compilation step.
There's may be a catch, but being in VS2010, you should just click on the startup project, select 'properties' ->
'configuration properties' -> 'general' -> 'configuration type' -> set it to 'dynamic library (dll)'

DLLNotFoundException The Specified module could not be found

I make use of the Belgium Identity Card SDK for reading data from a idcard.
The SDK exists of 2 components: interface dll and a wrapper dll.
In VS2010, i can make a reference to the interface dll, but not to the wrapper dll, so I put it manually in the bin folder. When I migrate my application to another pc on the localhost, it is not able to find the wrapper dll.
Not even when I (on the 2nd pc):
-installed the sdk.
-put the wrapper dll into the bin folder and system32 folder
In visual studio, properties of the interface dll, I've set "Copy Local" to true.
What can I do?
This could just be a difference in path names between machines.
I would create a folder at the top level of your solution and place these DLLs in there. Call it something obvious like "Solution dependencies". Then you can reference them as needed and set them copy to local as required. You wont always be able to reference a DLL, especially if it isn't .NET compatible.
I'm curious about your statement of interface and wrapper dlls. Is the wrapper dll not meant to be a .NET wrapper for a C++ style dll?

Categories