From 57b2c23c1c919eeb5379a1d84fdd2d8309c7e3be Mon Sep 17 00:00:00 2001 From: David Hall Date: Sat, 20 Jul 2019 10:29:06 -0600 Subject: [PATCH] Completed unit testing and fixes for UtilApiSet.h --- PInvoke/Kernel32/UtilApiSet.cs | 28 ++++++++----- UnitTests/PInvoke/Kernel32/Kernel32.csproj | 1 + UnitTests/PInvoke/Kernel32/UtilApiSetTests.cs | 58 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 UnitTests/PInvoke/Kernel32/UtilApiSetTests.cs diff --git a/PInvoke/Kernel32/UtilApiSet.cs b/PInvoke/Kernel32/UtilApiSet.cs index 6d67fc79..e57dcc64 100644 --- a/PInvoke/Kernel32/UtilApiSet.cs +++ b/PInvoke/Kernel32/UtilApiSet.cs @@ -6,10 +6,12 @@ namespace Vanara.PInvoke public static partial class Kernel32 { /// - /// Generates simple tones on the speaker. The function is synchronous; it performs an alertable wait and does not return control to its caller until the - /// sound finishes. + /// Generates simple tones on the speaker. The function is synchronous; it performs an alertable wait and does not return control to + /// its caller until the sound finishes. /// - /// The frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF). + /// + /// The frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF). + /// /// The duration of the sound, in milliseconds. /// /// If the function succeeds, the return value is nonzero. @@ -31,8 +33,8 @@ namespace Vanara.PInvoke /// /// - /// [Some information relates to pre-released product which may be substantially modified before it's commercially released. Microsoft makes no - /// warranties, express or implied, with respect to the information provided here.] + /// [Some information relates to pre-released product which may be substantially modified before it's commercially released. + /// Microsoft makes no warranties, express or implied, with respect to the information provided here.] /// /// Decodes a pointer in a specified process that was previously encoded with EncodePointer or EncodeRemotePointer. /// @@ -41,7 +43,7 @@ namespace Vanara.PInvoke /// The decoded pointer. /// Returns S_OK if successful, otherwise the function failed. // HRESULT WINAPI DecodeRemotePointer( _In_ HANDLE ProcessHandle, _In_opt_ PVOID Ptr, _Out_ PVOID * DecodedPtr ); https://msdn.microsoft.com/en-us/library/dn877133(v=vs.85).aspx - [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib.KernelBase, SetLastError = false, ExactSpelling = true)] [PInvokeData("UtilApiSet.h", MSDNShortId = "dn877133")] public static extern HRESULT DecodeRemotePointer(HPROCESS ProcessHandle, IntPtr Ptr, out IntPtr DecodedPtr); @@ -63,22 +65,26 @@ namespace Vanara.PInvoke /// /// - /// [Some information relates to pre-released product which may be substantially modified before it's commercially released. Microsoft makes no - /// warranties, express or implied, with respect to the information provided here.] + /// [Some information relates to pre-released product which may be substantially modified before it's commercially released. + /// Microsoft makes no warranties, express or implied, with respect to the information provided here.] + /// + /// + /// Encodes the specified pointer of the specified process. Encoded pointers can be used to provide another layer of protection for + /// pointer values. /// - /// Encodes the specified pointer of the specified process. Encoded pointers can be used to provide another layer of protection for pointer values. /// /// Handle to the remote process that owns the pointer. /// The pointer to be encoded. /// The encoded pointer. /// Returns S_OK if successful, otherwise the function failed. // HRESULT WINAPI EncodeRemotePointer( _In_ HANDLE ProcessHandle, _In_opt_ PVOID Ptr, _Out_ PVOID * EncodedPtr ); https://msdn.microsoft.com/en-us/library/dn877135(v=vs.85).aspx - [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib.KernelBase, SetLastError = false, ExactSpelling = true)] [PInvokeData("UtilApiSet.h", MSDNShortId = "dn877135")] public static extern HRESULT EncodeRemotePointer(HPROCESS ProcessHandle, IntPtr Ptr, out IntPtr EncodedPtr); /// - /// Encodes the specified pointer with a system-specific value. Encoded pointers can be used to provide another layer of protection for pointer values. + /// Encodes the specified pointer with a system-specific value. Encoded pointers can be used to provide another layer of protection + /// for pointer values. /// /// The system pointer to be encoded. /// The function returns the encoded pointer. diff --git a/UnitTests/PInvoke/Kernel32/Kernel32.csproj b/UnitTests/PInvoke/Kernel32/Kernel32.csproj index a1e30b12..8485c8ab 100644 --- a/UnitTests/PInvoke/Kernel32/Kernel32.csproj +++ b/UnitTests/PInvoke/Kernel32/Kernel32.csproj @@ -48,6 +48,7 @@ + diff --git a/UnitTests/PInvoke/Kernel32/UtilApiSetTests.cs b/UnitTests/PInvoke/Kernel32/UtilApiSetTests.cs new file mode 100644 index 00000000..d915fb4a --- /dev/null +++ b/UnitTests/PInvoke/Kernel32/UtilApiSetTests.cs @@ -0,0 +1,58 @@ +using NUnit.Framework; +using System; +using Vanara.InteropServices; +using static Vanara.PInvoke.Kernel32; + +namespace Vanara.PInvoke.Tests +{ + [TestFixture] + public class UtilApiSetTests + { + [Test] + public void BeepTest() + { + Assert.That(Beep(523, 500), Is.True); + Assert.That(Beep(587, 500), Is.True); + Assert.That(Beep(659, 500), Is.True); + } + + [Test] + public void EncodePointerTest() + { + Assert.That(() => + { + var pint = new PinnedObject(123); + var eptr = EncodePointer(pint); + Assert.That(eptr, Is.Not.EqualTo(IntPtr.Zero)); + var dptr = DecodePointer(eptr); + Assert.That((IntPtr)pint, Is.EqualTo(dptr)); + }, Throws.Nothing); + } + + [Test] + public void EncodeRemotePointerTest() + { + Assert.That(() => + { + var pint = new PinnedObject(123); + Assert.That(EncodeRemotePointer(GetCurrentProcess(), pint, out var eptr), ResultIs.Successful); + Assert.That(eptr, Is.Not.EqualTo(IntPtr.Zero)); + Assert.That(DecodeRemotePointer(GetCurrentProcess(), eptr, out var dptr), ResultIs.Successful); + Assert.That((IntPtr)pint, Is.EqualTo(dptr)); + }, Throws.Nothing); + } + + [Test] + public void EncodeSystemPointerTest() + { + Assert.That(() => + { + var pint = new PinnedObject(123); + var eptr = EncodeSystemPointer(pint); + Assert.That(eptr, Is.Not.EqualTo(IntPtr.Zero)); + var dptr = DecodeSystemPointer(eptr); + Assert.That((IntPtr)pint, Is.EqualTo(dptr)); + }, Throws.Nothing); + } + } +} \ No newline at end of file