Add CCW for an IStream object wrapping a Stream
このコミットが含まれているのは:
1 親
10876946df
コミット
b93a4a2a5b
13ファイル変更
+835
-1
検索条件に一致するファイルはありません
@@ -36,7 +36,7 @@ internal static class StreamExtensions
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance.</param>
|
||||
/// <param name="span">A region of memory. This method copies the contents of this region to the current stream.</param>
|
||||
public static void Write(this Stream stream, Span<byte> span)
|
||||
public static void Write(this Stream stream, ReadOnlySpan<byte> span)
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/coml2api.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
// TODO: remove this once TerraFX.Interop.Windows is updated
|
||||
internal static partial class STGM
|
||||
{
|
||||
public const int STGM_READ = 0x00000000;
|
||||
|
||||
public const int STGM_WRITE = 0x00000001;
|
||||
|
||||
public const int STGM_READWRITE = 0x00000002;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,585 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop.Windows;
|
||||
using static TerraFX.Interop.Windows.E;
|
||||
using static TerraFX.Interop.Windows.IID;
|
||||
using static TerraFX.Interop.Windows.S;
|
||||
using static TerraFX.Interop.Windows.STATFLAG;
|
||||
using static TerraFX.Interop.Windows.STG;
|
||||
using static TerraFX.Interop.Windows.STGM;
|
||||
using static TerraFX.Interop.Windows.STGTY;
|
||||
#if NET6_0_OR_GREATER
|
||||
using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers;
|
||||
#else
|
||||
using RuntimeHelpers = ComputeSharp.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers;
|
||||
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.Graphics.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="IWICStream"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class IWICStreamExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes an input <see cref="IWICStream"/> wrapping a given <see cref="Stream"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="stream">The target <see cref="IWICStream"/> object to initialize.</param>
|
||||
/// <param name="source">The input <see cref="Stream"/> instance to wrap.</param>
|
||||
/// <returns>An <see cref="HRESULT"/> value indicating the operation result.</returns>
|
||||
public static HRESULT InitializeFromStream(this ref IWICStream stream, Stream source)
|
||||
{
|
||||
using ComPtr<IStreamWrapper> streamWrapper = default;
|
||||
|
||||
IStreamWrapper.Create(source, streamWrapper.GetAddressOf());
|
||||
|
||||
return stream.InitializeFromIStream((IStream*)streamWrapper.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A manual CCW implementation for an <see cref="IStream"/> object wrapping a <see cref="Stream"/> instance.
|
||||
/// </summary>
|
||||
internal unsafe partial struct IStreamWrapper
|
||||
#if NET6_0_OR_GREATER
|
||||
: IUnknown.Interface
|
||||
#endif
|
||||
{
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="QueryInterface"/>
|
||||
private delegate int QueryInterfaceDelegate(IStreamWrapper* @this, Guid* riid, void** ppvObject);
|
||||
|
||||
/// <inheritdoc cref="AddRef"/>
|
||||
private delegate uint AddRefDelegate(IStreamWrapper* @this);
|
||||
|
||||
/// <inheritdoc cref="Release"/>
|
||||
private delegate uint ReleaseDelegate(IStreamWrapper* @this);
|
||||
|
||||
/// <inheritdoc cref="Read"/>
|
||||
private delegate HRESULT ReadDelegate(IStreamWrapper* @this, void* pv, uint cb, uint* pcbRead);
|
||||
|
||||
/// <inheritdoc cref="Write"/>
|
||||
private delegate HRESULT WriteDelegate(IStreamWrapper* @this, void* pv, uint cb, uint* pcbWritten);
|
||||
|
||||
/// <inheritdoc cref="Seek"/>
|
||||
private delegate HRESULT SeekDelegate(IStreamWrapper* @this, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition);
|
||||
|
||||
/// <inheritdoc cref="SetSize"/>
|
||||
private delegate HRESULT SetSizeDelegate(IStreamWrapper* @this, ULARGE_INTEGER libNewSize);
|
||||
|
||||
/// <inheritdoc cref="CopyTo"/>
|
||||
private delegate HRESULT CopyToDelegate(IStreamWrapper* @this, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
|
||||
|
||||
/// <inheritdoc cref="Commit"/>
|
||||
private delegate HRESULT CommitDelegate(IStreamWrapper* @this, uint grfCommitFlags);
|
||||
|
||||
/// <inheritdoc cref="Revert"/>
|
||||
private delegate HRESULT RevertDelegate(IStreamWrapper* @this);
|
||||
|
||||
/// <inheritdoc cref="LockRegion"/>
|
||||
private delegate HRESULT LockRegionDelegate(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType);
|
||||
|
||||
/// <inheritdoc cref="UnlockRegion"/>
|
||||
private delegate HRESULT UnlockRegionDelegate(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType);
|
||||
|
||||
/// <inheritdoc cref="Stat"/>
|
||||
private delegate HRESULT StatDelegate(IStreamWrapper* @this, STATSTG* pstatstg, uint grfStatFlag);
|
||||
|
||||
/// <inheritdoc cref="Clone"/>
|
||||
private delegate HRESULT CloneDelegate(IStreamWrapper* @this, IStream** ppstm);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="QueryInterfaceDelegate"/> instance wrapping <see cref="QueryInterface"/>.
|
||||
/// </summary>
|
||||
private static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="AddRefDelegate"/> instance wrapping <see cref="AddRef"/>.
|
||||
/// </summary>
|
||||
private static readonly AddRefDelegate AddRefWrapper = AddRef;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release"/>.
|
||||
/// </summary>
|
||||
private static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReadDelegate"/> instance wrapping <see cref="Read"/>.
|
||||
/// </summary>
|
||||
private static readonly ReadDelegate ReadWrapper = Read;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="WriteDelegate"/> instance wrapping <see cref="Write"/>.
|
||||
/// </summary>
|
||||
private static readonly WriteDelegate WriteWrapper = Write;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="SeekDelegate"/> instance wrapping <see cref="Seek"/>.
|
||||
/// </summary>
|
||||
private static readonly SeekDelegate SeekWrapper = Seek;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="SetSizeDelegate"/> instance wrapping <see cref="SetSize"/>.
|
||||
/// </summary>
|
||||
private static readonly SetSizeDelegate SetSizeWrapper = SetSize;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="CopyToDelegate"/> instance wrapping <see cref="CopyTo"/>.
|
||||
/// </summary>
|
||||
private static readonly CopyToDelegate CopyToWrapper = CopyTo;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="CommitDelegate"/> instance wrapping <see cref="Commit"/>.
|
||||
/// </summary>
|
||||
private static readonly CommitDelegate CommitWrapper = Commit;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="RevertDelegate"/> instance wrapping <see cref="Revert"/>.
|
||||
/// </summary>
|
||||
private static readonly RevertDelegate RevertWrapper = Revert;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="LockRegionDelegate"/> instance wrapping <see cref="LockRegion"/>.
|
||||
/// </summary>
|
||||
private static readonly LockRegionDelegate LockRegionWrapper = LockRegion;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="UnlockRegionDelegate"/> instance wrapping <see cref="UnlockRegion"/>.
|
||||
/// </summary>
|
||||
private static readonly UnlockRegionDelegate UnlockRegionWrapper = UnlockRegion;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="StatDelegate"/> instance wrapping <see cref="Stat"/>.
|
||||
/// </summary>
|
||||
private static readonly StatDelegate StatWrapper = Stat;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="CloneDelegate"/> instance wrapping <see cref="Clone"/>.
|
||||
/// </summary>
|
||||
private static readonly CloneDelegate CloneWrapper = Clone;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The shared vtable pointer for <see cref="IStreamWrapper"/> instances.
|
||||
/// </summary>
|
||||
private static readonly void** Vtbl = InitVtbl();
|
||||
|
||||
/// <summary>
|
||||
/// Setups the vtable pointer for <see cref="IStreamWrapper"/>.
|
||||
/// </summary>
|
||||
/// <returns>The initialized vtable pointer for <see cref="IStreamWrapper"/>.</returns>
|
||||
private static void** InitVtbl()
|
||||
{
|
||||
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IStreamWrapper), sizeof(void*) * 14);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
lpVtbl[0] = (delegate* unmanaged<IStreamWrapper*, Guid*, void**, int>)&QueryInterface;
|
||||
lpVtbl[1] = (delegate* unmanaged<IStreamWrapper*, uint>)&AddRef;
|
||||
lpVtbl[2] = (delegate* unmanaged<IStreamWrapper*, uint>)&Release;
|
||||
lpVtbl[3] = (delegate* unmanaged<IStreamWrapper*, void*, uint, uint*, HRESULT>)&Read;
|
||||
lpVtbl[4] = (delegate* unmanaged<IStreamWrapper*, void*, uint, uint*, HRESULT>)&Write;
|
||||
lpVtbl[5] = (delegate* unmanaged<IStreamWrapper*, LARGE_INTEGER, uint, ULARGE_INTEGER*, HRESULT>)&Seek;
|
||||
lpVtbl[6] = (delegate* unmanaged<IStreamWrapper*, ULARGE_INTEGER, HRESULT>)&SetSize;
|
||||
lpVtbl[7] = (delegate* unmanaged<IStreamWrapper*, IStream*, ULARGE_INTEGER, ULARGE_INTEGER*, ULARGE_INTEGER*, HRESULT>)&CopyTo;
|
||||
lpVtbl[8] = (delegate* unmanaged<IStreamWrapper*, uint, HRESULT>)&Commit;
|
||||
lpVtbl[9] = (delegate* unmanaged<IStreamWrapper*, HRESULT>)&Revert;
|
||||
lpVtbl[10] = (delegate* unmanaged<IStreamWrapper*, ULARGE_INTEGER, ULARGE_INTEGER, uint, HRESULT>)&LockRegion;
|
||||
lpVtbl[11] = (delegate* unmanaged<IStreamWrapper*, ULARGE_INTEGER, ULARGE_INTEGER, uint, HRESULT>)&UnlockRegion;
|
||||
lpVtbl[12] = (delegate* unmanaged<IStreamWrapper*, STATSTG*, uint, HRESULT>)&Stat;
|
||||
lpVtbl[13] = (delegate* unmanaged<IStreamWrapper*, IStream**, HRESULT>)&Clone;
|
||||
#else
|
||||
lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(QueryInterfaceWrapper);
|
||||
lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(AddRefWrapper);
|
||||
lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ReleaseWrapper);
|
||||
lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(ReadWrapper);
|
||||
lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(WriteWrapper);
|
||||
lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(SeekWrapper);
|
||||
lpVtbl[6] = (void*)Marshal.GetFunctionPointerForDelegate(SetSizeWrapper);
|
||||
lpVtbl[7] = (void*)Marshal.GetFunctionPointerForDelegate(CopyToWrapper);
|
||||
lpVtbl[8] = (void*)Marshal.GetFunctionPointerForDelegate(CommitWrapper);
|
||||
lpVtbl[9] = (void*)Marshal.GetFunctionPointerForDelegate(RevertWrapper);
|
||||
lpVtbl[10] = (void*)Marshal.GetFunctionPointerForDelegate(LockRegionWrapper);
|
||||
lpVtbl[11] = (void*)Marshal.GetFunctionPointerForDelegate(UnlockRegionWrapper);
|
||||
lpVtbl[12] = (void*)Marshal.GetFunctionPointerForDelegate(StatWrapper);
|
||||
lpVtbl[13] = (void*)Marshal.GetFunctionPointerForDelegate(CloneWrapper);
|
||||
#endif
|
||||
|
||||
return lpVtbl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The vtable pointer for the current instance.
|
||||
/// </summary>
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <summary>
|
||||
/// The current reference count for the object (from <c>IUnknown</c>).
|
||||
/// </summary>
|
||||
public volatile int referenceCount;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="GCHandle"/> to the captured <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
public GCHandle streamHandle;
|
||||
|
||||
/// <summary>
|
||||
/// Creates and initializes a new <see cref="IStreamWrapper"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance to wrap.</param>
|
||||
/// <param name="streamWrapper">The target <see cref="IStreamWrapper"/> instance to initialize.</param>
|
||||
public static void Create(Stream stream, IStreamWrapper** streamWrapper)
|
||||
{
|
||||
IStreamWrapper* @this = (IStreamWrapper*)NativeMemory.Alloc((nuint)sizeof(IStreamWrapper));
|
||||
|
||||
@this->lpVtbl = Vtbl;
|
||||
@this->referenceCount = 1;
|
||||
@this->streamHandle = GCHandle.Alloc(stream);
|
||||
|
||||
*streamWrapper = @this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the captured <see cref="Stream"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>The captured <see cref="Stream"/> instance</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private Stream GetStream()
|
||||
{
|
||||
return Unsafe.As<Stream>(this.streamHandle.Target!);
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <inheritdoc/>
|
||||
public HRESULT QueryInterface(Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged<IStreamWrapper*, Guid*, void**, HRESULT>)lpVtbl[0])((IStreamWrapper*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged<IStreamWrapper*, uint>)lpVtbl[1])((IStreamWrapper*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged<IStreamWrapper*, uint>)lpVtbl[2])((IStreamWrapper*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="IStream.QueryInterface(Guid*, void**)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static int QueryInterface(IStreamWrapper* @this, Guid* riid, void** ppvObject)
|
||||
{
|
||||
if (ppvObject is null)
|
||||
{
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
if (riid->Equals(IID_IUnknown) ||
|
||||
riid->Equals(IID_ISequentialStream) ||
|
||||
riid->Equals(IID_IStream))
|
||||
{
|
||||
_ = Interlocked.Increment(ref @this->referenceCount);
|
||||
|
||||
*ppvObject = @this;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
*ppvObject = null;
|
||||
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.AddRef"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint AddRef(IStreamWrapper* @this)
|
||||
{
|
||||
return (uint)Interlocked.Increment(ref @this->referenceCount);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Release"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint Release(IStreamWrapper* @this)
|
||||
{
|
||||
uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount);
|
||||
|
||||
if (referenceCount == 0)
|
||||
{
|
||||
@this->streamHandle.Free();
|
||||
|
||||
NativeMemory.Free(@this);
|
||||
}
|
||||
|
||||
return referenceCount;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Read(void*, uint, uint*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Read(IStreamWrapper* @this, void* pv, uint cb, uint* pcbRead)
|
||||
{
|
||||
if (pv == null)
|
||||
{
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
if (cb > int.MaxValue)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (pcbRead != null)
|
||||
{
|
||||
*pcbRead = 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Span<byte> dst = new(pv, (int)cb);
|
||||
|
||||
while (dst.Length > 0)
|
||||
{
|
||||
int bytesRead = @this->GetStream().Read(dst);
|
||||
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
if (pcbRead != null)
|
||||
{
|
||||
*pcbRead += (uint)bytesRead;
|
||||
}
|
||||
|
||||
dst = dst.Slice(bytesRead);
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Write(void*, uint, uint*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Write(IStreamWrapper* @this, void* pv, uint cb, uint* pcbWritten)
|
||||
{
|
||||
if (pv == null)
|
||||
{
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
if (cb > int.MaxValue)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (pcbWritten != null)
|
||||
{
|
||||
*pcbWritten = 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@this->GetStream().Write(new ReadOnlySpan<byte>(pv, (int)cb));
|
||||
|
||||
if (pcbWritten != null)
|
||||
{
|
||||
*pcbWritten = cb;
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Seek(LARGE_INTEGER, uint, ULARGE_INTEGER*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Seek(IStreamWrapper* @this, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition)
|
||||
{
|
||||
SeekOrigin origin = (SeekOrigin)dwOrigin;
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
case SeekOrigin.Current:
|
||||
case SeekOrigin.End:
|
||||
break;
|
||||
default:
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (plibNewPosition != null)
|
||||
{
|
||||
*plibNewPosition = default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
long newPosition = @this->GetStream().Seek(dlibMove.QuadPart, origin);
|
||||
|
||||
if (plibNewPosition != null)
|
||||
{
|
||||
plibNewPosition->QuadPart = (ulong)newPosition;
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.SetSize(ULARGE_INTEGER)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT SetSize(IStreamWrapper* @this, ULARGE_INTEGER libNewSize)
|
||||
{
|
||||
if (libNewSize.QuadPart > long.MaxValue)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@this->GetStream().SetLength((long)libNewSize.QuadPart);
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.CopyTo(IStream*, ULARGE_INTEGER, ULARGE_INTEGER*, ULARGE_INTEGER*)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT CopyTo(IStreamWrapper* @this, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
|
||||
{
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Commit(uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Commit(IStreamWrapper* @this, uint grfCommitFlags)
|
||||
{
|
||||
try
|
||||
{
|
||||
@this->GetStream().Flush();
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Revert"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Revert(IStreamWrapper* @this)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT LockRegion(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT UnlockRegion(IStreamWrapper* @this, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Stat(STATSTG*, uint)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Stat(IStreamWrapper* @this, STATSTG* pstatstg, uint grfStatFlag)
|
||||
{
|
||||
if (pstatstg == null)
|
||||
{
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
*pstatstg = default;
|
||||
|
||||
STATFLAG statFlag = (STATFLAG)grfStatFlag;
|
||||
|
||||
if ((statFlag & (STATFLAG_DEFAULT | STATFLAG_NONAME | STATFLAG_NOOPEN)) != statFlag)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Stream stream = @this->GetStream();
|
||||
|
||||
if (!statFlag.HasFlag(STATFLAG_NONAME))
|
||||
{
|
||||
if (stream is FileStream asFileStream)
|
||||
{
|
||||
pstatstg->pwcsName = (ushort*)Marshal.StringToCoTaskMemUni(asFileStream.Name);
|
||||
}
|
||||
}
|
||||
|
||||
bool canRead = stream.CanRead;
|
||||
bool canWrite = stream.CanWrite;
|
||||
|
||||
if (canRead && !canWrite)
|
||||
{
|
||||
pstatstg->grfMode = STGM_READ;
|
||||
}
|
||||
else if (canRead && canWrite)
|
||||
{
|
||||
pstatstg->grfMode = STGM_READWRITE;
|
||||
}
|
||||
else if (!canRead && canWrite)
|
||||
{
|
||||
pstatstg->grfMode = STGM_WRITE;
|
||||
}
|
||||
else
|
||||
{
|
||||
ThrowHelper.ThrowNotSupportedException();
|
||||
}
|
||||
|
||||
pstatstg->type = (uint)STGTY_STREAM;
|
||||
pstatstg->cbSize.QuadPart = (ulong)stream.Length;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (pstatstg->pwcsName != null)
|
||||
{
|
||||
Marshal.FreeCoTaskMem((IntPtr)pstatstg->pwcsName);
|
||||
|
||||
pstatstg->pwcsName = null;
|
||||
}
|
||||
|
||||
return ex.HResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IStream.Clone(IStream**)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static HRESULT Clone(IStreamWrapper* @this, IStream** ppstm)
|
||||
{
|
||||
return STG_E_INVALIDFUNCTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,17 +217,25 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\NativeTypeNameAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\VtblIndexAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\WPARAM.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\minwindef\FILETIME.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\minwindef\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\uuids\Windows.Manual.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\DXGI.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\E.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\S.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\STG.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\Windows.Manual.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\wtypesbase\CLSCTX.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\wtypes\STATFLAG.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\combaseapi\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\coml2api\STGM.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\minwinbase\SECURITY_ATTRIBUTES.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\IID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\IStream.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\STATSTG.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\STGTY.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\synchapi\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\Unknwnbase\IID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\Unknwnbase\IUnknown.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\CLSID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\GUID.cs" />
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from shared/minwindef.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal partial struct FILETIME
|
||||
{
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint dwLowDateTime;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint dwHighDateTime;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from shared/winerror.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class STG
|
||||
{
|
||||
[NativeTypeName("#define STG_E_INVALIDFUNCTION _HRESULT_TYPEDEF_(0x80030001L)")]
|
||||
public const int STG_E_INVALIDFUNCTION = unchecked((int)(0x80030001));
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from shared/wtypes.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal enum STATFLAG
|
||||
{
|
||||
STATFLAG_DEFAULT = 0,
|
||||
|
||||
STATFLAG_NONAME = 1,
|
||||
|
||||
STATFLAG_NOOPEN = 2,
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/Unknwnbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class IID
|
||||
{
|
||||
public static ref readonly Guid IID_IUnknown
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0xC0,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x46
|
||||
};
|
||||
|
||||
Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/coml2api.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class STGM
|
||||
{
|
||||
[NativeTypeName("#define STGM_READ 0x00000000L")]
|
||||
public const int STGM_READ = 0x00000000;
|
||||
|
||||
[NativeTypeName("#define STGM_WRITE 0x00000001L")]
|
||||
public const int STGM_WRITE = 0x00000001;
|
||||
|
||||
[NativeTypeName("#define STGM_READWRITE 0x00000002L")]
|
||||
public const int STGM_READWRITE = 0x00000002;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/objidlbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static partial class IID
|
||||
{
|
||||
public static ref readonly Guid IID_ISequentialStream
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x30, 0x3A, 0x73, 0x0C,
|
||||
0x1C, 0x2A,
|
||||
0xCE, 0x11,
|
||||
0xAD,
|
||||
0xE5,
|
||||
0x00,
|
||||
0xAA,
|
||||
0x00,
|
||||
0x44,
|
||||
0x77,
|
||||
0x3D
|
||||
};
|
||||
|
||||
Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public static ref readonly Guid IID_IStream
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x0C, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0xC0,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x46
|
||||
};
|
||||
|
||||
Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,13 @@ namespace TerraFX.Interop.Windows
|
||||
return ((delegate* unmanaged[Stdcall]<IStream*, ULARGE_INTEGER, ULARGE_INTEGER, uint, int>)(lpVtbl[11]))((IStream*)Unsafe.AsPointer(ref this), libOffset, cb, dwLockType);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(12)]
|
||||
public HRESULT Stat(STATSTG* pstatstg, [NativeTypeName("DWORD")] uint grfStatFlag)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<IStream*, STATSTG*, uint, int>)(lpVtbl[12]))((IStream*)Unsafe.AsPointer(ref this), pstatstg, grfStatFlag);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(13)]
|
||||
public HRESULT Clone(IStream** ppstm)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/objidlbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal unsafe partial struct STATSTG
|
||||
{
|
||||
[NativeTypeName("LPOLESTR")]
|
||||
public ushort* pwcsName;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint type;
|
||||
|
||||
public ULARGE_INTEGER cbSize;
|
||||
|
||||
public FILETIME mtime;
|
||||
|
||||
public FILETIME ctime;
|
||||
|
||||
public FILETIME atime;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint grfMode;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint grfLocksSupported;
|
||||
|
||||
[NativeTypeName("CLSID")]
|
||||
public Guid clsid;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint grfStateBits;
|
||||
|
||||
[NativeTypeName("DWORD")]
|
||||
public uint reserved;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/objidlbase.h in the Windows SDK for Windows 10.0.20348.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal enum STGTY
|
||||
{
|
||||
STGTY_STORAGE = 1,
|
||||
|
||||
STGTY_STREAM = 2,
|
||||
|
||||
STGTY_LOCKBYTES = 3,
|
||||
|
||||
STGTY_PROPERTY = 4,
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする