Exemplo n.º 1
0
        internal int GetPropertyValue(object component, int dispid, object[] retval)
        {
            if (!(component is UnsafeNativeMethods.IDispatch))
            {
                return(NativeMethods.E_NOINTERFACE);
            }
            UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component;
            try
            {
                Guid g = Guid.Empty;
                NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO();
                int hr;

                try
                {
                    hr = iDispatch.Invoke(dispid,
                                          ref g,
                                          SafeNativeMethods.GetThreadLCID(),
                                          NativeMethods.DISPATCH_PROPERTYGET,
                                          new NativeMethods.tagDISPPARAMS(),
                                          retval,
                                          pExcepInfo, null);

                    /*if (hr != NativeMethods.S_OK){
                     * Com2PropertyDescriptor.PrintExceptionInfo(pExcepInfo);
                     *
                     * } */
                    if (hr == NativeMethods.DISP_E_EXCEPTION)
                    {
                        hr = pExcepInfo.scode;
                    }
                }
                catch (ExternalException ex)
                {
                    hr = ex.ErrorCode;
                }
                return(hr);
            }
            catch
            {
                //Debug.Fail(e.ToString() + " " + component.GetType().GUID.ToString() + " " + component.ToString());
            }
            return(NativeMethods.E_FAIL);
        }
Exemplo n.º 2
0
        internal unsafe HRESULT GetPropertyValue(object component, Ole32.DispatchID dispid, object[] retval)
        {
            if (!(component is UnsafeNativeMethods.IDispatch))
            {
                return(HRESULT.E_NOINTERFACE);
            }

            UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component;
            try
            {
                Guid g = Guid.Empty;
                NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO();
                var dispParams = new Ole32.DISPPARAMS();
                try
                {
                    HRESULT hr = iDispatch.Invoke(
                        dispid,
                        &g,
                        Kernel32.GetThreadLocale(),
                        NativeMethods.DISPATCH_PROPERTYGET,
                        &dispParams,
                        retval,
                        pExcepInfo,
                        null);
                    if (hr == HRESULT.DISP_E_EXCEPTION)
                    {
                        return((HRESULT)pExcepInfo.scode);
                    }

                    return(hr);
                }
                catch (ExternalException ex)
                {
                    return((HRESULT)ex.ErrorCode);
                }
            }
            catch
            {
            }

            return(HRESULT.E_FAIL);
        }
Exemplo n.º 3
0
            /// <include file='doc\COM2AboutBoxPropertyDescriptor.uex' path='docs/doc[@for="Com2AboutBoxPropertyDescriptor.AboutBoxUITypeEditor.EditValue"]/*' />
            /// <devdoc>
            ///      Edits the given object value using the editor style provided by
            ///      GetEditorStyle.  A service provider is provided so that any
            ///      required editing services can be obtained.
            /// </devdoc>
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                object component = context.Instance;

                if (Marshal.IsComObject(component) && component is UnsafeNativeMethods.IDispatch)
                {
                    UnsafeNativeMethods.IDispatch pDisp      = (UnsafeNativeMethods.IDispatch)component;
                    NativeMethods.tagEXCEPINFO    pExcepInfo = new NativeMethods.tagEXCEPINFO();
                    Guid g = Guid.Empty;

                    int hr = pDisp.Invoke(NativeMethods.ActiveX.DISPID_ABOUTBOX,
                                          ref g,
                                          SafeNativeMethods.GetThreadLCID(),
                                          NativeMethods.DISPATCH_METHOD,
                                          new NativeMethods.tagDISPPARAMS(),
                                          null,
                                          pExcepInfo, null);

                    Debug.Assert(NativeMethods.Succeeded(hr), "Failed to launch about box.");
                }
                return(value);
            }
Exemplo n.º 4
0
            /// <summary>
            ///  Edits the given object value using the editor style provided by GetEditorStyle.
            ///  A service provider is provided so that any required editing services can be obtained.
            /// </summary>
            public unsafe override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                object component = context.Instance;

                if (Marshal.IsComObject(component) && component is UnsafeNativeMethods.IDispatch)
                {
                    UnsafeNativeMethods.IDispatch pDisp = (UnsafeNativeMethods.IDispatch)component;
                    var     pExcepInfo = new NativeMethods.tagEXCEPINFO();
                    var     dispParams = new Ole32.DISPPARAMS();
                    Guid    g          = Guid.Empty;
                    HRESULT hr         = pDisp.Invoke(
                        Ole32.DispatchID.ABOUTBOX,
                        &g,
                        Kernel32.GetThreadLocale(),
                        NativeMethods.DISPATCH_METHOD,
                        &dispParams,
                        null,
                        pExcepInfo,
                        null);
                    Debug.Assert(hr.Succeeded(), "Failed to launch about box.");
                }

                return(value);
            }
 /// <include file='doc\COM2AboutBoxPropertyDescriptor.uex' path='docs/doc[@for="Com2AboutBoxPropertyDescriptor.AboutBoxUITypeEditor.EditValue"]/*' />
 /// <devdoc>
 ///      Edits the given object value using the editor style provided by
 ///      GetEditorStyle.  A service provider is provided so that any
 ///      required editing services can be obtained.
 /// </devdoc>
 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
          object component = context.Instance;
          
          if (Marshal.IsComObject(component) && component is UnsafeNativeMethods.IDispatch) {
             UnsafeNativeMethods.IDispatch pDisp = (UnsafeNativeMethods.IDispatch)component;
             NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO();
             Guid g = Guid.Empty;
 
             int hr = pDisp.Invoke(NativeMethods.ActiveX.DISPID_ABOUTBOX,
                                   ref g,
                                   SafeNativeMethods.GetThreadLCID(),
                                   NativeMethods.DISPATCH_METHOD,
                                   new NativeMethods.tagDISPPARAMS(),
                                   null,
                                   pExcepInfo, null);
                                   
             Debug.Assert(NativeMethods.Succeeded(hr), "Failed to launch about box.");
          }
          return value;
 }
Exemplo n.º 6
0
        /// <include file='doc\COM2PropertyDescriptor.uex' path='docs/doc[@for="Com2PropertyDescriptor.GetNativeValue"]/*' />
        /// <devdoc>
        ///     Retrieves the current native value of the property on component,
        ///     invoking the getXXX method.  An exception in the getXXX
        ///     method will pass through.
        /// </devdoc>
        public Object GetNativeValue(Object component){
            if (component == null)
                return null;

            if (component is ICustomTypeDescriptor) {
                component = ((ICustomTypeDescriptor)component).GetPropertyOwner(this);
            }

            if (component == null || !Marshal.IsComObject(component) || !(component is UnsafeNativeMethods.IDispatch))
                return null;

            UnsafeNativeMethods.IDispatch pDisp = (UnsafeNativeMethods.IDispatch)component;
            Object[] pVarResult = new Object[1];
            NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO();
            Guid g = Guid.Empty;

            int hr = pDisp.Invoke(this.dispid,
                                  ref g,
                                  SafeNativeMethods.GetThreadLCID(),
                                  NativeMethods.DISPATCH_PROPERTYGET,
                                  new NativeMethods.tagDISPPARAMS(),
                                  pVarResult,
                                  pExcepInfo, null);

            switch (hr) {
            case NativeMethods.S_OK:
            case NativeMethods.S_FALSE:

                if (pVarResult[0] == null || Convert.IsDBNull(pVarResult[0])) {
                    lastValue = null;
                }
                else {
                    lastValue = pVarResult[0];
                }
                return lastValue;
            case NativeMethods.DISP_E_EXCEPTION:
                //PrintExceptionInfo(pExcepInfo);
                return null;
            default:
                throw new ExternalException(SR.GetString(SR.DispInvokeFailed, "GetValue" , hr), hr);
            }
        }
Exemplo n.º 7
0
        public override void SetValue(Object component, Object value) {

            if (this.readOnly) {
                throw new NotSupportedException(SR.GetString(SR.COM2ReadonlyProperty, this.Name ));
            }

            object owner = component;
            if (owner is ICustomTypeDescriptor) {
                owner = ((ICustomTypeDescriptor)owner).GetPropertyOwner(this);
            }

            if (owner == null || !Marshal.IsComObject(owner) || !(owner is UnsafeNativeMethods.IDispatch)) {
                return;
            }

            // do we need to convert the type?
            if (valueConverter != null) {
                bool cancel = false;
                value = valueConverter.ConvertManagedToNative(value, this, ref cancel);
                if (cancel) {
                    return;
                }
            }

            UnsafeNativeMethods.IDispatch pDisp = (UnsafeNativeMethods.IDispatch)owner;

            NativeMethods.tagDISPPARAMS dp = new NativeMethods.tagDISPPARAMS();
            NativeMethods.tagEXCEPINFO excepInfo = new NativeMethods.tagEXCEPINFO();
            dp.cArgs = 1;
            dp.cNamedArgs = 1;
            int[] namedArgs = new int[]{NativeMethods.DISPID_PROPERTYPUT};
            GCHandle gcHandle = GCHandle.Alloc(namedArgs, GCHandleType.Pinned);

            try {
                dp.rgdispidNamedArgs = Marshal.UnsafeAddrOfPinnedArrayElement(namedArgs, 0);
                IntPtr mem = Marshal.AllocCoTaskMem( 16 /*Marshal.SizeOf(typeof(VARIANT)) */);
                SafeNativeMethods.VariantInit(new HandleRef(null, mem));
                Marshal.GetNativeVariantForObject(value, mem);
                dp.rgvarg = mem;
                try {

                    Guid g = Guid.Empty;
                    int hr = pDisp.Invoke(this.dispid,
                                          ref g,
                                          SafeNativeMethods.GetThreadLCID(),
                                          NativeMethods.DISPATCH_PROPERTYPUT,
                                          dp,
                                          null,
                                          excepInfo, new IntPtr[1]);


                    string errorInfo = null;
                    if (hr == NativeMethods.DISP_E_EXCEPTION && excepInfo.scode != 0) {
                        hr = excepInfo.scode;
                        errorInfo = excepInfo.bstrDescription;
                    }

                    switch (hr) {
                    case NativeMethods.E_ABORT:
                    case NativeMethods.OLE_E_PROMPTSAVECANCELLED:
                        // cancelled checkout, etc.
                        return;
                    case NativeMethods.S_OK:
                    case NativeMethods.S_FALSE:
                        OnValueChanged(component, EventArgs.Empty);
                        lastValue = value;
                        return;
                    default:
                        
                        //Debug.Fail(String.Format("IDispatch::Invoke(INVOKE_PROPPUT) returned hr=0x{0:X}", hr));
                        
                        if (pDisp is UnsafeNativeMethods.ISupportErrorInfo) {
                            g = typeof(UnsafeNativeMethods.IDispatch).GUID;
                            if (NativeMethods.Succeeded(((UnsafeNativeMethods.ISupportErrorInfo)pDisp).InterfaceSupportsErrorInfo(ref g))) {
                                UnsafeNativeMethods.IErrorInfo pErrorInfo = null;
                                UnsafeNativeMethods.GetErrorInfo(0, ref pErrorInfo);
                                string info= null;
                                if (pErrorInfo != null) {
                                    if (NativeMethods.Succeeded(pErrorInfo.GetDescription(ref info))) {
                                        errorInfo = info;
                                    }
                                }
                                
                            }
                        }
                        else if (errorInfo == null) {
                            StringBuilder strMessage = new StringBuilder(256);
                        
                            int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | 
                                                                    NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS,
                                                                    NativeMethods.NullHandleRef, 
                                                                    hr,
                                                                    CultureInfo.CurrentCulture.LCID,
                                                                    strMessage,
                                                                    255,
                                                                    NativeMethods.NullHandleRef);
                            
                            
                            if (result == 0) {   
                                errorInfo = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.DispInvokeFailed, "SetValue", hr));
                            }
                            else {       
                                errorInfo = strMessage.ToString();
                                // strip of any trailing cr/lf
                                while (errorInfo.Length > 0 && 
                                        errorInfo[errorInfo.Length -1] == '\n' ||
                                        errorInfo[errorInfo.Length -1] == '\r') {
                                    errorInfo = errorInfo.Substring(0, errorInfo.Length-1);
                                }    
                            }
                        }
                        throw new ExternalException(errorInfo, hr);
                    }
                }
                finally {
                    SafeNativeMethods.VariantClear(new HandleRef(null, mem));
                    Marshal.FreeCoTaskMem(mem);
                }
            }
            finally {
                gcHandle.Free();
            }
        }
        internal int GetPropertyValue(Object component, int dispid, Object[] retval) {
            if (!(component is UnsafeNativeMethods.IDispatch)) {
                return NativeMethods.E_NOINTERFACE;
            }
            UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component;
            try {
                Guid g = Guid.Empty;
                NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO();
                int hr;

                try{

                   hr = iDispatch.Invoke(dispid,
                                             ref g,
                                             SafeNativeMethods.GetThreadLCID(),
                                             NativeMethods.DISPATCH_PROPERTYGET,
                                             new NativeMethods.tagDISPPARAMS(),
                                             retval,
                                             pExcepInfo, null);

                   /*if (hr != NativeMethods.S_OK){
                     Com2PropertyDescriptor.PrintExceptionInfo(pExcepInfo);

                   } */
                   if (hr == NativeMethods.DISP_E_EXCEPTION) {
                       hr = pExcepInfo.scode;
                   }

                }
                catch (ExternalException ex){
                    hr = ex.ErrorCode;
                }
                return hr;
            }
            catch {
                //Debug.Fail(e.ToString() + " " + component.GetType().GUID.ToString() + " " + component.ToString());
            }
            return NativeMethods.E_FAIL;
        }