Share via


Using C# in C++ project with Visual Studio 2005

Today when I have been cleaning folders on my hard drive, I have found yet another demo project. I have built it sometime ago to demonstrate how how one can have C# and C++ project in one solution and link them both into one binary using C++ linker. It looks like it had never found its way to my blog and I have decided to fix this by posting it today.

For this demo, I have generated WCF service server and client in C# and then build C# code into a .netmodule and linked it together with C++ code into one binary. Same time I build C# as another client just for testing purpose. Most if not all code is generated from IDE using standard wizards.

To achieve this I had to do the following in addition to generating all C# code:

1) I created a Pre-Build event using information from Build.log for C# project to link C# code into a .net module

2) I changed linker settings for C++ code.

a. I have added path to C# .netmodule to Add Module Assembly (/ASSEMBLYMODULE)

b. I had to change to use LTCG in Debug mode also

c. and Additional Includes settings in command line

3) Added #using<> in my cpp code to reference C# .net module

4) Changed Additional include settings such that compiler can find .netmodule when compiling C++ code.

 

That should be all one has to do to repeat this task. When I worked on the sample,

I was primarily interested in demonstrating that using similar approach one can keep most of application native and rebuild one component as managed (/clr) in order to start using C# code and VS IDE wizards for generating WCF service clients. Perhaps someone else may find this sample interesting. I am attaching a zip file with source code to the post.

Linking CSharp and CPP in one binary.zip

Comments

  • Anonymous
    July 24, 2007
    Today when I have been cleaning folders on my hard drive, I have found yet another demo project. I have

  • Anonymous
    July 31, 2007
    Interesting example. Since it is the mix of C# and C++, how can we debug into the C# code?