How to link C# and C++ assemblies into a single executable
- by swingkid
I have VS2008 solution containg a project that generates a C# executable that references a project that generates a dll containing both C++/CLI and unmanaged C++.
I would like to merge these into a single executable, as the C++ dll contains security code that I want to embed in the main executable.
I cannot use ILMerge, as the dll contains both managed and unmanaged code. The suggested solution seems to be to use link.exe to link the C# assembly with the C++ object files. This is what I am trying to do.
I manually edited the project file for the c# executable to generate a netmodule. I added a post build step to the executable project to run link.exe to link the c# netmodule and the compiled C++ object files together, then run mt.exe to merge the assembly manifests created by both projects. This runs successfully, but the exe still contains a reference to and uses the c++ types defined in the dll generated by the normal build process for the C++ project.
I then specified /NOASSEMBLY in the project settings for the C++ dll, so it also generates a netmodule. In the C# project, I removed the reference to the C++ project, but added a project dependancy in the solution. I manually edited the C# project file to include similar to:
<ItemGroup>
<AddModules Include="..\Debug\librarycode.netmodule" />
</ItemGroup>
i.e. to reference the C++ netmodule that is now generated by the C++ project.
However, now the linker step in my post build event fails with:
error LNK2027: unresolved module reference 'librarycode.netmodule'
fatal error LNK1311: 1 unresolved module references:
This is entirely understandable, as I am not linking in the librarycode netmodule; I am linking in the C++ object files used to generate the netmodule instead.
So in short, how do I merge a c# executable and C++ object files into a single assembly? What have I missed?
My source of reference so far (appart from the link.exe command link reference etc on MSDN) are the two following articles:
http://blogs.msdn.com/texblog/archive/2007/04/05/linking-native-c-into-c-applications.aspx
http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx
I have a demonstration solution that shows my workings so far, if this helps.
Thank you very much in advance.