How to call C# dll from C++ RRS feed

  • Question

  • Hi All,

    How to call a c# dll from C++ code.

    Please share any sample code or any link related to this which have code.

    Thanks in advance.



    Friday, February 22, 2013 12:15 PM


All replies

  • Hi Agarwal.ashish,

    1) Use CCW (Com Callable Wraper). To mark your managed(c#) assembly with "Register for COM interop"  please follow below steps

    Step 1. With a project selected in Solution Explorer, on the Project menu, click Properties.
    Step 2. Click the Compile tab in Visual Basic. Click the Build tab in C#.
    Step 3. Select the Register for COM interop check box.

    else you can visit

          a) http://msdn.microsoft.com/en-us/library/ms404285.aspx

    For more details on "How to call a managed DLL from native Visual C++ code

    For example see here


    2) Use Reverse P/Invoke as shown below


    when you use Reverse P/Invoke be careful for marshalling of parameters.

    may be it helps you.
    If anything is unclear feel free to ask me... :)


    ENjoy ThE WorLD Of COdE

    • Edited by Nans11 Friday, February 22, 2013 1:02 PM
    • Proposed as answer by Vic Vega Friday, February 22, 2013 3:02 PM
    • Marked as answer by agrawal.ashish Friday, February 22, 2013 4:38 PM
    Friday, February 22, 2013 12:47 PM
  • When oyu say C++ do you mean Native Code (machine code) or C++ that creates .NET applications? (compiles to MSIL, not machien code).

    For the first, Nans11 seems to know the way. What you want to do is "using a .NET/CLR Library in C++ native code".

    Forthe later: Just like you would use it in C# - Add a reference, use the namespace.

    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Friday, February 22, 2013 2:58 PM
  • I'm having trouble with the C++ part of the CodeProject article. With VS 6.0, the C++ code won't compile (Win 7), so I decided to go with VS 2010. With VS 2010, the C++ code has a compilation error:

    ITest* pTest = NULL;
    Error 1 error C2065: 'ITest' : undeclared

    I imported the .tlb file, and included the .tli file, which appears to have the declaration of ITest. So I'm baffled as to why the compiler does not know what ITest is. I was hoping a C++ guru could offer suggestions, or perhaps post something on the CodeProject article (from 2006), as the author provided just a snippet of C++ code, not the entire listing.

    Thanks in advance.

    Friday, August 9, 2013 4:39 AM
  • Im able to invoke C# dll from my C++ exe but everything works in my machine, whereas in non developer machine (no visual studio) throws exception at CoInitialize(NULL) let me know where i going wrong.



    Thursday, June 26, 2014 9:54 AM
  • Hello,

      Compile your C++ code with the /clr flag. With that, you can call into any .NET code with relative ease..

     if the reply help you mark it as your answer.
     Free No OLE C# Word PDF, ExcelPowerPoint Component(Create, Modify, Convert & Print) 
    Thursday, June 26, 2014 10:27 AM