Esempio n. 1
0
        private object this[uint property, Type type]
        {
            get
            {
                uint          dataType;
                StringBuilder stringValue = new StringBuilder("");
                uint          bufSize     = 0;
                int           intValue;
                long          timeValue = 0;

                uint ret = RemotableNativeMethods.MsiSummaryInfoGetProperty(
                    (int)this.Handle,
                    property,
                    out dataType,
                    out intValue,
                    ref timeValue,
                    stringValue,
                    ref bufSize);
                if (ret != 0 && dataType != (uint)VarEnum.VT_LPSTR)
                {
                    throw InstallerException.ExceptionFromReturnCode(ret);
                }

                switch ((VarEnum)dataType)
                {
                case VarEnum.VT_EMPTY:
                {
                    if (type == typeof(DateTime))
                    {
                        return(DateTime.MinValue);
                    }
                    else if (type == typeof(string))
                    {
                        return(String.Empty);
                    }
                    else if (type == typeof(short))
                    {
                        return((short)0);
                    }
                    else
                    {
                        return((int)0);
                    }
                }

                case VarEnum.VT_LPSTR:
                {
                    if (ret == (uint)NativeMethods.Error.MORE_DATA)
                    {
                        stringValue.Capacity = (int)++bufSize;
                        ret = RemotableNativeMethods.MsiSummaryInfoGetProperty(
                            (int)this.Handle,
                            property,
                            out dataType,
                            out intValue,
                            ref timeValue,
                            stringValue,
                            ref bufSize);
                    }
                    if (ret != 0)
                    {
                        throw InstallerException.ExceptionFromReturnCode(ret);
                    }
                    return(stringValue.ToString());
                }

                case VarEnum.VT_I2:
                case VarEnum.VT_I4:
                {
                    if (type == typeof(string))
                    {
                        return(intValue.ToString(CultureInfo.InvariantCulture));
                    }
                    else if (type == typeof(short))
                    {
                        return((short)intValue);
                    }
                    else
                    {
                        return(intValue);
                    }
                }

                case VarEnum.VT_FILETIME:
                {
                    if (type == typeof(string))
                    {
                        return(DateTime.FromFileTime(timeValue).ToString(CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        return(DateTime.FromFileTime(timeValue));
                    }
                }

                default:
                {
                    throw new InstallerException();
                }
                }
            }

            set
            {
                uint   dataType    = (uint)VarEnum.VT_NULL;
                string stringValue = "";
                int    intValue    = 0;
                long   timeValue   = 0;

                if (type == typeof(short))
                {
                    dataType = (uint)VarEnum.VT_I2;
                    intValue = (int)(short)value;   // Double cast because value is a *boxed* short.
                }
                else if (type == typeof(int))
                {
                    dataType = (uint)VarEnum.VT_I4;
                    intValue = (int)value;
                }
                else if (type == typeof(string))
                {
                    dataType    = (uint)VarEnum.VT_LPSTR;
                    stringValue = (string)value;
                }
                else // (type == typeof(DateTime))
                {
                    dataType  = (uint)VarEnum.VT_FILETIME;
                    timeValue = ((DateTime)value).ToFileTime();
                }

                uint ret = NativeMethods.MsiSummaryInfoSetProperty(
                    (int)this.Handle,
                    property,
                    dataType,
                    intValue,
                    ref timeValue,
                    stringValue);
                if (ret != 0)
                {
                    throw InstallerException.ExceptionFromReturnCode(ret);
                }
            }
        }