简体   繁体   English

如何将UDF的SAFEARRAY传递给来自C#的未损坏代码

[英]How to pass SAFEARRAY of UDTs to unmaged code from C#

I also used VT_RECORD. 我也用过VT_RECORD。 But didn't got success in passing safearray of UDTs. 但是没有成功通过UDT的安全阵列。

        [ComVisible(true)]
        [StructLayout(LayoutKind.Sequential)]
        public class MY_CLASS
        {
            [MarshalAs(UnmanagedType.U4)]
            public Int32 width;
            [MarshalAs(UnmanagedType.U4)]
            public Int32 height;
        };

    [DllImport("mydll.dll")]
    public static extern Int32 GetTypes(
        [In, Out][MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_RECORD, SafeArrayUserDefinedSubType = typeof(MY_CLASS))]MY_CLASS[] myClass,
        [In, Out][MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_RECORD, SafeArrayUserDefinedSubType = typeof(Guid))]Guid[] guids
        );

If i communicate with my unmanaged code without 1st parameter, Then there is no error in passing "guids" parameter to unmanaged code. 如果我在没有1st参数的情况下与我的非托管代码进行通信,那么将“guids”参数传递给非托管代码就没有错误。

I also able to cast elements of obtained SAFEARRAY at unmanaged side to GUID type. 我还能够在非托管端将获得的SAFEARRAY元素转换为GUID类型。 But If i tried to pass my UDT class MY_CLASS to unmanaged code with SAFEARRAY then it fails on managed code. 但是如果我试图通过SAFEARRAY将我的UDT类MY_CLASS传递给非托管代码,那么它在托管代码上失败了。 (as above code snippet) (如上面的代码片段)

It shows exception "An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in myapp.exe" "Additional information: Specified array was not of the expected type." 它显示异常“myapp.exe中出现类型'System.Runtime.InteropServices.SafeArrayTypeMismatchException'的未处理异常”“附加信息:指定的数组不是预期的类型。”

Plz help me in such a situation to pass SAFEARRAY of UDTs to unmaged code. Plz帮助我在这种情况下将SAFEARRAY的UDT传递给了未损坏的代码。

I got one solution to this problem. 我有一个解决这个问题的方法。


I tried an alternative to this problem. 我尝试了替代这个问题。 I passed UDT having SAFEARRAYs as its members to unmanaged code. 我通过UDT将SAFEARRAY作为其成员使用非托管代码。

Here is managed code that I followed, 这是我遵循的托管代码,

    [ComVisible(true)]
    [StructLayout(LayoutKind.Sequential)]
    public class MY_CLASS
    {
        [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.I4)]
        public Int32[] width;
        [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.I4)]
        public Int32[] height;
    };

    [DllImport("mydll.dll")]
    public static extern Int32 GetTypes(
        [In, Out] MY_CLASS myClass
        );

and on unmanaged side, 在非管理方面,

    typedef struct _MY_STRUCT
    {
        SAFEARRAY * pWidths;
        SAFEARRAY * pHeights;
    }MY_STRUCT;

    HRESULT GetTypes(MY_STRUCT * pMyStruct)
    {
        // Here I can use pMyStruct->pWidths or pMyStruct->pHeights
        //      paramater as safearray of int32 type.
        // I can modify it's element and it will be visible
        //      on managed side.

        return S_OK;
    }

I uses this type of mechanism to pass UDT having array of Value-Types instead of passing array of UDTs. 我使用这种类型的机制来传递具有Value-Types数组的UDT,而不是传递UDT数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM