How to pass SAFEARRAY of UDTs to unmaged code from C#.
        Posted  
        
            by themilan
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by themilan
        
        
        
        Published on 2010-04-18T18:56:18Z
        Indexed on 
            2010/04/18
            19:03 UTC
        
        
        Read the original article
        Hit count: 619
        
I also used VT_RECORD. But didn't got success in passing safearray of UDTs.
        [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.
I also able to cast elements of obtained SAFEARRAY at unmanaged side to GUID type. But If i tried to pass my UDT class MY_CLASS to unmanaged code with SAFEARRAY then it fails on managed code. (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."
Plz help me in such a situation to pass SAFEARRAY of UDTs to unmaged code.
© Stack Overflow or respective owner