Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LOWORD and HIWORD Macros for Extracting Low-Order and High-Order Words from a 32-Bit Value #1300

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/PInvokeClassMacros.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ internal class PInvokeClassMacros
/// <param name="h">The high word.</param>
/// <returns>The LRESULT value.</returns>
internal static global::Windows.Win32.Foundation.LRESULT MAKELRESULT(ushort l, ushort h) => unchecked((global::Windows.Win32.Foundation.LRESULT)(nint)MAKELONG(l, h));

/// <summary>
/// Retrieves the low-order word from the specified 32-bit value.
/// </summary>
/// <param name="value">The 32-bit value.</param>
/// <returns>The low-order word.</returns>
internal static ushort LOWORD(uint value) => (ushort)(value & 0xFFFF);

/// <summary>
/// Retrieves the high-order word from the specified 32-bit value.
/// </summary>
/// <param name="value">The 32-bit value.</param>
/// <returns>The high-order word.</returns>
internal static ushort HIWORD(uint value) => (ushort)(value >> 16);
}
26 changes: 26 additions & 0 deletions test/GenerationSandbox.Tests/MacroTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ public void MAKELRESULTTest()
Assert.Equal((LRESULT)(nint)0xFFFFFFFF, MAKELRESULT(0xFFFF, 0xFFFF));
}
}

[Fact]
public void LOWORDTest()
{
Assert.Equal((ushort)0x0000, LOWORD(0x00000000u));
Assert.Equal((ushort)0x0001, LOWORD(0x00010001u));
Assert.Equal((ushort)0x0000, LOWORD(0xFFFF0000u));
Assert.Equal((ushort)0x1234, LOWORD(0x56781234u));
Assert.Equal((ushort)0xFFFF, LOWORD(0xFFFFFFFFu));
Assert.Equal((ushort)0xABCD, LOWORD(0x1234ABCDu));
Assert.Equal((ushort)0x0000, LOWORD(0xFFFF0000u));
Assert.Equal((ushort)0x8000, LOWORD(0xFFFF8000u));
}

[Fact]
public void HIWORDTest()
{
Assert.Equal((ushort)0x0000, HIWORD(0x00000000u));
Assert.Equal((ushort)0x0001, HIWORD(0x00010001u));
Assert.Equal((ushort)0xFFFF, HIWORD(0xFFFF0000u));
Assert.Equal((ushort)0x5678, HIWORD(0x56781234u));
Assert.Equal((ushort)0xFFFF, HIWORD(0xFFFFFFFFu));
Assert.Equal((ushort)0x1234, HIWORD(0x1234ABCDu));
Assert.Equal((ushort)0xFFFF, HIWORD(0xFFFF1234u));
Assert.Equal((ushort)0x8000, HIWORD(0x80000000u));
}
}
2 changes: 2 additions & 0 deletions test/GenerationSandbox.Tests/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ GetTickCount
GetWindowText
GetWindowTextLength
HDC_UserSize
HIWORD
HRESULT_FROM_WIN32
IDirectorySearch
IEnumDebugPropertyInfo
Expand All @@ -24,6 +25,7 @@ IShellWindows
IStream
KEY_EVENT_RECORD
LoadLibrary
LOWORD
MainAVIHeader
MAKELPARAM
MAKELRESULT
Expand Down
17 changes: 15 additions & 2 deletions test/Microsoft.Windows.CsWin32.Tests/MacrosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ public void MacroAPIsGenerateWithAppropriateVisibility(bool publicVisibility)
Assert.True(this.generator.TryGenerate("MAKELONG", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
var method = Assert.Single(this.FindGeneratedMethod("MAKELONG"));
var makelongMethod = Assert.Single(this.FindGeneratedMethod("MAKELONG"));
Assert.True(makelongMethod.Modifiers.Any(publicVisibility ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword));

Assert.True(method.Modifiers.Any(publicVisibility ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword));
this.generator = this.CreateGenerator(DefaultTestGeneratorOptions with { Public = publicVisibility });
Assert.True(this.generator.TryGenerate("LOWORD", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
var lowordMethod = Assert.Single(this.FindGeneratedMethod("LOWORD"));
Assert.True(lowordMethod.Modifiers.Any(publicVisibility ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword));

this.generator = this.CreateGenerator(DefaultTestGeneratorOptions with { Public = publicVisibility });
Assert.True(this.generator.TryGenerate("HIWORD", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
var hiwordMethod = Assert.Single(this.FindGeneratedMethod("HIWORD"));
Assert.True(hiwordMethod.Modifiers.Any(publicVisibility ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword));
}

[Theory]
Expand Down