using System.Runtime.InteropServices;
using System.Text;
namespace Vanara.PInvoke
{
public static partial class Kernel32
{
/// Adds a character string to the local atom table and returns a unique value (an atom) identifying the string.
///
/// Type: LPCTSTR
///
/// 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 GetAtomName function.
///
///
/// Alternatively, you can use an integer atom that has been converted using the MAKEINTATOM macro. See the Remarks for more information.
///
///
///
/// Type: ATOM
/// If the function succeeds, the return value is the newly created atom.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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 ATOM AddAtom(string lpString);
///
/// Decrements the reference count of a local string atom. If the atom's reference count is reduced to zero, DeleteAtom
/// removes the string associated with the atom from the local atom table.
///
///
/// Type: ATOM
/// The atom to be deleted.
///
///
/// Type: ATOM
/// If the function succeeds, the return value is zero.
/// If the function fails, the return value is the nAtom parameter. To get extended error information, call GetLastError.
///
// 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 ATOM DeleteAtom(ATOM nAtom);
/// Searches the local atom table for the specified character string and retrieves the atom associated with that string.
///
/// Type: LPCTSTR
/// The character string for which to search.
///
/// Alternatively, you can use an integer atom that has been converted using the MAKEINTATOM macro. See Remarks for more information.
///
///
///
/// Type: ATOM
/// If the function succeeds, the return value is the atom associated with the given string.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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 ATOM FindAtom(string lpString);
/// Retrieves a copy of the character string associated with the specified local atom.
///
/// Type: ATOM
/// The local atom that identifies the character string to be retrieved.
///
///
/// Type: LPTSTR
/// The character string.
///
///
/// Type: int
/// The size, in characters, of the buffer.
///
///
/// Type: UINT
///
/// 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 fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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(ATOM nAtom, StringBuilder lpBuffer, int nSize);
/// Retrieves a copy of the character string associated with the specified local atom.
///
/// Type: ATOM
/// The local atom that identifies the character string to be retrieved.
///
/// On success, the character string associated with the specified local atom. On failure, .
public static string GetAtomName(ATOM nAtom) => FunctionHelper.CallMethodWithStrBuf((sb, sz) => GetAtomName(nAtom, sb, sz), 255, out var result) > 0 ? result : null;
/// Adds a character string to the global atom table and returns a unique value (an atom) identifying the string.
///
/// Type: LPCTSTR
///
/// 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
/// GlobalGetAtomName function.
///
///
/// Alternatively, you can use an integer atom that has been converted using the MAKEINTATOM macro. See the Remarks for more information.
///
///
///
/// Type: ATOM
/// If the function succeeds, the return value is the newly created atom.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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 ATOM GlobalAddAtom(string lpString);
/// Adds a character string to the global atom table and returns a unique value (an atom) identifying the string.
///
/// Type: LPCTSTR
///
/// 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
/// GlobalGetAtomName function.
///
///
/// Alternatively, you can use an integer atom that has been converted using the MAKEINTATOM macro. See the Remarks for more information.
///
///
/// The flags.
///
/// Type: ATOM
/// If the function succeeds, the return value is the newly created atom.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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 ATOM GlobalAddAtomEx(string lpString, uint Flags);
///
/// Decrements the reference count of a global string atom. If the atom's reference count reaches zero, GlobalDeleteAtom
/// removes the string associated with the atom from the global atom table.
///
///
/// Type: ATOM
/// The atom and character string to be deleted.
///
///
/// Type: ATOM
/// The function always returns ( ATOM) 0.
///
/// To determine whether the function has failed, call SetLastError with ERROR_SUCCESS before calling
/// GlobalDeleteAtom, then call GetLastError. If the last error code is still ERROR_SUCCESS,
/// GlobalDeleteAtom has succeeded.
///
///
// 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 ATOM GlobalDeleteAtom(ATOM nAtom);
///
/// Searches the global atom table for the specified character string and retrieves the global atom associated with that string.
///
///
/// Type: LPCTSTR
/// The null-terminated character string for which to search.
///
/// Alternatively, you can use an integer atom that has been converted using the MAKEINTATOM macro. See the Remarks for more information.
///
///
///
/// Type: ATOM
/// If the function succeeds, the return value is the global atom associated with the given string.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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 ATOM GlobalFindAtom(string lpString);
/// Retrieves a copy of the character string associated with the specified global atom.
///
/// Type: ATOM
/// The global atom associated with the character string to be retrieved.
///
///
/// Type: LPTSTR
/// The buffer for the character string.
///
///
/// Type: int
/// The size, in characters, of the buffer.
///
///
/// Type: UINT
///
/// 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 fails, the return value is zero. To get extended error information, call GetLastError.
///
// 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(ATOM nAtom, StringBuilder lpBuffer, int nSize);
/// Retrieves a copy of the character string associated with the specified global atom.
///
/// Type: ATOM
/// The global atom associated with the character string to be retrieved.
///
/// On success, the character string associated with the specified global atom. On failure, .
public static string GlobalGetAtomName(ATOM nAtom) => FunctionHelper.CallMethodWithStrBuf((sb, sz) => GlobalGetAtomName(nAtom, sb, sz), 255, out var result) > 0 ? result : null;
/// Initializes the local atom table and sets the number of hash buckets to the specified size.
///
/// Type: DWORD
///
/// The number of hash buckets to use for the atom table. If this parameter is zero, the default number of hash buckets are created.
///
/// To achieve better performance, specify a prime number in nSize.
///
///
/// Type: BOOL
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero.
///
// BOOL WINAPI InitAtomTable( _In_ DWORD nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms649064(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms649064")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InitAtomTable(uint nSize);
/// Represents a Windows atom.
[StructLayout(LayoutKind.Sequential)]
public struct ATOM
{
private ushort value;
/// Gets a value indicating whether this instance is invalid.
/// true if this instance is invalid; otherwise, false.
public bool IsInvalid => value == 0;
/// The invalid atom
public static readonly ATOM INVALID_ATOM = new ATOM();
/// Determines whether the specified , is equal to this instance.
/// The to compare with this instance.
/// true if the specified is equal to this instance; otherwise, false.
public override bool Equals(object obj)
{
if (obj is ATOM a)
return value.Equals(a.value);
return value.Equals(obj);
}
/// Returns a hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
public override int GetHashCode() => (int)value;
/// Converts to string.
/// A that represents this instance.
public override string ToString() => value.ToString();
}
}
}