SafeArrayPutElement 方法抛出 System.AccessViolationException

发布于 2024-09-13 08:48:05 字数 1869 浏览 4 评论 0原文

我正在尝试将一个整数数组从 C# 传递到 C++/CLI。这是我的代码:

// 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 行抛出以下异常:

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

我有一种感觉,我使用 SafeArrayPutElement 方法做的事情基本上是错误的。你能发现错误吗?

(顺便说一句,我在 将一组接口从 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});
        }
    }
}

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 Passing an array of interfaces from C# to C++/CLI . I think the difference is big enough to warrant two separate questions.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2024-09-20 08:48:05
    safeArrayPointer = SafeArrayCreate(VT_UNKNOWN,1,arrayDim);

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

你可以看到事情进展如何。相反,创建一个整数数组。使固定:

    safeArrayPointer = SafeArrayCreate(VT_I4,1,arrayDim);
    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文