using System; namespace Vanara.PInvoke { /// Platform invokable enumerated types, constants and functions from windows.h public static partial class Macros { /// Aligns a number to the neighboring multiple. /// The value to align. /// A number that is a power of 2 (e.g. 2, 4, 8, 16, ...). /// /// A value that is aligned to the next multiple of . This value may be the same as . /// /// pow2 - Parameter must be a power of 2. public static long ALIGN_TO_MULTIPLE(long value, int pow2) { // Ensure pow2 is a power of 2 if (pow2 == 0 || (pow2 & (pow2 - 1)) != 0) throw new ArgumentOutOfRangeException(nameof(pow2), "Parameter must be a power of 2."); return (value + pow2 - 1) & (~((long)pow2 - 1)); } /// Retrieves the signed x-coordinate from the specified LPARAM value. /// The value to be converted. /// The signed x-coordinate. // https://docs.microsoft.com/en-us/windows/win32/api/windowsx/nf-windowsx-get_x_lparam // void GET_X_LPARAM( lp ); [PInvokeData("windowsx.h")] public static int GET_X_LPARAM(IntPtr lp) => unchecked((short)(long)lp); /// Retrieves the signed y-coordinate from the given LPARAM value. /// The value to be converted. /// The signed y-coordinate. // https://docs.microsoft.com/en-us/windows/win32/api/windowsx/nf-windowsx-get_y_lparam // void GET_Y_LPARAM( lp ); [PInvokeData("windowsx.h")] public static int GET_Y_LPARAM(IntPtr lp) => unchecked((short)((long)lp >> 16)); /// Retrieves the high-order byte from the given 16-bit value. /// The value to be converted. /// The return value is the high-order byte of the specified value. public static byte HIBYTE(ushort wValue) => (byte)((wValue >> 8) & 0xff); /// Gets the high 8-bytes from a value. /// The value. /// The high 8-bytes as a . public static int HighPart(this long lValue) => unchecked((int)(lValue >> 32)); /// Retrieves the high-order word from the specified 32-bit value. /// The value to be converted. /// The return value is the high-order word of the specified value. public static ushort HIWORD(uint dwValue) => (ushort)((dwValue >> 16) & 0xffff); /// Retrieves the high-order word from the specified 32-bit value. /// The value to be converted. /// The return value is the high-order word of the specified value. public static ushort HIWORD(IntPtr dwValue) => unchecked((ushort)((long)dwValue >> 16)); /// Retrieves the high-order word from the specified 32-bit value. /// The value to be converted. /// The return value is the high-order word of the specified value. public static ushort HIWORD(UIntPtr dwValue) => unchecked((ushort)((ulong)dwValue >> 16)); /// Determines whether a value is an integer identifier for a resource. /// The pointer to be tested whether it contains an integer resource identifier. /// If the value is a resource identifier, the return value is TRUE. Otherwise, the return value is FALSE. [PInvokeData("WinBase.h", MSDNShortId = "ms648028")] public static bool IS_INTRESOURCE(IntPtr ptr) => unchecked((ulong)ptr.ToInt64()) >> 16 == 0; /// Retrieves the low-order byte from the given 16-bit value. /// The value to be converted. /// The return value is the low-order byte of the specified value. public static byte LOBYTE(ushort wValue) => (byte)(wValue & 0xff); /// Gets the lower 8-bytes from a value. /// The value. /// The lower 8-bytes as a . public static uint LowPart(this long lValue) => (uint)(lValue & 0xffffffff); /// Retrieves the low-order word from the specified 32-bit value. /// The value to be converted. /// The return value is the low-order word of the specified value. public static ushort LOWORD(uint dwValue) => (ushort)(dwValue & 0xffff); /// Retrieves the low-order word from the specified 32-bit value. /// The value to be converted. /// The return value is the low-order word of the specified value. public static ushort LOWORD(IntPtr dwValue) => unchecked((ushort)(long)dwValue); /// Retrieves the low-order word from the specified 32-bit value. /// The value to be converted. /// The return value is the low-order word of the specified value. public static ushort LOWORD(UIntPtr dwValue) => unchecked((ushort)(ulong)dwValue); /// Converts the specified atom into a pointer, so it can be passed to functions which accept either atoms or strings. /// The numeric value to be made into an integer atom. This parameter can be either an integer atom or a string atom. /// A pointer with the atom as the low-order word. /// Although the return value of the MAKEINTATOM macro is cast as an LPTSTR value, it cannot be used as a string pointer except when it is passed to atom-management functions that require an LPTSTR argument. // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-makeintatom // void MAKEINTATOM( i ); [PInvokeData("winbase.h", MSDNShortId = "NF:winbase.MAKEINTATOM")] public static IntPtr MAKEINTATOM(ushort i) => new IntPtr(unchecked((int)MAKELONG(i, 0))); /// /// Converts an integer value to a resource type compatible with the resource-management functions. This macro is used in place of a /// string containing the name of the resource. /// /// The integer value to be converted. /// The return value is string representation of the integer value. [PInvokeData("WinUser.h", MSDNShortId = "ms648029")] public static ResourceId MAKEINTRESOURCE(int id) => id; /// Creates a LONG value by concatenating the specified values. /// The low-order word of the new value. /// The high-order word of the new value. /// The return value is a LONG value. public static uint MAKELONG(ushort wLow, ushort wHigh) => ((uint)wHigh << 16) | ((uint)wLow & 0xffff); /// Creates a LONG64 value by concatenating the specified values. /// The low-order double word of the new value. /// The high-order double word of the new value. /// The return value is a LONG64 value. public static ulong MAKELONG64(uint dwLow, uint dwHigh) => ((ulong)dwHigh << 32) | ((ulong)dwLow & 0xffffffff); /// Creates a LONG64 value by concatenating the specified values. /// The low-order double word of the new value. /// The high-order double word of the new value. /// The return value is a LONG64 value. public static long MAKELONG64(uint dwLow, int dwHigh) => ((long)dwHigh << 32) | ((long)dwLow & 0xffffffff); /// Creates a value for use as an lParam parameter in a message. The macro concatenates the specified values. /// The low-order word of the new value. /// The high-order word of the new value. /// The return value is an LPARAM value. public static IntPtr MAKELPARAM(ushort wLow, ushort wHigh) => new IntPtr(MAKELONG(wLow, wHigh)); /// Creates a WORD value by concatenating the specified values. /// The low-order byte of the new value. /// The high-order byte of the new value. /// The return value is a WORD value. public static ushort MAKEWORD(byte bLow, byte bHigh) => (ushort)(bHigh << 8 | bLow & 0xff); /// Retrieves the high-order 16-bit value from the specified 32-bit value. /// The value to be converted. /// The return value is the high-order 16-bit value of the specified value. public static short SignedHIWORD(int iValue) => (short)((iValue >> 16) & 0xffff); /// Retrieves the high-order 16-bit value from the specified 32-bit value. /// The value to be converted. /// The return value is the high-order 16-bit value of the specified value. public static short SignedHIWORD(IntPtr iValue) => SignedHIWORD(unchecked((int)iValue.ToInt64())); /// Retrieves the low-order 16-bit value from the specified 32-bit value. /// The value to be converted. /// The return value is the low-order 16-bit value of the specified value. public static short SignedLOWORD(int iValue) => (short)(iValue & 0xffff); /// Retrieves the low-order 16-bit value from the specified 32-bit value. /// The value to be converted. /// The return value is the low-order 16-bit value of the specified value. public static short SignedLOWORD(IntPtr iValue) => SignedLOWORD(unchecked((int)iValue.ToInt64())); } //public static T GetLParam(this System.Windows.Forms.Message msg) => (T)msg.GetLParam(typeof(T)); /*private static int GetEmbeddedNullStringLengthAnsi(string s) { int index = s.IndexOf('\0'); if (index > -1) { string str = s.Substring(0, index); string str2 = s.Substring(index + 1); return ((GetPInvokeStringLength(str) + GetEmbeddedNullStringLengthAnsi(str2)) + 1); } return GetPInvokeStringLength(s); } public static int GetPInvokeStringLength(string s) { if (string.IsNullOrEmpty(s)) return 0; if (Marshal.SystemDefaultCharSize == 2) return s.Length; if (s.IndexOf('\0') > -1) return GetEmbeddedNullStringLengthAnsi(s); return lstrlen(s); }*/ }