static void MarshalValue(object value, out int size, out IntPtr data)
 {
     if (value is string)
     {
         data = Marshal.StringToBSTR(value as string);
         size = 0;
     }
     else if (value is char)
     {
         BlittableChar c = (BlittableChar)((char)value);
         size = Marshal.SizeOf(c);
         data = Marshal.AllocCoTaskMem(size);
         Marshal.StructureToPtr(c, data, false);
     }
     else if (value is bool)
     {
         BlittableBoolean b = (BlittableBoolean)((bool)value);
         size = Marshal.SizeOf(b);
         data = Marshal.AllocCoTaskMem(size);
         Marshal.StructureToPtr(b, data, false);
     }
     else
     {
         size = Marshal.SizeOf(value);
         data = Marshal.AllocCoTaskMem(size);
         Marshal.StructureToPtr(value, data, false);
     }
 }
        public void CharTest()
        {
            const char    value     = 'x';
            BlittableChar blittable = (BlittableChar)value;

            Assert.Equal(value, blittable);
        }
Exemple #3
0
        public void CharTest()
        {
            const char value     = 'x';
            var        blittable = new BlittableChar(value);

            Assert.Equal(value, blittable);
        }
            private bool UnmarshalResult(int dataTypeId, IntPtr dataPtr, out object result)
            {
                if (dataTypeId < 0)
                {
                    result = new ContextVariable(m_opaque, dataPtr);
                    return(true);
                }
                if (dataTypeId == 0) // special case for null object
                {
                    result = null;
                    return(true);
                }
                if (dataTypeId >= basicTypes.Length)
                {
                    result = null;
                    return(false);
                }
                Type dataType = Type.GetType(basicTypes[dataTypeId]);

                if (dataType == typeof(string))
                {
                    if (dataPtr == IntPtr.Zero)
                    {
                        result = string.Empty;
                        return(true);
                    }
                    result = Marshal.PtrToStringBSTR(dataPtr);
                    Marshal.FreeBSTR(dataPtr);
                    return(true);
                }
                if (dataType == typeof(char))
                {
                    BlittableChar c = Marshal.PtrToStructure <BlittableChar>(dataPtr);
                    Marshal.FreeCoTaskMem(dataPtr);
                    result = (char)c;
                    return(true);
                }
                if (dataType == typeof(bool))
                {
                    BlittableBoolean b = Marshal.PtrToStructure <BlittableBoolean>(dataPtr);
                    Marshal.FreeCoTaskMem(dataPtr);
                    result = (bool)b;
                    return(true);
                }
                result = Marshal.PtrToStructure(dataPtr, dataType);
                Marshal.FreeCoTaskMem(dataPtr);
                return(true);
            }