How to resolve the problem with CLR dependencies using C# and C++/CLI dlls

Eugene Traty 0 Reputation points
2025-03-21T12:08:53.5866667+00:00

There are two .dll files, one written in C++ (providing an API via P/Invoke), and the other in C# (.NET 4.5.2), which exports it. There was a need to rewrite everything in C++/CLI. Everything was done, and if you run the resulting output through a test utility in the build output folder, it works perfectly. However, when copying to the server (also written in C#, .NET 4.8), an exception System.IO.FileNotFoundException: Could not load file or assembly referencing a CLR DLL occurs. This happens at the moment of binding C# delegates and static methods of the new CLR class in the C++ library (I tried doing it without delegates - the result is the same. I also tried creating a full class object and referencing its method in the delegate - the same result). Moreover, if you combine the old P/Invoke and the new CLR, the exception only occurs when accessing the new API, while P/Invoke calls link normally. Both libraries are in the same path on the server, in the same folder. In the reference settings for the C# library, I specified the path to the C++/CLI DLL in the build output directory - to no avail.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,916 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.