public int TryInvokePropertySet(string propertyName, object value) { int hr = NativeMethods.S_OK; IDispatch dispatch = null; try { if (IsDispatch) { dispatch = TryGetUniqueRCW <IDispatch>(); if (dispatch != null) { if (MarshalEx.Succeeded(hr = dispatch.InvokePropertySet(propertyName, value))) { } } } } catch { } finally { ComPtr.CleanupUniqueRCW(dispatch); } return(hr); }
public int TryInvokeMethod(string name, object[] args, out object returnValue) { int hr = NativeMethods.S_OK; IDispatch dispatch = null; returnValue = null; try { if (IsDispatch) { dispatch = TryGetUniqueRCW <IDispatch>(); if (dispatch != null) { if (MarshalEx.Succeeded(hr = dispatch.InvokeMethod(name, args, out returnValue))) { } } } } catch { } finally { ComPtr.CleanupUniqueRCW(dispatch); } return(hr); }
public static int InvokePropertySet(this IDispatch dispatch, string name, object value) { int hr = NativeMethods.S_OK; int dispid = 0; if (MarshalEx.Succeeded(dispatch.GetIdOfName(name, out dispid))) { hr = dispatch.InvokePropertySet(dispid, value); } return(hr); }
public override void SetValue(object component, object value) { ComPtr p = component as ComPtr; if (p != null) { if (MarshalEx.Succeeded(p.TryInvokePropertySet(_comPtrProperty.Name, value))) { _comPtrProperty.Value = value; } } }
public static int InvokeMethod(this IDispatch dispatch, string name, object[] args, out object returnValue) { int hr = NativeMethods.S_OK; int id = 0; returnValue = null; if (MarshalEx.Succeeded(hr = dispatch.GetIdOfName(name, out id))) { hr = InvokeMethod(dispatch, id, args, out returnValue); } return(hr); }
public override object GetValue(object component) { #if DEBUG if (_comPropertyInfo == null) { if (_comPtrProperty.Name.Equals("[RefCount]")) { return(((ComPtr)component).RefCount); } } #endif if (_comPropertyInfo == null) { if (_comPtrProperty.Name.Equals("[Pointer]")) { return(((ComPtr)component).DangerousGetHandle()); } } try { if (_comPtrProperty.Name.Equals("MailSession")) { return(_comPtrProperty.Value); } } catch { } ComPtr p = component as ComPtr; if (p != null) { object value = null; if (MarshalEx.Succeeded(p.TryInvokePropertyGet(_comPtrProperty.Name, out value))) { return(value); } } return(_comPtrProperty.Value); }
public int TryGetItemCount() { IDispatch dispatch = null; object count = 0; try { dispatch = TryGetUniqueRCW <IDispatch>(); if (MarshalEx.Succeeded(dispatch.InvokePropertyGet("Count", out count))) { return(count is int?(int)count : -1); } } catch { } finally { CleanupUniqueRCW(dispatch); } return(-1); }
public object TryGetFirstAvailableProperty(string[] propertyNames) { object value = null; ComTypeInfo comType = TryGetComTypeInfo(); if (comType != null) { foreach (string propertyName in propertyNames) { ComPropertyInfo comPropertyInfo = comType.Properties.Where(x => x.Name.Equals(propertyName)).FirstOrDefault(); if ((comPropertyInfo != null) && (comPropertyInfo.GetFunction != null)) { if (MarshalEx.Succeeded(TryInvokePropertyGet(comPropertyInfo.GetFunction.DispId, out value))) { break; } } } } return(value); }
public bool Is <T>() { if (this.IsInvalid) { return(false); } var riid = typeof(T).GUID; IntPtr p = IntPtr.Zero; try { if (MarshalEx.Succeeded(Marshal.QueryInterface(this.handle, ref riid, out p))) { int count = Marshal.Release(p); return(true); } } catch { } return(false); }
public static int InvokePropertyGet(this IDispatch dispatch, int dispid, out object value) { int hr = NativeMethods.S_OK; value = null; var guid = Guid.Empty; var lcid = NativeMethods.LOCALE_SYSTEM_DEFAULT; var flags = System.Runtime.InteropServices.ComTypes.INVOKEKIND.INVOKE_PROPERTYGET; var dp = default(System.Runtime.InteropServices.ComTypes.DISPPARAMS); var pExcepInfo = default(System.Runtime.InteropServices.ComTypes.EXCEPINFO); uint pArgErr = 0; Variant variant = default(Variant); try { hr = dispatch.Invoke( dispid, ref guid, lcid, flags, ref dp, out variant, ref pExcepInfo, out pArgErr ); if (MarshalEx.Succeeded(hr)) { try { switch (variant.VariantType) { case VarEnum.VT_PTR: GlobalExceptionHandler.HandleException(); break; case VarEnum.VT_UNKNOWN: case VarEnum.VT_DISPATCH: if (variant.ptr1.Equals(IntPtr.Zero) == false) { value = new ComPtr(variant.ptr1); int count = Marshal.Release(variant.ptr1); } break; case VarEnum.VT_USERDEFINED: value = variant.ToObject(); break; default: value = variant.ToObject(); break; } } catch (COMException ex) { hr = ex.ErrorCode; } } } catch { GlobalExceptionHandler.HandleException(); } finally { variant.Clear(); } return(hr); }
public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { if (_propertyDescriptorCollection == null) { _propertyDescriptorCollection = new PropertyDescriptorCollection(new PropertyDescriptor[] { }, true); ComTypeInfo comTypeInfo = TryGetComTypeInfo(); if (comTypeInfo != null) { List <ComPtrPropertyDescriptor> list = new List <ComPtrPropertyDescriptor>(); foreach (ComPropertyInfo comPropertyInfo in comTypeInfo.GetProperties(true)) { ComFunctionInfo getComFunctionInfo = comPropertyInfo.GetFunction; bool bReadOnly = comPropertyInfo.SetFunction == null ? true : false; if (getComFunctionInfo != null) { VarEnum variantType = getComFunctionInfo.ReturnParameter.VariantType; switch (variantType) { case VarEnum.VT_PTR: case VarEnum.VT_DISPATCH: case VarEnum.VT_UNKNOWN: continue; case VarEnum.VT_SAFEARRAY: continue; } // Special case. MailSession is a PITA property that causes modal dialog. if (comPropertyInfo.Name.Equals("MailSession")) { ComPtrProperty comPtrProperty = new ComPtrProperty(comPropertyInfo.Name, comPropertyInfo.Description, 0, typeof(int), variantType, true); list.Add(new ComPtrPropertyDescriptor(ref comPtrProperty, comPropertyInfo, attributes)); continue; } object value = null; if (MarshalEx.Succeeded(TryInvokePropertyGet(getComFunctionInfo.DispId, out value))) { Type propertyType = typeof(object); if (value != null) { propertyType = value.GetType(); } else { bReadOnly = true; } ComPtrProperty comPtrProperty = new ComPtrProperty(comPropertyInfo.Name, comPropertyInfo.Description, value, propertyType, variantType, bReadOnly); list.Add(new ComPtrPropertyDescriptor(ref comPtrProperty, comPropertyInfo, attributes)); } } } #if DEBUG var refCountProperty = new ComPtrProperty("[RefCount]", "", this.RefCount, typeof(int), VarEnum.VT_I4, true); list.Add(new ComPtrPropertyDescriptor(ref refCountProperty, null, attributes)); #endif var pointerProperty = new ComPtrProperty("[Pointer]", "", this.handle, typeof(string), VarEnum.VT_INT, true); list.Add(new ComPtrPropertyDescriptor(ref pointerProperty, null, attributes)); _propertyDescriptorCollection = new PropertyDescriptorCollection(list.ToArray()); } } return(_propertyDescriptorCollection); }