How to call a C++ delegated function from C#, when C++ call C# functions?
<p>I would like to call a C# library from C++.</p>

<p>The C# library, request a delegated function, 
in order to report the results.</p>

<p>Maybe my purpose is confusing: the conception is, C++ call C# function, and the C# function would call a callback function from C++.</p>

<p>I have been blocked in the C# call C++ callback function , the COM Interop is arcane for me.</p>

<p>My example code be :</p>

<p>C# code :</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CSharpLibraryNameSpace
{
    // Interface declaration.
    public delegate int NativeDelegateType(int x);
    //public delegate int NativeDelegateType([In, MarshalAs(UnmanagedType.LPStr)] string arg1);

    public interface ManagedInterface
    {
        int Add(int Number1, int Number2);

        int CalltheCallbackFun(NativeDelegateType callbackFun);
    };


    // Interface implementation.
    public class ManagedCSharpClass : ManagedInterface
    {
        public int Add(int Number1, int Number2)
        {
            Console.Write("Add\n");
            return Number1 + Number2;
        }

        public int 
            CalltheCallbackFun(
            /*[MarshalAs(UnmanagedType.FunctionPtr)]*/ NativeDelegateType callbackFun)
        {
            Console.Write("BB\n");
            string str;
            str = "AAA";
           unsafe
           {
               fixed (char* p = str)
               {
                   Console.Write("before call callbackFun\n");

                   callbackFun(0x01);
               }
           }
            return 0;
        }
    }
}
</code></pre>

<p>The C++ code :</p>

<pre><code>#include &lt;windows.h&gt;
// Import the type library.

#import "CSharpLibrary.tlb" raw_interfaces_only
using namespace CSharpLibrary;

 typedef void (__stdcall * C_Callback)(int);

 __declspec(dllexport) int __stdcall theCallback(void)
{
    return 0;
}/*theCallback*/

 class CPPcallback :public _NativeDelegateType
 {
 public:
     CPPcallback(){};
            virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount( 
            /* [out] */ UINT *pctinfo)
        {
            return E_NOTIMPL;
        }

        virtual HRESULT STDMETHODCALLTYPE GetTypeInfo( 
            /* [in] */ UINT iTInfo,
            /* [in] */ LCID lcid,
            /* [out] */ ITypeInfo **ppTInfo)
        {

             if (ppTInfo == NULL)
                return E_INVALIDARG;
            *ppTInfo = NULL;

            if(iTInfo != 0)
                return DISP_E_BADINDEX;

            AddRef();      // AddRef and return pointer to cached
                               // typeinfo for this object.
            *ppTInfo = NULL;

            return NOERROR;
        }

        virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( 
        /* [in] */ REFIID riid,
        /* [size_is][in] */ LPOLESTR *rgszNames,
        /* [in] */ UINT cNames,
        /* [in] */ LCID lcid,
        /* [size_is][out] */ DISPID *rgDispId)
        {
            return E_NOTIMPL;
        }

        virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke( 
        /* [in] */ DISPID dispIdMember,
        /* [in] */ REFIID riid,
        /* [in] */ LCID lcid,
        /* [in] */ WORD wFlags,
        /* [out][in] */ DISPPARAMS *pDispParams,
        /* [out] */ VARIANT *pVarResult,
        /* [out] */ EXCEPINFO *pExcepInfo,
        /* [out] */ UINT *puArgErr)
        {
            return 0;
        }

        virtual HRESULT STDMETHODCALLTYPE QueryInterface(/* [in] */ REFIID riid,
                /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
        {
            if (riid == IID_IUnknown) 
            {
                *ppvObject = static_cast&lt;IUnknown*&gt;(this); 
                AddRef();
                return S_OK;
            }

            if (riid == IID_IDispatch) {
                *ppvObject = static_cast&lt;IDispatch*&gt;(this); 
                AddRef();
                return S_OK;
            }

            //if (riid == IID_CPPcallback ) 
            {
                *ppvObject = static_cast&lt;CPPcallback*&gt;(this);
                AddRef();
                return S_OK;
            }

            *ppvObject = NULL;
            return E_NOINTERFACE;
        }

        virtual ULONG STDMETHODCALLTYPE AddRef( void)
        {
            return InterlockedIncrement(&amp;_refCount);
        }

        virtual ULONG STDMETHODCALLTYPE Release( void)
        {
            return  InterlockedDecrement(&amp;_refCount);
        }

 private:
        long _refCount;
 };

int main(int argc, char *argv[])
{
    // Initialize COM.
    HRESULT hr = CoInitialize(NULL);

    // Create the interface pointer.
    ManagedInterfacePtr CSharpDLLPtr(__uuidof(ManagedCSharpClass));

    long lResult = 0;

    // Call the Add method.
    CSharpDLLPtr-&gt;Add(5, 10, &amp;lResult);
    long aa;
    aa = 3;
    CPPcallback cppcallback;


    CSharpDLLPtr-&gt;CalltheCallbackFun(&amp;cppcallback, &amp;aa);
    wprintf(L"The result is %d\n", lResult);


    // Uninitialize COM.
    CoUninitialize();
    return 0;
}
</code></pre>

<p>The C++ calls C# function works. But the line 
     CSharpDLLPtr->CalltheCallbackFun(&amp;cppcallback, &amp;aa);</p>

<p>would go into the function QueryInterface,  GetTypeInfo then back to C++ main function directly. That is The line in C# </p>

<pre><code>  Console.Write("before call callbackFun\n");
</code></pre>

<p>Would not be reached.</p>

<p>How should I do to register a C++ callback function in C#?</p>