void System.Runtime.InteropServices.ComTypes.IDataObject.GetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { try { medium = new System.Runtime.InteropServices.ComTypes.STGMEDIUM(); if (formatetc.cfFormat == (Int16)DataFormats.GetFormat(NativeMethods.CFSTR_FILECONTENTS).Id) { m_lindex = formatetc.lindex; } if (GetTymedUseable(formatetc.tymed)) { if ((formatetc.tymed & TYMED.TYMED_ISTREAM) != TYMED.TYMED_NULL) { if (objectType != ClipboardDataType.File) { return; } try { //DropSuccess?.Invoke(this, null); medium.tymed = TYMED.TYMED_ISTREAM; IStream o = (IStream)GetData("FileContents", false); medium.unionmember = Marshal.GetComInterfaceForObject(o, typeof(IStream)); return; } catch (Exception ex) { //ISLogger.Write("InputshareDataObject: Get FileContents failed: " + ex.Message); return; } } else if ((formatetc.tymed & TYMED.TYMED_HGLOBAL) != TYMED.TYMED_NULL) { medium.tymed = TYMED.TYMED_HGLOBAL; medium.unionmember = NativeMethods.GlobalAlloc(NativeMethods.GHND | NativeMethods.GMEM_DDESHARE, 1); if (medium.unionmember == IntPtr.Zero) { throw new OutOfMemoryException(); } try { ((System.Runtime.InteropServices.ComTypes.IDataObject) this).GetDataHere(ref formatetc, ref medium); return; } catch { NativeMethods.GlobalFree(new HandleRef((STGMEDIUM)medium, medium.unionmember)); medium.unionmember = IntPtr.Zero; return; } } medium.tymed = formatetc.tymed; ((System.Runtime.InteropServices.ComTypes.IDataObject) this).GetDataHere(ref formatetc, ref medium); } else { Marshal.ThrowExceptionForHR(NativeMethods.DV_E_TYMED); } }catch (Exception ex) { medium = new STGMEDIUM(); ISLogger.Write("InputshareDataObject: " + ex.Message); } }
private static IntPtr CreateMmDeviceEnumerator() { var mmde = new MMDeviceEnumeratorObject() as IMMDeviceEnumerator; return(Marshal.GetComInterfaceForObject(mmde, typeof(IMMDeviceEnumerator))); }
MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers, bool allowByRefMatch, object[] parameters) { MethodBase m; int i, j; if (match == null) { throw new ArgumentNullException("match"); } /* first look for an exact match... */ MethodBase exact_match = null; for (i = 0; i < match.Length; ++i) { m = match [i]; ParameterInfo[] args = m.GetParameters(); if (args.Length != types.Length) { continue; } for (j = 0; j < types.Length; ++j) { if (types [j] != args [j].ParameterType) { break; } } if (j == types.Length) { if (exact_match != null) { exact_match = null; break; } else { exact_match = m; } } } if (exact_match != null) { return(exact_match); } /* Try methods with ParamArray attribute */ bool isdefParamArray = false; Type elementType = null; for (i = 0; i < match.Length; ++i) { m = match [i]; ParameterInfo[] args = m.GetParameters(); if (args.Length > types.Length + 1) { continue; } else if (args.Length == 0) { continue; } isdefParamArray = Attribute.IsDefined(args [args.Length - 1], typeof(ParamArrayAttribute)); if (!isdefParamArray) { continue; } elementType = args [args.Length - 1].ParameterType.GetElementType(); for (j = 0; j < types.Length; ++j) { if (j < (args.Length - 1) && types [j] != args [j].ParameterType) { break; } else if (j >= (args.Length - 1) && types [j] != elementType) { break; } } if (j == types.Length) { return(m); } } if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0) { return(null); } MethodBase result = null; for (i = 0; i < match.Length; ++i) { m = match [i]; ParameterInfo[] args = m.GetParameters(); if (args.Length != types.Length) { continue; } if (!check_arguments(types, args, allowByRefMatch)) { continue; } if (result != null) { result = GetBetterMethod(result, m, types); } else { result = m; } } if (result != null || parameters == null || types.Length != parameters.Length) { return(result); } // Xamarin-5278: try with parameters that are COM objects // REVIEW: do we also need to implement best method match? for (i = 0; i < match.Length; ++i) { m = match [i]; ParameterInfo[] methodArgs = m.GetParameters(); if (methodArgs.Length != types.Length) { continue; } for (j = 0; j < types.Length; ++j) { var requiredType = methodArgs [j].ParameterType; if (types [j] == requiredType) { continue; } #if !MOBILE if (types [j] == typeof(__ComObject) && requiredType.IsInterface) { var iface = Marshal.GetComInterfaceForObject(parameters [j], requiredType); if (iface != IntPtr.Zero) { // the COM object implements the desired interface Marshal.Release(iface); continue; } } #endif break; } if (j == types.Length) { return(m); } } return(null); }
public void GetTypedObjectForIUnknown_UncastableType_ThrowsInvalidCastException() { Assert.Throws <InvalidCastException>(() => Marshal.GetComInterfaceForObject(new object(), typeof(INonGenericInterface))); Assert.Throws <InvalidCastException>(() => Marshal.GetComInterfaceForObject(new object(), typeof(INonGenericInterface), CustomQueryInterfaceMode.Allow)); Assert.Throws <InvalidCastException>(() => Marshal.GetComInterfaceForObject <object, INonGenericInterface>(new object())); }
private IntPtr GetToken() { return(Marshal.GetComInterfaceForObject(this, typeof(IObjPool))); }
public void GetComInterfaceForObject_InvalidObject_ThrowsArgumentException(object o) { AssertExtensions.Throws <ArgumentException>("o", () => Marshal.GetComInterfaceForObject(o, typeof(INonGenericInterface))); AssertExtensions.Throws <ArgumentException>("o", () => Marshal.GetComInterfaceForObject(o, typeof(INonGenericInterface), CustomQueryInterfaceMode.Allow)); AssertExtensions.Throws <ArgumentException>("o", () => Marshal.GetComInterfaceForObject <object, INonGenericInterface>(o)); }
public void GetComInterfaceForObject_InvalidType_ThrowsArgumentException(Type type) { AssertExtensions.Throws <ArgumentException>("t", () => Marshal.GetComInterfaceForObject(new object(), type)); AssertExtensions.Throws <ArgumentException>("t", () => Marshal.GetComInterfaceForObject(new object(), type, CustomQueryInterfaceMode.Allow)); }
public void GetComInterfaceForObject_NullType_ThrowsArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("T", () => Marshal.GetComInterfaceForObject(new object(), null)); AssertExtensions.Throws <ArgumentNullException>("T", () => Marshal.GetComInterfaceForObject(new object(), null, CustomQueryInterfaceMode.Allow)); }
public void GetComInterfaceForObject_NullObject_ThrowsArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("o", () => Marshal.GetComInterfaceForObject(null, typeof(INonGenericInterface))); AssertExtensions.Throws <ArgumentNullException>("o", () => Marshal.GetComInterfaceForObject(null, typeof(INonGenericInterface), CustomQueryInterfaceMode.Allow)); AssertExtensions.Throws <ArgumentNullException>("o", () => Marshal.GetComInterfaceForObject <string, string>(null)); }
public void GetComInterfaceForObject_Unix_ThrowsPlatformNotSupportedException() { Assert.Throws <PlatformNotSupportedException>(() => Marshal.GetComInterfaceForObject(null, null)); Assert.Throws <PlatformNotSupportedException>(() => Marshal.GetComInterfaceForObject(null, null, CustomQueryInterfaceMode.Allow)); Assert.Throws <PlatformNotSupportedException>(() => Marshal.GetComInterfaceForObject <int, int>(1)); }