using System; using System.Runtime.InteropServices; using ProcessHacker.Native.Api; using ProcessHacker.Native.Objects; namespace ProcessHacker.Native { public static class Utils { /// /// Reads a Unicode string. /// /// A UNICODE_STRING structure. /// A string. /// This function is needed because some LSA strings are not /// null-terminated, so we can't use .NET's marshalling. public static string ReadUnicodeString(UnicodeString str) { if (str.Length == 0) return null; return Marshal.PtrToStringUni(str.Buffer, str.Length / 2); } public static string ReadUnicodeString(ProcessHandle processHandle, UnicodeString str) { if (str.Length == 0) return null; byte[] strData = processHandle.ReadMemory(str.Buffer, str.Length); GCHandle strDataHandle = GCHandle.Alloc(strData, GCHandleType.Pinned); try { return Marshal.PtrToStringUni(strDataHandle.AddrOfPinnedObject(), str.Length / 2); } finally { strDataHandle.Free(); } } /// /// Swaps the order of the bytes in the argument. /// /// The number to change. /// A number. public static int ByteSwap(int v) { byte b1 = (byte)v; byte b2 = (byte)(v >> 8); byte b3 = (byte)(v >> 16); byte b4 = (byte)(v >> 24); return b4 | (b3 << 8) | (b2 << 16) | (b1 << 24); } /// /// Swaps the order of the bytes. /// /// A number. public static int SwapBytes(this int v) { return ByteSwap(v); } /// /// Swaps the order of the bytes in the argument. /// /// The number to change. /// A number. public static uint ByteSwap(uint v) { byte b1 = (byte)v; byte b2 = (byte)(v >> 8); byte b3 = (byte)(v >> 16); byte b4 = (byte)(v >> 24); return (uint)(b4 | (b3 << 8) | (b2 << 16) | (b1 << 24)); } /// /// Swaps the order of the bytes. /// /// A number. public static uint SwapBytes(this uint v) { return ByteSwap(v); } /// /// Swaps the order of the bytes in the argument. /// /// The number to change. /// A number. public static ushort ByteSwap(ushort v) { byte b1 = (byte)v; byte b2 = (byte)(v >> 8); return (ushort)(b2 | (b1 << 8)); } /// /// Swaps the order of the bytes. /// /// A number. public static ushort SwapBytes(this ushort v) { return ByteSwap(v); } } }