mirror of https://github.com/dahall/Vanara.git
Completed winbase.h unit tests and fixes for ATOM based functions
parent
5f0e30daa2
commit
e3ce94ff0c
|
@ -9,10 +9,12 @@ namespace Vanara.PInvoke
|
|||
/// <param name="lpString">
|
||||
/// <para>Type: <c>LPCTSTR</c></para>
|
||||
/// <para>
|
||||
/// The null-terminated string to be added. The string can have a maximum size of 255 bytes. Strings differing only in case are considered identical. The
|
||||
/// case of the first string added is preserved and returned by the <c>GetAtomName</c> function.
|
||||
/// The null-terminated string to be added. The string can have a maximum size of 255 bytes. Strings differing only in case are
|
||||
/// considered identical. The case of the first string added is preserved and returned by the <c>GetAtomName</c> function.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.
|
||||
/// </para>
|
||||
/// <para>Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.</para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>Type: <c>ATOM</c></para>
|
||||
|
@ -22,11 +24,11 @@ namespace Vanara.PInvoke
|
|||
// ATOM WINAPI AddAtom( _In_ LPCTSTR lpString); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649056(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649056")]
|
||||
public static extern ushort AddAtom(string lpString);
|
||||
public static extern ATOM AddAtom(string lpString);
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the reference count of a local string atom. If the atom's reference count is reduced to zero, <c>DeleteAtom</c> removes the string
|
||||
/// associated with the atom from the local atom table.
|
||||
/// Decrements the reference count of a local string atom. If the atom's reference count is reduced to zero, <c>DeleteAtom</c>
|
||||
/// removes the string associated with the atom from the local atom table.
|
||||
/// </summary>
|
||||
/// <param name="nAtom">
|
||||
/// <para>Type: <c>ATOM</c></para>
|
||||
|
@ -40,13 +42,15 @@ namespace Vanara.PInvoke
|
|||
// ATOM WINAPI DeleteAtom( _In_ ATOM nAtom); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649057(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649057")]
|
||||
public static extern ushort DeleteAtom(ushort nAtom);
|
||||
public static extern ATOM DeleteAtom(ATOM nAtom);
|
||||
|
||||
/// <summary>Searches the local atom table for the specified character string and retrieves the atom associated with that string.</summary>
|
||||
/// <param name="lpString">
|
||||
/// <para>Type: <c>LPCTSTR</c></para>
|
||||
/// <para>The character string for which to search.</para>
|
||||
/// <para>Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See Remarks for more information.</para>
|
||||
/// <para>
|
||||
/// Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See Remarks for more information.
|
||||
/// </para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>Type: <c>ATOM</c></para>
|
||||
|
@ -56,7 +60,7 @@ namespace Vanara.PInvoke
|
|||
// ATOM WINAPI FindAtom( _In_ LPCTSTR lpString); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649058(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649058")]
|
||||
public static extern ushort FindAtom(string lpString);
|
||||
public static extern ATOM FindAtom(string lpString);
|
||||
|
||||
/// <summary>Retrieves a copy of the character string associated with the specified local atom.</summary>
|
||||
/// <param name="nAtom">
|
||||
|
@ -74,14 +78,15 @@ namespace Vanara.PInvoke
|
|||
/// <returns>
|
||||
/// <para>Type: <c>UINT</c></para>
|
||||
/// <para>
|
||||
/// If the function succeeds, the return value is the length of the string copied to the buffer, in characters, not including the terminating null character.
|
||||
/// If the function succeeds, the return value is the length of the string copied to the buffer, in characters, not including the
|
||||
/// terminating null character.
|
||||
/// </para>
|
||||
/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
|
||||
/// </returns>
|
||||
// UINT WINAPI GetAtomName( _In_ ATOM nAtom, _Out_ LPTSTR lpBuffer, _In_ int nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649059(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649059")]
|
||||
public static extern uint GetAtomName(ushort nAtom, StringBuilder lpBuffer, int nSize);
|
||||
public static extern uint GetAtomName(ATOM nAtom, StringBuilder lpBuffer, int nSize);
|
||||
|
||||
/// <summary>Retrieves a copy of the character string associated with the specified local atom.</summary>
|
||||
/// <param name="nAtom">
|
||||
|
@ -89,16 +94,19 @@ namespace Vanara.PInvoke
|
|||
/// <para>The local atom that identifies the character string to be retrieved.</para>
|
||||
/// </param>
|
||||
/// <returns>On success, the character string associated with the specified local atom. On failure, <see langword="null"/>.</returns>
|
||||
public static string GetAtomName(ushort nAtom) => FunctionHelper.CallMethodWithStrBuf((sb, sz) => GetAtomName(nAtom, sb, sz), 255, out var result) > 0 ? result : null;
|
||||
public static string GetAtomName(ATOM nAtom) => FunctionHelper.CallMethodWithStrBuf((sb, sz) => GetAtomName(nAtom, sb, sz), 255, out var result) > 0 ? result : null;
|
||||
|
||||
/// <summary>Adds a character string to the global atom table and returns a unique value (an atom) identifying the string.</summary>
|
||||
/// <param name="lpString">
|
||||
/// <para>Type: <c>LPCTSTR</c></para>
|
||||
/// <para>
|
||||
/// The null-terminated string to be added. The string can have a maximum size of 255 bytes. Strings that differ only in case are considered identical.
|
||||
/// The case of the first string of this name added to the table is preserved and returned by the <c>GlobalGetAtomName</c> function.
|
||||
/// The null-terminated string to be added. The string can have a maximum size of 255 bytes. Strings that differ only in case are
|
||||
/// considered identical. The case of the first string of this name added to the table is preserved and returned by the
|
||||
/// <c>GlobalGetAtomName</c> function.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.
|
||||
/// </para>
|
||||
/// <para>Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.</para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>Type: <c>ATOM</c></para>
|
||||
|
@ -108,16 +116,19 @@ namespace Vanara.PInvoke
|
|||
// ATOM WINAPI GlobalAddAtom( _In_ LPCTSTR lpString); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649060(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649060")]
|
||||
public static extern ushort GlobalAddAtom(string lpString);
|
||||
public static extern ATOM GlobalAddAtom(string lpString);
|
||||
|
||||
/// <summary>Adds a character string to the global atom table and returns a unique value (an atom) identifying the string.</summary>
|
||||
/// <param name="lpString">
|
||||
/// <para>Type: <c>LPCTSTR</c></para>
|
||||
/// <para>
|
||||
/// The null-terminated string to be added. The string can have a maximum size of 255 bytes. Strings that differ only in case are considered identical.
|
||||
/// The case of the first string of this name added to the table is preserved and returned by the <c>GlobalGetAtomName</c> function.
|
||||
/// The null-terminated string to be added. The string can have a maximum size of 255 bytes. Strings that differ only in case are
|
||||
/// considered identical. The case of the first string of this name added to the table is preserved and returned by the
|
||||
/// <c>GlobalGetAtomName</c> function.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.
|
||||
/// </para>
|
||||
/// <para>Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.</para>
|
||||
/// </param>
|
||||
/// <param name="Flags">The flags.</param>
|
||||
/// <returns>
|
||||
|
@ -128,11 +139,11 @@ namespace Vanara.PInvoke
|
|||
// ATOM WINAPI GlobalAddAtom( _In_ LPCTSTR lpString); https://msdn.microsoft.com/en-us/library/windows/desktop/dn764994(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "dn764994")]
|
||||
public static extern ushort GlobalAddAtomEx(string lpString, uint Flags);
|
||||
public static extern ATOM GlobalAddAtomEx(string lpString, uint Flags);
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the reference count of a global string atom. If the atom's reference count reaches zero, <c>GlobalDeleteAtom</c> removes the string
|
||||
/// associated with the atom from the global atom table.
|
||||
/// Decrements the reference count of a global string atom. If the atom's reference count reaches zero, <c>GlobalDeleteAtom</c>
|
||||
/// removes the string associated with the atom from the global atom table.
|
||||
/// </summary>
|
||||
/// <param name="nAtom">
|
||||
/// <para>Type: <c>ATOM</c></para>
|
||||
|
@ -142,20 +153,25 @@ namespace Vanara.PInvoke
|
|||
/// <para>Type: <c>ATOM</c></para>
|
||||
/// <para>The function always returns ( <c>ATOM</c>) 0.</para>
|
||||
/// <para>
|
||||
/// To determine whether the function has failed, call <c>SetLastError</c> with <c>ERROR_SUCCESS</c> before calling <c>GlobalDeleteAtom</c>, then call
|
||||
/// <c>GetLastError</c>. If the last error code is still <c>ERROR_SUCCESS</c>, <c>GlobalDeleteAtom</c> has succeeded.
|
||||
/// To determine whether the function has failed, call <c>SetLastError</c> with <c>ERROR_SUCCESS</c> before calling
|
||||
/// <c>GlobalDeleteAtom</c>, then call <c>GetLastError</c>. If the last error code is still <c>ERROR_SUCCESS</c>,
|
||||
/// <c>GlobalDeleteAtom</c> has succeeded.
|
||||
/// </para>
|
||||
/// </returns>
|
||||
// ATOM WINAPI GlobalDeleteAtom( _In_ ATOM nAtom); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649061(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649061")]
|
||||
public static extern ushort GlobalDeleteAtom(ushort nAtom);
|
||||
public static extern ATOM GlobalDeleteAtom(ATOM nAtom);
|
||||
|
||||
/// <summary>Searches the global atom table for the specified character string and retrieves the global atom associated with that string.</summary>
|
||||
/// <summary>
|
||||
/// Searches the global atom table for the specified character string and retrieves the global atom associated with that string.
|
||||
/// </summary>
|
||||
/// <param name="lpString">
|
||||
/// <para>Type: <c>LPCTSTR</c></para>
|
||||
/// <para>The null-terminated character string for which to search.</para>
|
||||
/// <para>Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.</para>
|
||||
/// <para>
|
||||
/// Alternatively, you can use an integer atom that has been converted using the <c>MAKEINTATOM</c> macro. See the Remarks for more information.
|
||||
/// </para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>Type: <c>ATOM</c></para>
|
||||
|
@ -165,7 +181,7 @@ namespace Vanara.PInvoke
|
|||
// ATOM WINAPI GlobalFindAtom( _In_ LPCTSTR lpString); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649062(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649062")]
|
||||
public static extern ushort GlobalFindAtom(string lpString);
|
||||
public static extern ATOM GlobalFindAtom(string lpString);
|
||||
|
||||
/// <summary>Retrieves a copy of the character string associated with the specified global atom.</summary>
|
||||
/// <param name="nAtom">
|
||||
|
@ -183,14 +199,15 @@ namespace Vanara.PInvoke
|
|||
/// <returns>
|
||||
/// <para>Type: <c>UINT</c></para>
|
||||
/// <para>
|
||||
/// If the function succeeds, the return value is the length of the string copied to the buffer, in characters, not including the terminating null character.
|
||||
/// If the function succeeds, the return value is the length of the string copied to the buffer, in characters, not including the
|
||||
/// terminating null character.
|
||||
/// </para>
|
||||
/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
|
||||
/// </returns>
|
||||
// UINT WINAPI GlobalGetAtomName( _In_ ATOM nAtom, _Out_ LPTSTR lpBuffer, _In_ int nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649063(v=vs.85).aspx
|
||||
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
|
||||
[PInvokeData("Winbase.h", MSDNShortId = "ms649063")]
|
||||
public static extern uint GlobalGetAtomName(ushort nAtom, StringBuilder lpBuffer, int nSize);
|
||||
public static extern uint GlobalGetAtomName(ATOM nAtom, StringBuilder lpBuffer, int nSize);
|
||||
|
||||
/// <summary>Retrieves a copy of the character string associated with the specified global atom.</summary>
|
||||
/// <param name="nAtom">
|
||||
|
@ -198,12 +215,14 @@ namespace Vanara.PInvoke
|
|||
/// <para>The global atom associated with the character string to be retrieved.</para>
|
||||
/// </param>
|
||||
/// <returns>On success, the character string associated with the specified global atom. On failure, <see langword="null"/>.</returns>
|
||||
public static string GlobalGetAtomName(ushort nAtom) => FunctionHelper.CallMethodWithStrBuf((sb, sz) => GlobalGetAtomName(nAtom, sb, sz), 255, out var result) > 0 ? result : null;
|
||||
public static string GlobalGetAtomName(ATOM nAtom) => FunctionHelper.CallMethodWithStrBuf((sb, sz) => GlobalGetAtomName(nAtom, sb, sz), 255, out var result) > 0 ? result : null;
|
||||
|
||||
/// <summary>Initializes the local atom table and sets the number of hash buckets to the specified size.</summary>
|
||||
/// <param name="nSize">
|
||||
/// <para>Type: <c>DWORD</c></para>
|
||||
/// <para>The number of hash buckets to use for the atom table. If this parameter is zero, the default number of hash buckets are created.</para>
|
||||
/// <para>
|
||||
/// The number of hash buckets to use for the atom table. If this parameter is zero, the default number of hash buckets are created.
|
||||
/// </para>
|
||||
/// <para>To achieve better performance, specify a prime number in nSize.</para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
|
@ -216,5 +235,37 @@ namespace Vanara.PInvoke
|
|||
[PInvokeData("Winbase.h", MSDNShortId = "ms649064")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool InitAtomTable(uint nSize);
|
||||
|
||||
/// <summary>Represents a Windows atom.</summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ATOM
|
||||
{
|
||||
private ushort value;
|
||||
|
||||
/// <summary>Gets a value indicating whether this instance is invalid.</summary>
|
||||
/// <value><c>true</c> if this instance is invalid; otherwise, <c>false</c>.</value>
|
||||
public bool IsInvalid => value == 0;
|
||||
|
||||
/// <summary>The invalid atom</summary>
|
||||
public static readonly ATOM INVALID_ATOM = new ATOM();
|
||||
|
||||
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
||||
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
||||
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is ATOM a)
|
||||
return value.Equals(a.value);
|
||||
return value.Equals(obj);
|
||||
}
|
||||
|
||||
/// <summary>Returns a hash code for this instance.</summary>
|
||||
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
||||
public override int GetHashCode() => (int)value;
|
||||
|
||||
/// <summary>Converts to string.</summary>
|
||||
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
|
||||
public override string ToString() => value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -48,6 +48,7 @@
|
|||
<Compile Include="AppModelTests.cs" />
|
||||
<Compile Include="InterlockedApiTests.cs" />
|
||||
<Compile Include="InteropServices\SafeLocalHandleTests.cs" />
|
||||
<Compile Include="WinBase.AtomTests.cs" />
|
||||
<Compile Include="WinBase.AppTests.cs" />
|
||||
<Compile Include="WerApiTests.cs" />
|
||||
<Compile Include="VersionHelpersTests.cs" />
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Vanara.InteropServices;
|
||||
using static Vanara.PInvoke.AdvApi32;
|
||||
using static Vanara.PInvoke.Kernel32;
|
||||
|
||||
namespace Vanara.PInvoke.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public partial class WinBaseTests
|
||||
{
|
||||
[Test]
|
||||
public void AtomTest()
|
||||
{
|
||||
const string name = "Test";
|
||||
Assert.That(InitAtomTable(7), ResultIs.Successful);
|
||||
var atom = AddAtom(name);
|
||||
Assert.That(atom, Is.Not.Zero);
|
||||
Assert.That(FindAtom(name), Is.EqualTo(atom));
|
||||
Assert.That(GetAtomName(atom), Is.EqualTo(name));
|
||||
Assert.That(DeleteAtom(atom), ResultIs.Value(ATOM.INVALID_ATOM));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GlobalAtomTest()
|
||||
{
|
||||
const string name = "Test";
|
||||
var atom = GlobalAddAtom(name);
|
||||
Assert.That(atom, Is.Not.Zero);
|
||||
Assert.That(GlobalFindAtom(name), Is.EqualTo(atom));
|
||||
Assert.That(GlobalGetAtomName(atom), Is.EqualTo(name));
|
||||
Assert.That(GlobalDeleteAtom(atom), ResultIs.Value(ATOM.INVALID_ATOM));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue