Skip to content

Commit

Permalink
Make Clock work with NativeAOT
Browse files Browse the repository at this point in the history
Removes Marshal.GetHINSTANCE call from WindowClass
  • Loading branch information
JeremyKuhne committed Jul 18, 2021
1 parent fbc5502 commit a95a94d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/Samples/Petzold/5th/Clock/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#define GDIPLUS
// #define GDIPLUS

#if GDIPLUS
using WInterop.GdiPlus;
Expand Down Expand Up @@ -34,9 +34,9 @@ private static void Main()
internal class Clock : WindowClass
{
#if GDIPLUS
private readonly GdiPlusPen _blackPen = new GdiPlusPen(Color.Black);
private readonly GdiPlusBrush _blackBrush = new GdiPlusBrush(Color.Black);
private readonly GdiPlusBrush _whiteBrush = new GdiPlusBrush(Color.White);
private readonly GdiPlusPen _blackPen = new(Color.Black);
private readonly GdiPlusBrush _blackBrush = new(Color.Black);
private readonly GdiPlusBrush _whiteBrush = new(Color.White);
#endif

private void SetIsotropic(DeviceContext hdc)
Expand All @@ -47,7 +47,7 @@ private void SetIsotropic(DeviceContext hdc)
hdc.SetViewportOrigin(new Point(_clientSize.Width / 2, _clientSize.Height / 2));
}

private void RotatePoint(Point[] pt, int iNum, int iAngle)
private static void RotatePoint(Point[] pt, int iNum, int iAngle)
{
for (int i = 0; i < iNum; i++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>N:\repos\winterop\bin\AnyCPU\Release\Clock\net5.0-windows\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishReadyToRun>True</PublishReadyToRun>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Tests/WInterop.Tests/Com/MarshallingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public unsafe void Unknown_RoundTrip()
StorageMode mode = StorageMode.ReadWrite | StorageMode.Create | StorageMode.ShareExclusive,
StorageFormat format = StorageFormat.DocFile)
{
STGOPTIONS options = new STGOPTIONS
STGOPTIONS options = new()
{
usVersion = 1,

Expand Down
18 changes: 10 additions & 8 deletions src/WInterop.Desktop/Modules/Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ public static ModuleInstance GetModuleHandle(IntPtr address)
return new ModuleInstance(handle);
}

/// <summary>
/// Gets the module handle for the launching executable.
/// </summary>
public static ModuleInstance GetExeModuleHandle()
=> new(GetModuleHandleHelper(moduleName: null, GetModuleFlags.UnchangedRefCount));

/// <summary>
/// Gets the specified module handle without increasing the ref count.
/// </summary>
public static ModuleInstance GetModuleHandle(string moduleName)
{
return new ModuleInstance(GetModuleHandleHelper(moduleName, GetModuleFlags.UnchangedRefCount));
}
=> new(GetModuleHandleHelper(moduleName, GetModuleFlags.UnchangedRefCount));

/// <summary>
/// Gets a module handle and pins the module so it can't be unloaded until the process exits.
/// </summary>
public static ModuleInstance GetModuleHandleAndPin(string moduleName)
{
return new ModuleInstance(GetModuleHandleHelper(moduleName, GetModuleFlags.Pin));
}
=> new(GetModuleHandleHelper(moduleName, GetModuleFlags.Pin));

/// <summary>
/// Gets a ref counted module handle for the specified module.
Expand All @@ -55,7 +57,7 @@ public static ModuleInstance GetRefCountedModuleHandle(string moduleName)
return new RefCountedModuleInstance(GetModuleHandleHelper(moduleName, 0));
}

private static unsafe IntPtr GetModuleHandleHelper(string moduleName, GetModuleFlags flags)
private static unsafe IntPtr GetModuleHandleHelper(string? moduleName, GetModuleFlags flags)
{
IntPtr GetHandle(IntPtr n, GetModuleFlags f)
{
Expand All @@ -65,7 +67,7 @@ IntPtr GetHandle(IntPtr n, GetModuleFlags f)
return handle;
}

if (moduleName == null)
if (moduleName is null)
return GetHandle(IntPtr.Zero, flags);

fixed (void* name = moduleName)
Expand Down
2 changes: 1 addition & 1 deletion src/WInterop.Desktop/Modules/Native/Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static extern IntPtr GetProcAddress(
ModuleInstance hModule,
[MarshalAs(UnmanagedType.LPStr)] string methodName);

// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683200.aspx
// https://docs.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexw
[DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
public static unsafe extern bool GetModuleHandleExW(
GetModuleFlags dwFlags,
Expand Down
6 changes: 3 additions & 3 deletions src/WInterop.Desktop/WInterop.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
</PropertyGroup>
<Import Project="..\Common\Common.projitems" Label="Shared" />
<ItemGroup>
<Compile Remove="Modules\BufferWrappers\**" />
<Compile Remove="ProcessAndThreads\BufferWrappers\**" />
<Compile Remove="Storage\BufferWrappers\**" />
<Compile Remove="Windows\BufferWrappers\**" />
<EmbeddedResource Remove="Modules\BufferWrappers\**" />
<EmbeddedResource Remove="ProcessAndThreads\BufferWrappers\**" />
<EmbeddedResource Remove="Storage\BufferWrappers\**" />
<EmbeddedResource Remove="Windows\BufferWrappers\**" />
<None Remove="Modules\BufferWrappers\**" />
<None Remove="ProcessAndThreads\BufferWrappers\**" />
<None Remove="Storage\BufferWrappers\**" />
<None Remove="Windows\BufferWrappers\**" />
Expand All @@ -42,7 +45,4 @@
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Modules\BufferWrappers\" />
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions src/WInterop.Desktop/Windows/Classes/WindowClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using WInterop.Errors;
using WInterop.Gdi;
Expand Down Expand Up @@ -56,7 +54,7 @@ public unsafe WindowClass(
else if (cursor == CursorHandle.NoCursor)
cursor = default;

moduleInstance ??= new ModuleInstance(Marshal.GetHINSTANCE(Assembly.GetCallingAssembly().Modules.First()));
moduleInstance ??= Modules.Modules.GetExeModuleHandle();

if (menuId != 0 && menuName != null)
throw new ArgumentException($"Can't set both {nameof(menuName)} and {nameof(menuId)}.");
Expand Down
6 changes: 3 additions & 3 deletions src/WInterop.Desktop/Windows/Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace WInterop.Windows
public static partial class Windows
{
public static Rectangle DefaultBounds
=> new Rectangle(
=> new(
WindowDefines.CW_USEDEFAULT,
WindowDefines.CW_USEDEFAULT,
WindowDefines.CW_USEDEFAULT,
Expand Down Expand Up @@ -411,7 +411,7 @@ public static unsafe string GetWindowText<T>(this T window, int initialBuffer =
}
} while (result == buffer.Length - 1);

string text = new string(buffer, 0, result);
string text = new(buffer, 0, result);
ArrayPool<char>.Shared.Return(buffer);
return text;
}
Expand Down Expand Up @@ -848,7 +848,7 @@ public static unsafe ExtendedMonitorInfo GetExtendedMonitorInfo(this MonitorHand

public static unsafe DllVersionInfo GetCommonControlsVersion()
{
DllVersionInfo info = new DllVersionInfo { Size = (uint)sizeof(DllVersionInfo) };
DllVersionInfo info = new() { Size = (uint)sizeof(DllVersionInfo) };
WindowsImports.ComctlGetVersion(ref info).ThrowIfFailed();
return info;
}
Expand Down

0 comments on commit a95a94d

Please sign in to comment.