internal unsafe void ConvertToManaged(object pManagedHome, IntPtr pNativeHome)
        {
            switch (this.backPropAction)
            {
            case AsAnyMarshaler.BackPropAction.Array:
                MngdNativeArrayMarshaler.ConvertContentsToManaged(this.pvArrayMarshaler, ref pManagedHome, new IntPtr((void *)(&pNativeHome)));
                return;

            case AsAnyMarshaler.BackPropAction.Layout:
                StubHelpers.FmtClassUpdateCLRInternal(pManagedHome, (byte *)pNativeHome.ToPointer());
                return;

            case AsAnyMarshaler.BackPropAction.StringBuilderAnsi:
            {
                sbyte *newBuffer = (sbyte *)pNativeHome.ToPointer();
                ((StringBuilder)pManagedHome).ReplaceBufferAnsiInternal(newBuffer, Win32Native.lstrlenA(pNativeHome));
                return;
            }

            case AsAnyMarshaler.BackPropAction.StringBuilderUnicode:
            {
                char *newBuffer2 = (char *)pNativeHome.ToPointer();
                ((StringBuilder)pManagedHome).ReplaceBufferInternal(newBuffer2, Win32Native.lstrlenW(pNativeHome));
                return;
            }

            default:
                return;
            }
        }
Exemple #2
0
        internal unsafe void ConvertToManaged(object pManagedHome, IntPtr pNativeHome)
        {
            switch (backPropAction)
            {
            case BackPropAction.Array:
            {
                MngdNativeArrayMarshaler.ConvertContentsToManaged(
                    pvArrayMarshaler,
                    ref pManagedHome,
                    new IntPtr(&pNativeHome));
                break;
            }

            case BackPropAction.Layout:
            {
                StubHelpers.FmtClassUpdateCLRInternal(pManagedHome, (byte *)pNativeHome);
                break;
            }

            case BackPropAction.StringBuilderAnsi:
            {
                int length;
                if (pNativeHome == IntPtr.Zero)
                {
                    length = 0;
                }
                else
                {
                    length = string.strlen((byte *)pNativeHome);
                }

                ((StringBuilder)pManagedHome).ReplaceBufferAnsiInternal((sbyte *)pNativeHome, length);
                break;
            }

            case BackPropAction.StringBuilderUnicode:
            {
                int length;
                if (pNativeHome == IntPtr.Zero)
                {
                    length = 0;
                }
                else
                {
                    length = string.wcslen((char *)pNativeHome);
                }

                ((StringBuilder)pManagedHome).ReplaceBufferInternal((char *)pNativeHome, length);
                break;
            }

                // nothing to do for BackPropAction.None
            }
        }
Exemple #3
0
        private unsafe IntPtr ConvertArrayToNative(object pManagedHome, int dwFlags)
        {
            Type    elementType = pManagedHome.GetType().GetElementType() !;
            VarEnum vt;

            switch (Type.GetTypeCode(elementType))
            {
            case TypeCode.SByte: vt = VarEnum.VT_I1; break;

            case TypeCode.Byte: vt = VarEnum.VT_UI1; break;

            case TypeCode.Int16: vt = VarEnum.VT_I2; break;

            case TypeCode.UInt16: vt = VarEnum.VT_UI2; break;

            case TypeCode.Int32: vt = VarEnum.VT_I4; break;

            case TypeCode.UInt32: vt = VarEnum.VT_UI4; break;

            case TypeCode.Int64: vt = VarEnum.VT_I8; break;

            case TypeCode.UInt64: vt = VarEnum.VT_UI8; break;

            case TypeCode.Single: vt = VarEnum.VT_R4; break;

            case TypeCode.Double: vt = VarEnum.VT_R8; break;

            case TypeCode.Char: vt = (IsAnsi(dwFlags) ? (VarEnum)VTHACK_ANSICHAR : VarEnum.VT_UI2); break;

            case TypeCode.Boolean: vt = (VarEnum)VTHACK_WINBOOL; break;

            case TypeCode.Object:
            {
                if (elementType == typeof(IntPtr))
                {
                    vt = (IntPtr.Size == 4 ? VarEnum.VT_I4 : VarEnum.VT_I8);
                }
                else if (elementType == typeof(UIntPtr))
                {
                    vt = (IntPtr.Size == 4 ? VarEnum.VT_UI4 : VarEnum.VT_UI8);
                }
                else
                {
                    goto default;
                }
                break;
            }

            default:
                throw new ArgumentException(SR.Arg_NDirectBadObject);
            }

            // marshal the object as C-style array (UnmanagedType.LPArray)
            int dwArrayMarshalerFlags = (int)vt;

            if (IsBestFit(dwFlags))
            {
                dwArrayMarshalerFlags |= (1 << 16);
            }
            if (IsThrowOn(dwFlags))
            {
                dwArrayMarshalerFlags |= (1 << 24);
            }

            MngdNativeArrayMarshaler.CreateMarshaler(
                pvArrayMarshaler,
                IntPtr.Zero,      // not needed as we marshal primitive VTs only
                dwArrayMarshalerFlags,
                IntPtr.Zero);     // not needed as we marshal primitive VTs only

            IntPtr pNativeHome;
            IntPtr pNativeHomeAddr = new IntPtr(&pNativeHome);

            MngdNativeArrayMarshaler.ConvertSpaceToNative(
                pvArrayMarshaler,
                ref pManagedHome,
                pNativeHomeAddr);

            if (IsIn(dwFlags))
            {
                MngdNativeArrayMarshaler.ConvertContentsToNative(
                    pvArrayMarshaler,
                    ref pManagedHome,
                    pNativeHomeAddr);
            }
            if (IsOut(dwFlags))
            {
                backPropAction = BackPropAction.Array;
            }

            return(pNativeHome);
        }
        private unsafe IntPtr ConvertArrayToNative(object pManagedHome, int dwFlags)
        {
            Type    elementType = pManagedHome.GetType().GetElementType();
            VarEnum varEnum;

            switch (Type.GetTypeCode(elementType))
            {
            case TypeCode.Object:
                if (elementType == typeof(IntPtr))
                {
                    varEnum = ((IntPtr.Size == 4) ? VarEnum.VT_I4 : VarEnum.VT_I8);
                    goto IL_10D;
                }
                if (elementType == typeof(UIntPtr))
                {
                    varEnum = ((IntPtr.Size == 4) ? VarEnum.VT_UI4 : VarEnum.VT_UI8);
                    goto IL_10D;
                }
                break;

            case TypeCode.Boolean:
                varEnum = (VarEnum)254;
                goto IL_10D;

            case TypeCode.Char:
                varEnum = (AsAnyMarshaler.IsAnsi(dwFlags) ? ((VarEnum)253) : VarEnum.VT_UI2);
                goto IL_10D;

            case TypeCode.SByte:
                varEnum = VarEnum.VT_I1;
                goto IL_10D;

            case TypeCode.Byte:
                varEnum = VarEnum.VT_UI1;
                goto IL_10D;

            case TypeCode.Int16:
                varEnum = VarEnum.VT_I2;
                goto IL_10D;

            case TypeCode.UInt16:
                varEnum = VarEnum.VT_UI2;
                goto IL_10D;

            case TypeCode.Int32:
                varEnum = VarEnum.VT_I4;
                goto IL_10D;

            case TypeCode.UInt32:
                varEnum = VarEnum.VT_UI4;
                goto IL_10D;

            case TypeCode.Int64:
                varEnum = VarEnum.VT_I8;
                goto IL_10D;

            case TypeCode.UInt64:
                varEnum = VarEnum.VT_UI8;
                goto IL_10D;

            case TypeCode.Single:
                varEnum = VarEnum.VT_R4;
                goto IL_10D;

            case TypeCode.Double:
                varEnum = VarEnum.VT_R8;
                goto IL_10D;
            }
            throw new ArgumentException(Environment.GetResourceString("Arg_NDirectBadObject"));
IL_10D:
            int num = (int)varEnum;

            if (AsAnyMarshaler.IsBestFit(dwFlags))
            {
                num |= 65536;
            }
            if (AsAnyMarshaler.IsThrowOn(dwFlags))
            {
                num |= 16777216;
            }
            MngdNativeArrayMarshaler.CreateMarshaler(this.pvArrayMarshaler, IntPtr.Zero, num);
            IntPtr result;
            IntPtr pNativeHome = new IntPtr((void *)(&result));

            MngdNativeArrayMarshaler.ConvertSpaceToNative(this.pvArrayMarshaler, ref pManagedHome, pNativeHome);
            if (AsAnyMarshaler.IsIn(dwFlags))
            {
                MngdNativeArrayMarshaler.ConvertContentsToNative(this.pvArrayMarshaler, ref pManagedHome, pNativeHome);
            }
            if (AsAnyMarshaler.IsOut(dwFlags))
            {
                this.backPropAction = AsAnyMarshaler.BackPropAction.Array;
            }
            return(result);
        }
        private unsafe IntPtr ConvertArrayToNative(object pManagedHome, int dwFlags)
        {
            int     num;
            IntPtr  ptr;
            Type    elementType = pManagedHome.GetType().GetElementType();
            VarEnum enum2       = VarEnum.VT_EMPTY;

            switch (Type.GetTypeCode(elementType))
            {
            case TypeCode.Object:
                if (!(elementType == typeof(IntPtr)))
                {
                    if (elementType == typeof(UIntPtr))
                    {
                        enum2 = (IntPtr.Size == 4) ? VarEnum.VT_UI4 : VarEnum.VT_UI8;
                        goto Label_010D;
                    }
                    break;
                }
                enum2 = (IntPtr.Size == 4) ? VarEnum.VT_I4 : VarEnum.VT_I8;
                goto Label_010D;

            case TypeCode.Boolean:
                enum2 = (VarEnum)0xfe;
                goto Label_010D;

            case TypeCode.Char:
                enum2 = IsAnsi(dwFlags) ? ((VarEnum)0xfd) : VarEnum.VT_UI2;
                goto Label_010D;

            case TypeCode.SByte:
                enum2 = VarEnum.VT_I1;
                goto Label_010D;

            case TypeCode.Byte:
                enum2 = VarEnum.VT_UI1;
                goto Label_010D;

            case TypeCode.Int16:
                enum2 = VarEnum.VT_I2;
                goto Label_010D;

            case TypeCode.UInt16:
                enum2 = VarEnum.VT_UI2;
                goto Label_010D;

            case TypeCode.Int32:
                enum2 = VarEnum.VT_I4;
                goto Label_010D;

            case TypeCode.UInt32:
                enum2 = VarEnum.VT_UI4;
                goto Label_010D;

            case TypeCode.Int64:
                enum2 = VarEnum.VT_I8;
                goto Label_010D;

            case TypeCode.UInt64:
                enum2 = VarEnum.VT_UI8;
                goto Label_010D;

            case TypeCode.Single:
                enum2 = VarEnum.VT_R4;
                goto Label_010D;

            case TypeCode.Double:
                enum2 = VarEnum.VT_R8;
                goto Label_010D;
            }
            throw new ArgumentException(Environment.GetResourceString("Arg_NDirectBadObject"));
Label_010D:
            num = (int)enum2;
            if (IsBestFit(dwFlags))
            {
                num |= 0x10000;
            }
            if (IsThrowOn(dwFlags))
            {
                num |= 0x1000000;
            }
            MngdNativeArrayMarshaler.CreateMarshaler(this.pvArrayMarshaler, IntPtr.Zero, num);
            IntPtr pNativeHome = new IntPtr((void *)&ptr);

            MngdNativeArrayMarshaler.ConvertSpaceToNative(this.pvArrayMarshaler, ref pManagedHome, pNativeHome);
            if (IsIn(dwFlags))
            {
                MngdNativeArrayMarshaler.ConvertContentsToNative(this.pvArrayMarshaler, ref pManagedHome, pNativeHome);
            }
            if (IsOut(dwFlags))
            {
                this.backPropAction = BackPropAction.Array;
            }
            return(ptr);
        }