开发者

efficiently calling unmanaged method taking unmanaged objects as parameters from managed code

开发者 https://www.devze.com 2023-02-24 06:41 出处:网络
I have the following scenario. The managed code will initialize lots of object of a class which is a wrapper around an unmanaged struct. There are two approaches that I can do for this. One is to have

I have the following scenario. The managed code will initialize lots of object of a class which is a wrapper around an unmanaged struct. There are two approaches that I can do for this. One is to have a managed class wrapper that just has a pointer to the unmanaged object. The other is to have a full fledged managed class and create the unmanaged object when required to call into unmanaged methods. I have provided both the methods below. I was told that if I use the approach 1(having a pointer to unmanged object), the GC will have lots of issue knowing about the unmanaged portion and it is better to do approach 2. Does someone tell me which is better or if there is some other approach that is even better. My concern with Approach 2 is that there are copying to and fro everytime a unmanaged method is called. I am not sure if the GC issue outweighs it.

EDIT- the first approach has a ref class and the second has a value class. The reason the second is value is so that it can be added to lists more efficiently

In unmanaged:

struct A_UNMANAGED
{
    int a;
    int b[20];
};

void GetData(A_UNMANAGED& a); // populates A

In managed (First Approach)

public ref class A_MANAGED
{
    A_UNMANGED* ap;

public:
    property System::UInt32 a
   开发者_JS百科 {
        System::UInt32 get() { return ap->a; }
        void set(System::UInt32 value) { ap->a = value; }
    }

    property array<System::UInt32>^ b
    {
        array<System::UInt32>^ get() { return ap->b; }
        void set(array<System::UInt32>^ value) { b = value; } // assume this copy works
    }

internal:
    void GetData()
    {
        GetData(ap);
    }
};

In managed (Second Approach) (EDIT: updated to ref. Assume all the garbage collection and pointer creation is written correctly)

public value class A_MANAGED
{
    System::UInt32 a;
    array<System::UInt32>^ b;

public:
    property System::UInt32 a
    {
        System::UInt32 get() { return a; }
        void set(System::UInt32 value) { a = value; }
    }

    property array<System::UInt32>^ b
    {
        array<System::UInt32>^ get() { return b; }
        void set(array<System::UInt32>^ value) { b = value; }
    }

internal:
    void GetUnmanaged(A_UNMANAGED& obj1)
    {
        obj1.a = a; 
        pin_ptr<System::UInt32> bp = &b[0];
        memcpy(obj1.b, bp, 20);
    }

    void GetData()
    {
        A_UNMANAGED obj2;
        GetUnmanaged(obj2);
        GetData(obj2);
        // copy from obj2 to member variables
    }
};


No, the 1st snippet it the canonical way. The garbage collector only moves the pointer, it doesn't move the pointed-to object. That one should have been allocated with malloc() or the new operator, it cannot be moved.

There are otherwise several serious problems in your code. You don't seem to allocate the memory for A_UNMANAGED unless GetData() takes its argument by reference. GetData() is never called. This must normally be a ref class (not ref value) so you can provide a destructor and a finalizer to release the memory. The b property setter will bomb your program with a StackOverflowException. Be sure to study the language before tackling this project.

Check this answer for sample code.


As Hans said, the first way is the usual approach (though personally, I think P/Invoke would be more succinct in this particular case...). However, your A_MANAGED::b implementation will not work, which would be obvious if one were to try simply compiling it. Try this instead:

public ref class A_MANAGED
{
    A_UNMANAGED* ap;

public:
    A_MANAGED() : ap(new A_UNMANAGED() ) { }
    ~A_MANAGED() { this->!A_MANAGED(); }
    !A_MANAGED() { delete ap; ap = nullptr; }

    property int a
    {
        int get() { return ap->a; }
        void set(int value) { ap->a = value; }
    }

    property array<int>^ b
    {
        array<int>^ get()
        {
            using System::Runtime::InteropServices::Marshal;
            array<int>^ arr = gcnew array<int>(20);
            Marshal::Copy(System::IntPtr(ap->b), arr, 0, 20);
            return arr;
        }
        void set(array<int>^ value)
        {
            using System::Runtime::InteropServices::Marshal;
            Marshal::Copy(value, 0, System::IntPtr(ap->b), 20);
        }
    }

internal:
    void GetData()
    {
        ::GetData(*ap);
    }
};

And then there's the usual caveat about returning arrays from properties: it's a bad idea. Unless you really want to maintain parity with the unmanaged class' public interface, b should really be a pair of set/get functions rather than a property.

0

精彩评论

暂无评论...
验证码 换一张
取 消