Using C# library in another directory from C++ application - c#

I have a class, written in C#, shared between a C# and a C++ application.
To use it in the C++ app, I wrote a CLI wrapper class.
Simple diagram:
C++ App ---accesses---> CLI Library ---accesses---> C# library
Unfortunately, because of some name conflicts beyond my control, the C# and C+ applications need to be in separate directories.
What options are there for having my C++ app access these libraries in another directory?
Can I use AfxLoadLibrary in my C++ app to load the CLI library? In that case,
would the CLI dll and the C# dll have to be in the same directory?
Can I have the managed CLI library load the C# library dynamically?
Are there any other options that I'm missing?

Another option:
Add a yourapp.exe.config file to your exe folder, listing one or more subfolders as probing paths. Put the files for each library assembly into one of the subfolders. You can put all assemblies (except the application assembly) into one folder if you want.

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

Plugin architecture in C# with external dll like Open.XML from Nuget

I have created a plugin architecture in C# app. In special folder I upload dlls and system search for certain interface and using reflection invokes function within plugin. But one of plugins (dlls) references to Open.XML dll which is not installed on the server where app is running. Is it a way to create a plugin ( compile it ?) that contains all libraries that it needs. Or it should be done in a different way?
1) Distribute any required DLLs together with the plugin DLL, and put them inside the plugin folder. Either add the plugin folder to the probing path for assemblies in the app.config, or add a handler for the AssemblyResolve event.
2) Use ILMerge to combine the plugin and required assemblies into one assembly.
If you load all plugins into the same app domain, this will cause funny issues if the same types are merged into multiple plugins.

Use Open CV in class library

I'm creating application using my own dll files in C#.
I would like to create Class Library which uses Open CV. I have to add reference to project - Emgu.CV.dll and Emgu.Util. It's ok but OpenCV needs also extra dll files for example "opencv_calib3d231.dll" and others. They have to be in Debug folder when I want to run some Projet using OpenCV.
But what if I want to use OpenCv in my own class library? Where do I have to put this extra dll files?
Any idea how to fix this?
You cannot execute a class library by itself: ultimately a class library will always (indirectly) be used by some executable program. You have to ensure that dependencies of your class library can be found by said executable, for example by putting them in the directory that executable is in.
So, if you have an executable project (Project A) and a class library (Project B) used by the executable project that has native dependencies like opencv_calib3d231.dll, it suffices to ensure the native dependencies are in Project A's directory at runtime.
If you are developing a class library that will be used by other developers in their programs, you should distribute the dependencies of your class library with the library (or at least provide instructions on how to obtain the dependencies in your documentation). They can then ensure the operating system can find the dependencies upon loading your class library.

How to embed a DLL within a C# compiled application

I made a c# dll.but if the user want to use it in c# winforms he/she must have dll file in the exe folder.how can i do something with dll project that it automatically add dll to exe file when used in other winforms app?
is it possible?sorry if my english is not good
tnx
there are many ways.
here is a full guide:
http://msdn.microsoft.com/en-us/library/yx7xezcf(v=vs.110).aspx
I love this way to do this in code personally:
How to add folder to assembly search path at runtime in .NET?
You can save the dll in the Global Assembly cache.
Then you can refer to it in the application without needing it to be in the same folder.
http://support.microsoft.com/kb/837908
I think what you are getting at is the programmer wants to use a DLL you have provided, but does not want the DLL to be inside the same folder as the EXE file when compiled.
try this:
Embedding DLLs in a compiled executable
Yoy can embed / include DLL files inside the exe file
The key words here is Embedded DLL

how to use dll of same name but different version from different locations in same application?

We are trying to program a couple of plugins for another application. We have two directories each with the code it needs to run independently whcih contain all the dll's (but not the exe as its 3rd party and we have no control over it). There is some shared code in a dll, placed in each plugin directory. When introducing a new version of the plugin we need to change this shared dll in one of the plugin directories but leave the other. When we do this the old version breaks as it relies on the older version of the shared dll but it only loads one. Is there a way to force it to use a certain version of the dll?
you should use the latebinding approach System.Reflection

Categories