How to Add a Reference to an Unmanaged C++ Project Called by a C# Project

How do I add a reference to an unmanaged C++ project called by a C# project?

Visual Studio doesn't support referencing an unmanaged C++ project from a managed C# one, but MSBuild supports referencing any project from any other project.

You can manually add a reference to your project by editing the .csproj file by hand. In the file, find your existing set of ProjectReference elements (or add a new ItemGroup if you don't have one) and add the following reference:

<ProjectReference Include="..\mycpproject.csproj">
<Project>{b402782f-de0a-41fa-b364-60612a786fb2}</Project>
<Name>mycppproject</Name>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Content</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</ProjectReference>

When you perform the build, the reference will cause MSBuild to build the referenced project first. The ReferenceOutputAssembly value tells MSBuild not to copy the output assembly of the build (since the C++ project does not produce one), but the OutputItemType and CopyToOutputDirectory values instruct it to copy the output content to the referencing project's output folder.

You will be able to see the reference in Visual Studio, but you can't do much with it there.

This answer is based on a similar problem solved by Kirill Osenkov on his MSDN blog: https://blogs.msdn.microsoft.com/kirillosenkov/2015/04/04/how-to-have-a-project-reference-without-referencing-the-actual-binary/

Is there a way to reference a c++ project from a c# project in the same solution?

Well, the way I do it and have always done it is by obviously using Visual Studio, and assuming this C++ projects are VS projects you can easily create a VS Solution containing multiple projects that you can organize with "Solution Folders". The organization of your projects inside the solution is really up to how you want to organize it. It resembles a file system with nested folders. Needless to say that you can host projects in different languages such as C++, C#, VB...I'm not too sure if you can include a C project or not, that's out of my expertise.

See a screenshot below of a solution I created to demonstrate this...

Sample Image

Sample Image

Notice that "Business" has a nested solution folder (Utils) which contains a C++ project (ERM.CPPLibraries) and a VB project (ERM.VBLibraries). Then if you reference projects within the solution (Right click -> Add Reference), you will not need to copy the output assemblies everytime you compile your solution (or project(s)) VS is smart enough to resolve all dependencies, resolve them and update them.

Hope it gives you an idea

Edit based on comment

In simple words...No, it's not possible to reference a unmanaged project from a managed project in a VS solution. You can reference DLLs but not projects itself

Reference an unmanaged C++ DLL from Managed C++

You cannot add the unmanaged DLL as a reference to your managed C++ project. You can only do that with managed DLLs. What you do instead is link to the unmanaged DLL in the same way as you link to an unmanaged DLL in an unmanaged C++ project:

  • Use the header file for compilation.
  • Supply the unmanaged DLL's .lib file to the linker, for example by adding it to the list of Additional Dependencies list in the linker configuration pages.
  • Put the DLL in the same directory as the executable, so that it can be located by the loader.

Referencing an unmanaged C++ project within another unmanaged C++ project in Visual Studio 2008

Yes. You need to do two things:

  1. #include the respective header files, as you did
  2. Add a reference (Visual C++ supports two types, "dependencies" which are outdated and should not be used anymore, and "references" which are the correct ones). Use them to reference the other project, which must be a part of your solution. Meaning, in this case you must be able to COMPILE the other project.

Alternatively, if you do not have the source code, or you do not wish to compile the 3rd-party code for any other reason, you may also reference a compiled binary. The best way to do it is pragma comment lib. If this is what you need, please comment and I will edit my response.

Adding C++ DLL's to a C# project

You have to use P/Invoke to call unmanaged APIs from managed code.

Add managed DLL dependencied to unmanaged C++ project

Normal CLR search rules apply here. It first looks in the GAC and next looks in the directory in which the EXE is located. You can convince the COM runtime to locate A.dll from the registration, it can be stored anywhere the Regasm.exe /codebase option tells it to look. But that does not affect where the CLR looks for dependencies, it only considers the EXE location.

You can troubleshoot this by using the Fuslogvw.exe utility.

Alternatives are in general troublesome. As long as you have a [ComVisible] type in A.dll that's guaranteed to be instantiated first (think "Application") then you can subscribe the AppDomain.CurrentDomain.AssemblyResolve event in the constructor to help the CLR locate the other DLLs. But it is very important the constructor doesn't need types from B or C, you'll still crash when the jitter needs them to compile the constructor.

If that's not a suitable option then writing an appname.exe.config file can be somewhat useful if you prefer deploying the DLLs in a subdirectory of the EXE install directory. This is however rarely a good idea in a COM scenario since you are typically not in control over the EXE, it is usually somebody else's responsibility. Deploying locally is fine when you test your code. For production deployment you ought to seriously consider the GAC. In general a good idea in COM anyway since registration is machine-global which gives it strong DLL Hell headaches.



Related Topics



Leave a reply



Submit