Skip to content

Commit

Permalink
Update packages and add some simple WIC component enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyKuhne committed Jul 19, 2024
1 parent f236f3a commit 63bf46b
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/thirtytwo/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,7 @@ public static string GetUserDefaultLocaleName()
public static Direct2dFactory Direct2dFactory => s_direct2dFactory ??= new();
public static DirectWriteFactory DirectWriteFactory => s_directWriteFactory ??= new();
public static DirectWriteGdiInterop DirectWriteGdiInterop => s_directWriteGdiInterop ??= new();

/// <inheritdoc cref="Windows.Win32.Graphics.Imaging.ImagingFactory"/>
public static ImagingFactory ImagingFactory => s_imagingFactory ??= new();
}
2 changes: 1 addition & 1 deletion src/thirtytwo/Controls/ActiveXControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ActiveXControl(
_classId = classId;
IUnknown* unknown = ComHelpers.CreateComClass(classId);
_instance = new(unknown, takeOwnership: true);
_instanceAsActiveObject = unknown->QueryAgileInterface<IOleInPlaceActiveObject>();
_instanceAsActiveObject = unknown->TryQueryAgileInterface<IOleInPlaceActiveObject>();

using ComScope<IOleObject> oleObject = ComScope<IOleObject>.QueryFrom(unknown);
if (oleObject.Pointer->GetMiscStatus(DVASPECT.DVASPECT_CONTENT, out OLEMISC status).Succeeded)
Expand Down
3 changes: 3 additions & 0 deletions src/thirtytwo/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ IsWindowEnabled
IsWindowVisible
IUIAutomationElement
IUnknown
IWICBitmapDecoderInfo
IWICComponentInfo
IWICImagingFactory2
KEY_INFORMATION_CLASS
KEY_NAME_INFORMATION
Expand Down Expand Up @@ -386,6 +388,7 @@ UpdateWindow
USER_DEFAULT_SCREEN_DPI
VARIANT_*
VIRTUAL_KEY
WICComponentEnumerateOptions
WIN32_ERROR
WINDOWPOS
WM_*
Expand Down
3 changes: 3 additions & 0 deletions src/thirtytwo/Win32/Graphics/Imaging/BitmapDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Windows.Win32.Graphics.Imaging;

/// <summary>
/// WIC Bitmap Decoder.
/// </summary>
public unsafe class BitmapDecoder : DirectDrawBase<IWICBitmapDecoder>
{
public BitmapDecoder(IWICBitmapDecoder* pointer) : base(pointer) { }
Expand Down
51 changes: 51 additions & 0 deletions src/thirtytwo/Win32/Graphics/Imaging/ComponentEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Windows.Win32.System.Com;

namespace Windows.Win32.Graphics.Imaging;

public unsafe class ComponentEnumerator : DirectDrawBase<IEnumUnknown>
{
public ComponentEnumerator(IEnumUnknown* pointer) : base(pointer) { }

public ComponentEnumerator(WICComponentType componentType)
: base(CreateComponentEnumerator(Application.ImagingFactory, componentType))
{
}

public static IEnumUnknown* CreateComponentEnumerator(ImagingFactory factory, WICComponentType componentType)
{
IEnumUnknown* enumerator;
factory.Pointer->CreateComponentEnumerator(
(uint)componentType,
(uint)WICComponentEnumerateOptions.WICComponentEnumerateDefault,
&enumerator).ThrowOnFailure();

GC.KeepAlive(factory);
return enumerator;
}

public static implicit operator IEnumUnknown*(ComponentEnumerator d) => d.Pointer;

public bool Next([NotNullWhen(true)] out ComponentInfo? componentInfo)
{
componentInfo = null;
IEnumUnknown* enumerator = this;
if (enumerator is null)
{
return false;
}

uint fetched;
using ComScope<IUnknown> unknown = new(null);
HRESULT result = enumerator->Next(1, unknown, &fetched);
if (result != HRESULT.S_OK || fetched != 1)
{
return false;
}

componentInfo = new(unknown.Pointer->QueryInterface<IWICComponentInfo>());
return true;
}
}
36 changes: 36 additions & 0 deletions src/thirtytwo/Win32/Graphics/Imaging/ComponentInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Windows.Win32.Graphics.Imaging;

public unsafe class ComponentInfo : DirectDrawBase<IWICComponentInfo>
{
public ComponentInfo(IWICComponentInfo* pointer) : base(pointer) { }

public ComponentInfo(Guid componentClassId)
: base(CreateComponentInfo(Application.ImagingFactory, componentClassId))
{
}

public static IWICComponentInfo* CreateComponentInfo(ImagingFactory factory, Guid componentClassId)
{
IWICComponentInfo* info;
factory.Pointer->CreateComponentInfo(&componentClassId, &info).ThrowOnFailure();
GC.KeepAlive(factory);
return info;
}

public string FriendlyName
{
get
{
uint length;
Pointer->GetFriendlyName(0, null, &length).ThrowOnFailure();
char* name = stackalloc char[(int)length];
Pointer->GetFriendlyName(length, name, &length).ThrowOnFailure();
return new string(name);
}
}

public static implicit operator IWICComponentInfo*(ComponentInfo d) => d.Pointer;
}
3 changes: 3 additions & 0 deletions src/thirtytwo/Win32/Graphics/Imaging/ImagingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Windows.Win32.Graphics.Imaging;

/// <summary>
/// WIC Imaging Factory.
/// </summary>
public unsafe class ImagingFactory : DirectDrawBase<IWICImagingFactory2>
{
public ImagingFactory() : base(Create()) { }
Expand Down
13 changes: 10 additions & 3 deletions src/thirtytwo/Win32/System/Com/IUnknown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ namespace Windows.Win32.System.Com;

public unsafe partial struct IUnknown : IVTable<IUnknown, IUnknown.Vtbl>
{
public TInterface* QueryInterface<TInterface>() where TInterface : unmanaged, IComIID
public TInterface* TryQueryInterface<TInterface>() where TInterface : unmanaged, IComIID
{
TInterface* @interface = default;
QueryInterface(IID.Get<TInterface>(), (void**)&@interface);
return @interface;
}

public AgileComPointer<TInterface>? QueryAgileInterface<TInterface>()
public TInterface* QueryInterface<TInterface>() where TInterface : unmanaged, IComIID
{
TInterface* @interface = default;
QueryInterface(IID.Get<TInterface>(), (void**)&@interface).ThrowOnFailure();
return @interface;
}

public AgileComPointer<TInterface>? TryQueryAgileInterface<TInterface>()
where TInterface : unmanaged, IComIID
{
TInterface* @interface = QueryInterface<TInterface>();
TInterface* @interface = TryQueryInterface<TInterface>();
return @interface is null ? null : new(@interface, takeOwnership: true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/thirtytwo/thirtytwo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.85-beta">
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
18 changes: 18 additions & 0 deletions src/thirtytwo_tests/Win32/Graphics/Imaging/ComponentInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Windows.Win32.Graphics.Imaging;

public class ComponentInfoTests
{
[Fact]
public void EnumerateDecoders()
{
ComponentEnumerator enumerator = new(WICComponentType.WICDecoder);
while (enumerator.Next(out ComponentInfo? info))
{
Assert.NotNull(info);
Assert.NotNull(info.FriendlyName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void StandardDispatch_ImplDoesNotProvideEx()
&standard).ThrowOnFailure();

// StdDispatch does not provide an implementation of IDispatchEx.
IDispatchEx* dispatchEx = standard->QueryInterface<IDispatchEx>();
IDispatchEx* dispatchEx = standard->TryQueryInterface<IDispatchEx>();
Assert.True(dispatchEx is null);

standard->Release();
Expand Down
10 changes: 5 additions & 5 deletions src/thirtytwo_tests/thirtytwo_tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 63bf46b

Please sign in to comment.