Exemplo n.º 1
0
        public COMCallableIUnknown()
        {
            _handle = GCHandle.Alloc(this);

            IUnknownVTable *       vtable = (IUnknownVTable *)Marshal.AllocHGlobal(sizeof(IUnknownVTable)).ToPointer();
            QueryInterfaceDelegate qi     = new QueryInterfaceDelegate(QueryInterfaceImpl);

            vtable->QueryInterface = Marshal.GetFunctionPointerForDelegate(qi);
            _delegates.Add(qi);

            AddRefDelegate addRef = new AddRefDelegate(AddRefImpl);

            vtable->AddRef = Marshal.GetFunctionPointerForDelegate(addRef);
            _delegates.Add(addRef);


            ReleaseDelegate release = new ReleaseDelegate(ReleaseImpl);

            vtable->Release = Marshal.GetFunctionPointerForDelegate(release);
            _delegates.Add(release);


            IUnknownObject           = Marshal.AllocHGlobal(IntPtr.Size);
            *(void **)IUnknownObject = vtable;

            _interfaces.Add(IUnknownGuid, IUnknownObject);
        }
Exemplo n.º 2
0
        public IntPtr QueryInterface(ref Guid riid)
        {
            QueryInterfaceDelegate queryInterface = (QueryInterfaceDelegate)Marshal.GetDelegateForFunctionPointer(_unknownVTable->QueryInterface, typeof(QueryInterfaceDelegate));

            int hr = queryInterface(Self, ref riid, out IntPtr unk);

            return(hr == S_OK ? unk : IntPtr.Zero);
        }
Exemplo n.º 3
0
        private CcwHook(IDisposable comObject, IntPtr comInterface, IntPtr originalInterface, CcwWrappers wrappers)
        {
            this.comObject = comObject;
            this.originalInterface = originalInterface;
            this.wrappers = wrappers;

            queryInterfaceDetour = QueryInterfaceDetour;
            releaseDetour = ReleaseDetour;

            var vtable = Marshal.ReadIntPtr(comInterface);
            var originalQueryInterfacePointer = Marshal.ReadIntPtr(vtable, 0 * IntPtr.Size);
            var originalReleasePointer = Marshal.ReadIntPtr(vtable, 2 * IntPtr.Size);
            originalQueryInterface = Marshal.GetDelegateForFunctionPointer<QueryInterfaceDelegate>(originalQueryInterfacePointer);
            originalRelease = Marshal.GetDelegateForFunctionPointer<ReleaseDelegate>(originalReleasePointer);
            Marshal.WriteIntPtr(vtable, 0 * IntPtr.Size, Marshal.GetFunctionPointerForDelegate(queryInterfaceDetour));
            Marshal.WriteIntPtr(vtable, 2 * IntPtr.Size, Marshal.GetFunctionPointerForDelegate(releaseDetour));
        }
Exemplo n.º 4
0
        private CcwHook(IDisposable comObject, IntPtr comInterface, IntPtr originalInterface, CcwWrappers wrappers)
        {
            this.comObject         = comObject;
            this.originalInterface = originalInterface;
            this.wrappers          = wrappers;

            queryInterfaceDetour = QueryInterfaceDetour;
            releaseDetour        = ReleaseDetour;

            var vtable = Marshal.ReadIntPtr(comInterface);
            var originalQueryInterfacePointer = Marshal.ReadIntPtr(vtable, 0 * IntPtr.Size);
            var originalReleasePointer        = Marshal.ReadIntPtr(vtable, 2 * IntPtr.Size);

            originalQueryInterface = Marshal.GetDelegateForFunctionPointer <QueryInterfaceDelegate>(originalQueryInterfacePointer);
            originalRelease        = Marshal.GetDelegateForFunctionPointer <ReleaseDelegate>(originalReleasePointer);
            Marshal.WriteIntPtr(vtable, 0 * IntPtr.Size, Marshal.GetFunctionPointerForDelegate(queryInterfaceDetour));
            Marshal.WriteIntPtr(vtable, 2 * IntPtr.Size, Marshal.GetFunctionPointerForDelegate(releaseDetour));
        }
Exemplo n.º 5
0
            public static IntPtr Create(
                QueryInterfaceDelegate queryInterface,
                AddRefDelegate addRef,
                ReleaseDelegate release,
                BarDelegate bar)
            {
                var instance = new ManualVTable
                {
                    QueryInterface = Marshal.GetFunctionPointerForDelegate(queryInterface),
                    AddRef         = Marshal.GetFunctionPointerForDelegate(addRef),
                    Release        = Marshal.GetFunctionPointerForDelegate(release),
                    Bar            = Marshal.GetFunctionPointerForDelegate(bar)
                };
                var marshalledInstance = Marshal.AllocHGlobal(Marshal.SizeOf(instance));

                Marshal.StructureToPtr(instance, marshalledInstance, false);
                return(marshalledInstance);
            }
Exemplo n.º 6
0
        static Win32DnD()
        {
            // Required for all other OLE functions to work
            Win32OleInitialize(IntPtr.Zero);

            // We reuse those
            DragDropEventArgs     = new DragEventArgs(new DataObject(DataFormats.FileDrop, new string[0]), 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
            DragFeedbackEventArgs = new GiveFeedbackEventArgs(DragDropEffects.None, true);
            DragContinueEventArgs = new QueryContinueDragEventArgs(0, false, DragAction.Continue);
            DragFormats           = new ArrayList();
            DragFormatArray       = new FORMATETC[0];
            DragMediums           = new ArrayList();

            // Set up delegates
            // IDataObject
            DOQueryInterface      = new QueryInterfaceDelegate(ComIDataObject.QueryInterface);
            DOAddRef              = new AddRefDelegate(ComIDataObject.AddRef);
            DORelease             = new ReleaseDelegate(ComIDataObject.Release);
            GetData               = new GetDataDelegate(ComIDataObject.GetData);
            GetDataHere           = new GetDataHereDelegate(ComIDataObject.GetDataHere);
            QueryGetData          = new QueryGetDataDelegate(ComIDataObject.QueryGetData);
            GetCanonicalFormatEtc = new GetCanonicalFormatEtcDelegate(ComIDataObject.GetCanonicalFormatEtc);
            SetData               = new SetDataDelegate(ComIDataObject.SetData);
            EnumFormatEtc         = new EnumFormatEtcDelegate(ComIDataObject.EnumFormatEtc);
            DAdvise               = new DAdviseDelegate(ComIDataObject.DAdvise);
            DUnadvise             = new DUnadviseDelegate(ComIDataObject.DUnadvise);
            EnumDAdvise           = new EnumDAdviseDelegate(ComIDataObject.EnumDAdvise);

            // IDropSource
            DSQueryInterface  = new QueryInterfaceDelegate(ComIDropSource.QueryInterface);
            DSAddRef          = new AddRefDelegate(ComIDropSource.AddRef);
            DSRelease         = new ReleaseDelegate(ComIDropSource.Release);
            QueryContinueDrag = new QueryContinueDragDelegate(ComIDropSource.QueryContinueDrag);
            GiveFeedback      = new GiveFeedbackDelegate(ComIDropSource.GiveFeedback);

            // IDropTarget
            DTQueryInterface = new QueryInterfaceDelegate(ComIDropTarget.QueryInterface);
            DTAddRef         = new AddRefDelegate(ComIDropTarget.AddRef);
            DTRelease        = new ReleaseDelegate(ComIDropTarget.Release);
            DragEnter        = new DragEnterDelegate(ComIDropTarget.DragEnter);
            DragOver         = new DragOverDelegate(ComIDropTarget.DragOver);
            DragLeave        = new DragLeaveDelegate(ComIDropTarget.DragLeave);
            Drop             = new DropDelegate(ComIDropTarget.Drop);
        }
Exemplo n.º 7
0
        private protected CallableCOMWrapper(RefCountedFreeLibrary library, ref Guid desiredInterface, IntPtr pUnknown)
        {
            _library = library;
            _library.AddRef();

            IUnknownVTable *tbl = *(IUnknownVTable **)pUnknown;

            QueryInterfaceDelegate queryInterface = (QueryInterfaceDelegate)Marshal.GetDelegateForFunctionPointer(tbl->QueryInterface, typeof(QueryInterfaceDelegate));
            int hr = queryInterface(pUnknown, ref desiredInterface, out IntPtr pCorrectUnknown);

            if (hr != 0)
            {
                GC.SuppressFinalize(this);
                throw new InvalidCastException($"{GetType().FullName}.QueryInterface({desiredInterface}) failed, hr=0x{hr:x}");
            }

            ReleaseDelegate release = (ReleaseDelegate)Marshal.GetDelegateForFunctionPointer(tbl->Release, typeof(ReleaseDelegate));
            int             count   = release(pUnknown);

            Self           = pCorrectUnknown;
            _unknownVTable = *(IUnknownVTable **)pCorrectUnknown;
        }
Exemplo n.º 8
0
        private void SetupIUnknownMethods()
        {
            /* Read the COM v-table pointer */
            IntPtr pVtable = Marshal.ReadIntPtr(m_comPointer);

            const int ADDREF_VTABLE_INDEX = 0;
            const int RELEASE_VTABLE_INDEX = 1;
            const int QUERYINTERFACE_VTABLE_INDEX = 2;

            /* Get the function pointer */
            IntPtr pFunc = Marshal.ReadIntPtr(pVtable, ADDREF_VTABLE_INDEX * IntPtr.Size);

            /* Cast the function pointer to a .NET delegate */
            AddRefMethod = (AddRefDelegate)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(AddRefDelegate));

            /* Rinse and repeat */
            pFunc = Marshal.ReadIntPtr(pVtable, RELEASE_VTABLE_INDEX * IntPtr.Size);

            ReleaseMethod = (ReleaseDelegate)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(ReleaseDelegate));

            pFunc = Marshal.ReadIntPtr(pVtable, QUERYINTERFACE_VTABLE_INDEX * IntPtr.Size);

            QueryInterfaceMethod = (QueryInterfaceDelegate)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(QueryInterfaceDelegate));
        }
Exemplo n.º 9
0
 public void ReleaseMarshalledDelegates()
 {
     releaseDetour        = null;
     queryInterfaceDetour = null;
 }
Exemplo n.º 10
0
            public static IntPtr Create(
				QueryInterfaceDelegate queryInterface,
				AddRefDelegate addRef,
				ReleaseDelegate release,
				BarDelegate bar)
            {
                var instance = new ManualVTable
                {
                    QueryInterface = Marshal.GetFunctionPointerForDelegate(queryInterface),
                    AddRef = Marshal.GetFunctionPointerForDelegate(addRef),
                    Release = Marshal.GetFunctionPointerForDelegate(release),
                    Bar = Marshal.GetFunctionPointerForDelegate(bar)
                };
                var marshalledInstance = Marshal.AllocHGlobal(Marshal.SizeOf(instance));
                Marshal.StructureToPtr(instance, marshalledInstance, false);
                return marshalledInstance;
            }
Exemplo n.º 11
0
		static Win32DnD()
		{
			// Required for all other OLE functions to work
			Win32OleInitialize(IntPtr.Zero);

			// We reuse those
			DragDropEventArgs = new DragEventArgs(new DataObject(DataFormats.FileDrop, new string[0]), 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
			DragFeedbackEventArgs = new GiveFeedbackEventArgs(DragDropEffects.None, true);
			DragContinueEventArgs = new QueryContinueDragEventArgs(0, false, DragAction.Continue);
			DragFormats = new ArrayList();
			DragFormatArray = new FORMATETC[0];
			DragMediums = new ArrayList();

			// Set up delegates
			// IDataObject
			DOQueryInterface = new QueryInterfaceDelegate(ComIDataObject.QueryInterface);
			DOAddRef = new AddRefDelegate(ComIDataObject.AddRef);
			DORelease = new ReleaseDelegate(ComIDataObject.Release);
			GetData = new GetDataDelegate(ComIDataObject.GetData);
			GetDataHere = new GetDataHereDelegate(ComIDataObject.GetDataHere);
			QueryGetData = new QueryGetDataDelegate(ComIDataObject.QueryGetData);
			GetCanonicalFormatEtc = new GetCanonicalFormatEtcDelegate(ComIDataObject.GetCanonicalFormatEtc);
			SetData = new SetDataDelegate(ComIDataObject.SetData);
			EnumFormatEtc = new EnumFormatEtcDelegate(ComIDataObject.EnumFormatEtc);
			DAdvise = new DAdviseDelegate(ComIDataObject.DAdvise);
			DUnadvise = new DUnadviseDelegate(ComIDataObject.DUnadvise);
			EnumDAdvise = new EnumDAdviseDelegate(ComIDataObject.EnumDAdvise);

			// IDropSource
			DSQueryInterface = new QueryInterfaceDelegate(ComIDropSource.QueryInterface);
			DSAddRef = new AddRefDelegate(ComIDropSource.AddRef);
			DSRelease = new ReleaseDelegate(ComIDropSource.Release);
			QueryContinueDrag = new QueryContinueDragDelegate(ComIDropSource.QueryContinueDrag);
			GiveFeedback = new GiveFeedbackDelegate(ComIDropSource.GiveFeedback);

			// IDropTarget
			DTQueryInterface = new QueryInterfaceDelegate(ComIDropTarget.QueryInterface);
			DTAddRef = new AddRefDelegate(ComIDropTarget.AddRef);
			DTRelease = new ReleaseDelegate(ComIDropTarget.Release);
			DragEnter = new DragEnterDelegate(ComIDropTarget.DragEnter);
			DragOver = new DragOverDelegate(ComIDropTarget.DragOver);
			DragLeave = new DragLeaveDelegate(ComIDropTarget.DragLeave);
			Drop = new DropDelegate(ComIDropTarget.Drop);
		}
Exemplo n.º 12
0
 public void ReleaseMarshalledDelegates()
 {
     releaseDetour = null;
     queryInterfaceDetour = null;
 }