SafeArrayPutElement方法抛出System.AccessViolationException [英] SafeArrayPutElement method throws System.AccessViolationException

查看:106
本文介绍了SafeArrayPutElement方法抛出System.AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个整数数组从C#传递给C ++ / CLI。这是我的代码:

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});
        }
    }
}

运行此命令时, XX引发以下异常:

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


System.AccessViolationException:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

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

我觉得我在<$上做错了c $ c> SafeArrayPutElement 方法。您能发现错误吗?

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

(顺便说一句,我在从C#传递介面至C ++ / CLI 。我认为两者之间的差异足以应付两个独立的问题。)

(By the way, I have asked a more complex variation of this question at Passing an array of interfaces from C# to C++/CLI . I think the difference is big enough to warrant two separate questions.)

推荐答案

    safeArrayPointer = SafeArrayCreate(VT_UNKNOWN,1,arrayDim);

这将创建一组IUnknown接口指针。 SafeArrayPut()确保通过调用IUnknown :: AddRef()来增加接口指针的引用计数,因为它在数组中存储了指针的副本。

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.

您可以看到kaboom的情况。而是创建一个整数数组。修复:

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

    safeArrayPointer = SafeArrayCreate(VT_I4,1,arrayDim);

这篇关于SafeArrayPutElement方法抛出System.AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆