public static WeakReference <Derived> AllocateAndUseBaseType(ComWrappers cw, bool aggregateRefTracker) { var derived = new Derived(cw, aggregateRefTracker); // Use the base type IntPtr testWrapper = cw.GetOrCreateComInterfaceForObject(new Test(), CreateComInterfaceFlags.TrackerSupport); int id = derived.AddObjectRef(testWrapper); // Tell the tracker runtime to release its hold on the base instance. MockReferenceTrackerRuntime.ReleaseAllTrackerObjects(); // Validate the GC is tracking the entire Derived type. ForceGC(); derived.DropObjectRef(id); return(new WeakReference <Derived>(derived)); }
static WeakReference <ITrackerObjectWrapper> CreateAndRegisterWrapper(ComWrappers cw, IntPtr trackerObjRaw) { // Manually create a wrapper var iid = typeof(ITrackerObject).GUID; IntPtr iTestComObject; int hr = Marshal.QueryInterface(trackerObjRaw, ref iid, out iTestComObject); Assert.AreEqual(0, hr); var nativeWrapper = new ITrackerObjectWrapper(iTestComObject); nativeWrapper = (ITrackerObjectWrapper)cw.GetOrRegisterObjectForComInstance(trackerObjRaw, CreateObjectFlags.None, nativeWrapper); // Set this on the return instead of during creation since the returned wrapper may be the one from // the internal cache and not the one passed in above. nativeWrapper.ReregisterForFinalize = true; return(new WeakReference <ITrackerObjectWrapper>(nativeWrapper, trackResurrection: true)); }
protected unsafe override ComInterfaceEntry *ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count) { LastComputeVtablesObject = obj; if (ReturnInvalid) { count = -1; return(null); } if (obj is Test) { return(ComputeVtablesForTestObject((Test)obj, out count)); } else if (string.Equals(ManagedServerTypeName, obj.GetType().Name)) { IntPtr fpQueryInteface = default; IntPtr fpAddRef = default; IntPtr fpRelease = default; ComWrappers.GetIUnknownImpl(out fpQueryInteface, out fpAddRef, out fpRelease); var vtbl = new IUnknownVtbl() { QueryInterface = fpQueryInteface, AddRef = fpAddRef, Release = fpRelease }; var vtblRaw = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IUnknownVtbl), sizeof(IUnknownVtbl)); Marshal.StructureToPtr(vtbl, vtblRaw, false); // Including interfaces to allow QI, but not actually returning a valid vtable, since it is not needed for the tests here. var entryRaw = (ComInterfaceEntry *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IUnknownVtbl), sizeof(ComInterfaceEntry)); entryRaw[0].IID = typeof(Server.Contract.IConsumeNETServer).GUID; entryRaw[0].Vtable = vtblRaw; count = 1; return(entryRaw); } count = -1; return(null); }
public static int Main() { bool testComMarshal = true; ComWrappers.RegisterForMarshalling(new ComWrappersImpl()); try { TestByValue(testComMarshal); TestByRef(testComMarshal); TestOut(); TestFieldByValue(testComMarshal); TestFieldByRef(testComMarshal); } catch (Exception e) { Console.WriteLine($"Test failed: {e}"); return(101); } return(100); }
static void Main() { // Reflection trick does not helps too much. #if NET472 var field = typeof(Control.ControlAccessibleObject) .GetField("s_oleAccAvailable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); field.SetValue(null, IntPtr.Zero); #else Application.SetHighDpiMode(HighDpiMode.SystemAware); var field = typeof(Control.ControlAccessibleObject) .GetField("s_oleAccAvailable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); //field.SetValue(null, IntPtr.Zero); #endif ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
private static void ValidateRegisterForTrackerSupport() { Console.WriteLine($"Running {nameof(ValidateRegisterForTrackerSupport)}..."); var wrappers1 = GlobalComWrappers.Instance; ComWrappers.RegisterForTrackerSupport(wrappers1); Assert.Throws <InvalidOperationException>( () => { ComWrappers.RegisterForTrackerSupport(wrappers1); }, "Should not be able to re-register for global ComWrappers"); var wrappers2 = new GlobalComWrappers(); Assert.Throws <InvalidOperationException>( () => { ComWrappers.RegisterForTrackerSupport(wrappers2); }, "Should not be able to reset for global ComWrappers"); }
private static void ValidateRegisterForMarshalling() { Console.WriteLine($"Running {nameof(ValidateRegisterForMarshalling)}..."); var wrappers1 = GlobalComWrappers.Instance; ComWrappers.RegisterForMarshalling(wrappers1); Assert.Throws <InvalidOperationException>( () => { ComWrappers.RegisterForMarshalling(wrappers1); }); var wrappers2 = new GlobalComWrappers(); Assert.Throws <InvalidOperationException>( () => { ComWrappers.RegisterForMarshalling(wrappers2); }); }
static int Main(string[] doNotUse) { try { ValidateNonComWrappers(); ComWrappers.RegisterForTrackerSupport(TestComWrappers.TrackerSupportInstance); ValidateGlobalInstanceTrackerSupport(); ComWrappers.RegisterForMarshalling(TestComWrappers.MarshallingInstance); ValidateGlobalInstanceMarshalling(); ValidateLocalInstance(); } catch (Exception e) { Console.WriteLine($"Test Failure: {e}"); return(101); } return(100); }
protected unsafe override ComInterfaceEntry *ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count) { Assert.Equal(CreateComInterfaceFlags.None, flags); IntPtr fpQueryInterface = default; IntPtr fpAddRef = default; IntPtr fpRelease = default; ComWrappers.GetIUnknownImpl(out fpQueryInterface, out fpAddRef, out fpRelease); var vtblRaw = (IntPtr *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ComWrappersImpl), IntPtr.Size * 3); vtblRaw[0] = fpQueryInterface; vtblRaw[1] = fpAddRef; vtblRaw[2] = fpRelease; var entryRaw = (ComInterfaceEntry *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ComWrappersImpl), sizeof(ComInterfaceEntry)); entryRaw->IID = new Guid(IID_TestQueryInterface); entryRaw->Vtable = (IntPtr)vtblRaw; count = 1; return(entryRaw); }
public Derived(ComWrappers cw, bool aggregateRefTracker) : base(cw, aggregateRefTracker) { }