Beispiel #1
0
            private ManagedToNativeWrapper(IStream managedInterface)
            {
                IStreamInterface newInterface;

                lock (managedVtable)
                {
                    // Vtable may have been disposed when shutting down
                    if (vtableRefCount == 0 && comVtable == IntPtr.Zero)
                    {
                        CreateVtable();
                    }
                    vtableRefCount++;
                }

                try
                {
                    this.managedInterface = managedInterface;
                    gcHandle = GCHandle.Alloc(this);

                    newInterface          = new IStreamInterface();
                    newInterface.lpVtbl   = comVtable;
                    newInterface.gcHandle = (IntPtr)gcHandle;
                    comInterface          = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamInterface)));
                    Marshal.StructureToPtr(newInterface, comInterface, false);
                }
                catch
                {
                    this.Dispose();
                    throw;
                }
            }
Beispiel #2
0
 internal static void ReleaseInterface(IStream managedInterface)
 {
     if (managedInterface is NativeToManagedWrapper)
     {
         ((NativeToManagedWrapper)managedInterface).Dispose(true);
     }
 }
Beispiel #3
0
            public void Clone(out IStream ppstm)
            {
                IntPtr newInterface;

                ThrowExceptionForHR(managedVtable.Clone(comInterface, out newInterface));
                ppstm = NativeToManagedWrapper.GetInterface(newInterface, true);
            }
Beispiel #4
0
            internal static IntPtr GetUnderlyingInterface(IStream managedInterface)
            {
                if (managedInterface is NativeToManagedWrapper)
                {
                    NativeToManagedWrapper wrapper = (NativeToManagedWrapper)managedInterface;

                    wrapper.managedVtable.AddRef(wrapper.comInterface);
                    return(wrapper.comInterface);
                }
                else
                {
                    return(IntPtr.Zero);
                }
            }
Beispiel #5
0
            internal static IntPtr GetInterface(IStream managedInterface)
            {
                IntPtr comInterface;

                if (managedInterface == null)
                {
                    return(IntPtr.Zero);
                }
#if !RECURSIVE_WRAPPING
                else if ((comInterface = NativeToManagedWrapper.GetUnderlyingInterface(managedInterface)) == IntPtr.Zero)
#endif
                comInterface = new ManagedToNativeWrapper(managedInterface).comInterface;

                return(comInterface);
            }
Beispiel #6
0
        public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            byte[] buffer;
            long   written = 0;
            int    read;
            int    count;

            if (cb != 0)
            {
                if (cb < 4096)
                {
                    count = (int)cb;
                }
                else
                {
                    count = 4096;
                }
                buffer = new byte[count];
                SetSizeToPosition();
                while (true)
                {
                    if ((read = baseStream.Read(buffer, 0, count)) == 0)
                    {
                        break;
                    }
                    pstm.Write(buffer, read, IntPtr.Zero);
                    written += read;
                    if (written >= cb)
                    {
                        break;
                    }
                    if (cb - written < 4096)
                    {
                        count = (int)(cb - written);
                    }
                }
            }

            if (pcbRead != IntPtr.Zero)
            {
                Marshal.WriteInt64(pcbRead, written);
            }
            if (pcbWritten != IntPtr.Zero)
            {
                Marshal.WriteInt64(pcbWritten, written);
            }
        }
Beispiel #7
0
            private static int CopyTo(IntPtr @this, IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
            {
#if MAP_EX_TO_HR
                try
                {
#endif
                GetObject(@this).managedInterface.CopyTo(pstm, cb, pcbRead, pcbWritten);
                return(S_OK);

#if MAP_EX_TO_HR
            }
            catch (Exception e)
            {
                return(GetHRForException(e));
            }
#endif
            }
Beispiel #8
0
            internal static IStream GetUnderlyingInterface(IntPtr comInterface, bool outParam)
            {
                if (Marshal.ReadIntPtr(comInterface) == comVtable)
                {
                    IStream managedInterface = GetObject(comInterface).managedInterface;

                    if (outParam)
                    {
                        Release(comInterface);
                    }

                    return(managedInterface);
                }
                else
                {
                    return(null);
                }
            }
Beispiel #9
0
            private void Dispose()
            {
                if (gcHandle.IsAllocated)
                {
                    gcHandle.Free();
                }

                if (comInterface != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(comInterface);
                    comInterface = IntPtr.Zero;
                }

                managedInterface = null;

                lock (managedVtable)
                {
                    // Dispose vtable when shutting down
                    if (--vtableRefCount == 0 && Environment.HasShutdownStarted)
                    {
                        DisposeVtable();
                    }
                }
            }
			public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
			{
				ThrowExceptionForHR(managedVtable.CopyTo(comInterface, pstm, cb, pcbRead, pcbWritten));
			}
Beispiel #11
0
 public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
 {
     ThrowExceptionForHR(managedVtable.CopyTo(comInterface, pstm, cb, pcbRead, pcbWritten));
 }
Beispiel #12
0
		public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
		{
			byte[] buffer;
			long written = 0;
			int read;
			int count;

			if (cb != 0)
			{
				if (cb < 4096)
					count = (int)cb;
				else
					count = 4096;
				buffer = new byte[count];
				SetSizeToPosition();
				while (true)
				{
					if ((read = baseStream.Read(buffer, 0, count)) == 0)
						break;
					pstm.Write(buffer, read, IntPtr.Zero);
					written += read;
					if (written >= cb)
						break;
					if (cb - written < 4096)
						count = (int)(cb - written);
				}
			}

			if (pcbRead != IntPtr.Zero)
				Marshal.WriteInt64(pcbRead, written);
			if (pcbWritten != IntPtr.Zero)
				Marshal.WriteInt64(pcbWritten, written);
		}
Beispiel #13
0
		public void Clone(out IStream ppstm)
		{
			ppstm = null;
			throw new ExternalException(null, STG_E_INVALIDFUNCTION);
		}
			private ManagedToNativeWrapper(IStream managedInterface)
			{
				IStreamInterface newInterface;

				lock (managedVtable)
				{
					// Vtable may have been disposed when shutting down
					if (vtableRefCount == 0 && comVtable == IntPtr.Zero)
						CreateVtable();
					vtableRefCount++;
				}

				try
				{
					this.managedInterface = managedInterface;
					gcHandle = GCHandle.Alloc(this);

					newInterface = new IStreamInterface();
					newInterface.lpVtbl = comVtable;
					newInterface.gcHandle = (IntPtr)gcHandle;
					comInterface = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamInterface)));
					Marshal.StructureToPtr(newInterface, comInterface, false);
				}
				catch
				{
					this.Dispose();
					throw;
				}
			}
			public void Clone(out IStream ppstm)
			{
				IntPtr newInterface;

				ThrowExceptionForHR(managedVtable.Clone(comInterface, out newInterface));
				ppstm = NativeToManagedWrapper.GetInterface(newInterface, true);
			}
			private void Dispose()
			{
				if (gcHandle.IsAllocated)
					gcHandle.Free();

				if (comInterface != IntPtr.Zero)
				{
					Marshal.FreeHGlobal(comInterface);
					comInterface = IntPtr.Zero;
				}

				managedInterface = null;

				lock (managedVtable)
				{
					// Dispose vtable when shutting down
					if (--vtableRefCount == 0 && Environment.HasShutdownStarted)
						DisposeVtable();
				}
			}
Beispiel #17
0
 public void Clone(out IStream ppstm)
 {
     ppstm = null;
     throw new ExternalException(null, STG_E_INVALIDFUNCTION);
 }
			internal static void ReleaseInterface(IStream managedInterface)
			{
				if (managedInterface is NativeToManagedWrapper)
					((NativeToManagedWrapper)managedInterface).Dispose(true);
			}
			internal static IntPtr GetUnderlyingInterface(IStream managedInterface)
			{
				if (managedInterface is NativeToManagedWrapper)
				{
					NativeToManagedWrapper wrapper = (NativeToManagedWrapper)managedInterface;

					wrapper.managedVtable.AddRef(wrapper.comInterface);
					return wrapper.comInterface;
				}
				else
					return IntPtr.Zero;
			}
			private static int CopyTo(IntPtr @this, IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
			{
#if MAP_EX_TO_HR
				try
				{
#endif
					GetObject(@this).managedInterface.CopyTo(pstm, cb, pcbRead, pcbWritten);
					return S_OK;
#if MAP_EX_TO_HR
				}
				catch (Exception e)
				{
					return GetHRForException(e);
				}
#endif
			}
			internal static IntPtr GetInterface(IStream managedInterface)
			{
				IntPtr comInterface;

				if (managedInterface == null)
					return IntPtr.Zero;
#if !RECURSIVE_WRAPPING
				else if ((comInterface = NativeToManagedWrapper.GetUnderlyingInterface(managedInterface)) == IntPtr.Zero)
#endif
					comInterface = new ManagedToNativeWrapper(managedInterface).comInterface;

				return comInterface;
			}