English 中文(简体)
作为I Un known
原标题:managed-to-unmanaged Marshaling of object[] as IUnknown

I m using P/Invoke to call an unmanaged C function from C#, passing an array of objects. In the unmanaged code I query IUnknown for IDispatch. This works for the simple case, but getting IDispatch fails if one of the objects is an array itself.

管理代码:

    [DllImport("NativeDll.dll")]
    static extern void TakesAnObjectArray(int len, 
        [MarshalAs(UnmanagedType.LPArray, 
         ArraySubType = UnmanagedType.IUnknown)]object[] a);

    public static void exec1(int a, object b, string c)
    {
        Object[] info_array;
        Object[] parameters_array;

        parameters_array = new object[4];
        parameters_array[0] = a;
        parameters_array[1] = b;
        parameters_array[2] = c;
        parameters_array[3] = 55;
        // THIS WORKS GREAT
        TakesAnObjectArray(4, parameters_array);

        info_array = new object[6];
        info_array[0] = parameters_array;
        // THIS DOESN T
        // I CAN T GET IDISPATCH FOR THE 1ST  OBJECT 
        TakesAnObjectArray(6, info_array);
    }

un管理代码:

    void TakesAnObjectArray(int len, LPUNKNOWN p[])
    {
        HRESULT hr;
        for (int i=0; i<len; i++)
        {
            IDispatch *disp = NULL;
            hr = p[i]->QueryInterface(IID_IDispatch, (void**)&disp);
        }
    }

QueryInterface is successful most of the time. But, if the managed object is actually a System.Object[] , I can t get an IDispatch interface (hr = 0x80004002 = E_NOINTERFACE = No such interface supported ).

Can I use MarshalAs(...) in some way that will fix this? Or Is there another way to get this to work?

最佳回答

令人感兴趣的是,《保护移徙工人及其家庭成员权利公约》成功地使《公约》的第一部分工作得以完成。 由于该系统,它可能能够产生一个工地派遣点。 射线类型为[ComVisible(true)]。 因此,你可以找QI工作,但是,在不熟悉Array类成员的情况下,你实际上可以做什么。 你在第二波ni子上跑了,因为在宣言中,没有任何东西迫使塞军释放阵列元素。

您需要确定这一正确方式,但一个阵列并不是COM自动化中的发号物体。 缺席的Catmarshaling for Object[]是苏丹武装部队的一个安全阵列,其组成部分是VARIANT。 更改从LPUNKNOWN[]到FAFEARRAY* 的“Fen-Array”论点类型。 而且,将“pin”声明改为“单纯的物体”,而没有[《哈龙协定》]归属。

你们需要在我们的C++法典中使用安全发射器和安全发射器,以获取阵列内容。 第二种幻灯片需要一种额外的间接影响,因为第一个要素是包含苏丹武装部队本身的一个VARIANT。

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签