Files
Chronos/src/Titano.Core.Windows/Interop/ShellStream.cs
T
Claude 0e6c15cb13 feat(windows): add shared COM, window and shell-stream interop helpers
Win32Com joins threads to the MTA on demand, creates in-process COM objects
and releases wrappers deterministically; PropVariant (x64 layout) with
PropVariantClear; Win32Window validates HWNDs; ShellStream opens files through
SHCreateStreamOnFileEx with STGM_SHARE_DENY_NONE so readers never block writers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:46:02 +00:00

28 lines
1.2 KiB
C#

using System.Runtime.InteropServices;
using Titano.Core.IO;
namespace Titano.Core.Windows.Interop;
/// <summary>IStream over a file through <c>SHCreateStreamOnFileEx</c>.</summary>
internal static partial class ShellStream
{
private const uint STGM_READ = 0x0;
private const uint STGM_SHARE_DENY_NONE = 0x40;
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
/// <summary>
/// Opens <paramref name="path"/> read-only while letting other processes read and write it (FileShare.ReadWrite),
/// so a camera tether or a copy in progress is never blocked. Returns an owned IStream pointer the caller must
/// <see cref="Marshal.Release"/>. Failures surface as the matching .NET exception (FileNotFoundException, …).
/// </summary>
public static nint OpenRead(string path)
{
var hr = SHCreateStreamOnFileEx(LongPath.Normalize(path), STGM_READ | STGM_SHARE_DENY_NONE, FILE_ATTRIBUTE_NORMAL, 0, 0, out var stream);
Marshal.ThrowExceptionForHR(hr);
return stream;
}
[LibraryImport("shlwapi.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial int SHCreateStreamOnFileEx(string file, uint mode, uint attributes, int create, nint template, out nint stream);
}