Preparing your project

From WorkCDN

Jump to: navigation, search

Before your C++ code can call into C# (or .NET), there are some required settings you must make first. One option is to make the following options global to your project. I recommend against doing it that way as it will impact your build. Some of these options (turning off pre-compiled headers, for example) will slow down your compilation.

Instead, I prefer to set these options on a file-by-file basis as needed. Any file which will be calling into a C# DLL will need to have the following options set in its Properties dialog:

  • C/C++/General - Compile with common language runtime (/clr)
  • C/C++/General - Debug Information Format - Program Database (/Zi)
  • C/C++/Code Generation - Basic Runtime Checks - Default (others may work too)
  • C/C++/Code Generation - Enable Minimal Rebuild - No
  • C/C++/Code Generation - Enable C++ Exceptions - Yes With SEH Exceptions (/EHa)
  • C/C++/Precompiled Headers - Create/Use Precompiled Header - Not Using Precompiled Headers


Add the following near the top of your file (after #include's)

  • #pragma managed
  • using namespace System::Windows::Forms;
  • using namespace MyCSharpDialog;


...and any other namespaces you may want to add. This just makes coding easier so you won't always need to prepend your classes with the fully qualified name.


Right click on your project and select References. Add the following references.

  • mscorlib
  • System
  • System.Windows.Forms
  • Your special C# DLL - Your C# DLL allowing you to access your new C# dialogs.

Depending on your code, you may need to add more References. You will receive compilation errors if anything is missing.

Personal tools