views:

68

answers:

1

I am trying to pass an array of ints from C# to C++/CLI. Here's my code:

// SafeArrayTesting_PlusPlus.cpp  
#include "stdafx.h"  
#include <comdef.h>  
using namespace System;  
namespace SafeArrayTesting_PlusPlus  
{   
    public ref class MyCppClass  
    {       
       public:  
         MyCppClass();  
         ~MyCppClass();  
         void SetMyInts(array<int>^ myInts);  
     };  

     MyCppClass::MyCppClass(){}  
    MyCppClass::~MyCppClass(){}  

    void MyCppClass::SetMyInts(array<int>^ myInts)  
    {  
        // Create safearray
        SAFEARRAY  *safeArrayPointer;
        SAFEARRAYBOUND arrayDim[1];    // one dimensional array
        arrayDim[0].lLbound= 0;
        arrayDim[0].cElements= myInts->Length;
        safeArrayPointer = SafeArrayCreate(VT_UNKNOWN,1,arrayDim);

        // copy ints to safearray
        for (long lo= 0; lo < myInts->Length; lo++)
        {           
            cli::pin_ptr<int> pinnedIntPointer = &(myInts[lo]);
            SafeArrayPutElement(  
                safeArrayPointer,  
                &lo,  
                static_cast<void*> (pinnedIntPointer)); // line XX
        }

        // do something with the safearray here
    }
}   

// SafeArrayTesting_Main.cs
using SafeArrayTesting_PlusPlus;

namespace SafeArrayTesting_Main
{
    class SafeArrayTesting_Main
    {
        static void Main()
        {
            var myCppClass = new MyCppClass();
            myCppClass.SetMyInts(new[]{42});
        }
    }
}

When I run this, line XX throws the following exception:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I have a feeling that I'm doing something basically wrong with the SafeArrayPutElement method. Can you spot the error?

(By the way, I have asked a more complex variation of this question at http://stackoverflow.com/questions/3464901/ . I think the difference is big enough to warrant two separate questions.)

+1  A: 
    safeArrayPointer = SafeArrayCreate(VT_UNKNOWN,1,arrayDim);

That creates an array of IUnknown interface pointers. SafeArrayPut() makes sure to increase the reference count of the interface pointer by calling IUnknown::AddRef() since it stores a copy of the pointer in the array.

You can see how that goes kaboom. Create an array of integers instead. Fix:

    safeArrayPointer = SafeArrayCreate(VT_I4,1,arrayDim);
Hans Passant
Thanks a lot, Mr. Passant! I'm making this the accepted answer.