mirror of
https://github.com/mirror/processhacker
synced 2026-06-08 16:03:24 +00:00
6ff1e03e15
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1352 21ef857c-d57f-4fe0-8362-d861dc6d29cd
123 lines
3.7 KiB
C#
123 lines
3.7 KiB
C#
/*
|
|
* Process Hacker -
|
|
* IntPtr extension functions
|
|
*
|
|
* Copyright (C) 2009 wj32
|
|
* Copyright (C) 2009 Flavio Erlich
|
|
*
|
|
* This file is part of Process Hacker.
|
|
*
|
|
* Process Hacker is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Process Hacker is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace ProcessHacker.Native
|
|
{
|
|
public static class IntPtrExtensions
|
|
{
|
|
public static int CompareTo(this IntPtr ptr, IntPtr ptr2)
|
|
{
|
|
if (ptr.ToInt64() > ptr2.ToInt64())
|
|
return 1;
|
|
if (ptr.ToInt64() < ptr2.ToInt64())
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
public static int CompareTo(this IntPtr ptr, int ptr2)
|
|
{
|
|
if (ptr.ToInt64() > ptr2)
|
|
return 1;
|
|
if (ptr.ToInt64() < ptr2)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
public static IntPtr Decrement(this IntPtr ptr, IntPtr ptr2)
|
|
{
|
|
if (IntPtr.Size == sizeof(Int32))
|
|
return new IntPtr(ptr.ToInt32() - ptr2.ToInt32());
|
|
else
|
|
return new IntPtr(ptr.ToInt64() - ptr2.ToInt64());
|
|
}
|
|
|
|
public static IntPtr Decrement(this IntPtr ptr, int value)
|
|
{
|
|
return Increment(ptr, -value);
|
|
}
|
|
|
|
public static IntPtr Decrement(this IntPtr ptr, long value)
|
|
{
|
|
return Increment(ptr, -value);
|
|
}
|
|
|
|
public static T ElementAt<T>(this IntPtr ptr, int index)
|
|
{
|
|
var offset = Marshal.SizeOf(typeof(T)) * index;
|
|
var offsetPtr = ptr.Increment(offset);
|
|
return (T)Marshal.PtrToStructure(offsetPtr, typeof(T));
|
|
}
|
|
|
|
public static IntPtr Increment(this IntPtr ptr, int value)
|
|
{
|
|
unchecked
|
|
{
|
|
if (IntPtr.Size == sizeof(Int32))
|
|
return new IntPtr(ptr.ToInt32() + value);
|
|
else
|
|
return new IntPtr(ptr.ToInt64() + value);
|
|
}
|
|
}
|
|
|
|
public static IntPtr Increment(this IntPtr ptr, long value)
|
|
{
|
|
unchecked
|
|
{
|
|
if (IntPtr.Size == sizeof(Int32))
|
|
return new IntPtr(ptr.ToInt32() + value);
|
|
else
|
|
return new IntPtr(ptr.ToInt64() + value);
|
|
}
|
|
}
|
|
|
|
public static IntPtr Increment(this IntPtr ptr, IntPtr ptr2)
|
|
{
|
|
unchecked
|
|
{
|
|
if (IntPtr.Size == sizeof(Int32))
|
|
return new IntPtr(ptr.ToInt32() + ptr2.ToInt32());
|
|
else
|
|
return new IntPtr(ptr.ToInt64() + ptr2.ToInt64());
|
|
}
|
|
}
|
|
|
|
public static IntPtr Increment<T>(this IntPtr ptr)
|
|
{
|
|
return ptr.Increment(Marshal.SizeOf(typeof(T)));
|
|
}
|
|
|
|
public static uint ToUInt32(this IntPtr ptr)
|
|
{
|
|
return (uint)ptr.ToInt32();
|
|
}
|
|
|
|
public static ulong ToUInt64(this IntPtr ptr)
|
|
{
|
|
return (ulong)ptr.ToInt64();
|
|
}
|
|
}
|
|
}
|