locked
Mashaling array pointer

    Question

  • I am trying to make a managed C++ class to call a C# callback function.  I am making the following code as simple as possible to explain this:

    in C++

    public delegate void CallbackFoo(IntPtr pdata, int iSize);
    
    //Method Declaration
    void Foo(CallbackFoo  ^callbackFoo)
    
    //Method Definition
    void Foo(CallbackFoo  ^callbackFoo)
    {
        byte* pdata = new byte[100];
        callbackFoo(pdata, 100);
    }

    In C#

    void SomeFunc()
    {
        mycppClass = new MyCppClass();
        mycppClass.Foo(Callback);
    }
    
    public unsafe void Callback(IntPtr pData, int iSize)
    {
        Debug.WriteLine("Success:" + iSize);
    }

    The app builds and runs, callbackFoo(pdata, 100) generates the following exception:

    First-chance exception at 0x770E210B in WinIPCamera.WindowsPhone.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x03AAEF38. HRESULT:0x80131535 The text associated with this error code could not be found.
    
    WinRT information: System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #1': Windows Runtime marshaler does not support the type.
    Apparently the marshaler does not like IntPtr.  How can the array be passed to the callback method?


    Hong



    Monday, March 23, 2015 4:47 PM

Answers

All replies