Files
mirror-processhacker/trunk/ProcessHacker/MemoryAlloc.cs
T
wj32 5beff32478 * added MemoryAlloc class - for managing global-alloc'ed memory
* addded LSAHandle, LSAPolicyHandle
* addded ServiceBaseHandle, ServiceManagerHandle
* revised all Win32 code to use MemoryAlloc class
* revised Win32 function imports to be more natural when used (ref -> out, int -> bool)
* moved session ID display from Process tab on HackerWindow.cs to TokenWindow.cs


git-svn-id: svn://svn.code.sf.net/p/processhacker/code@332 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2008-12-19 05:47:47 +00:00

192 lines
6.3 KiB
C#

/*
* Process Hacker
*
* Copyright (C) 2008 wj32
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Runtime.InteropServices;
namespace ProcessHacker
{
/// <summary>
/// Represents an unmanaged memory allocation.
/// </summary>
public class MemoryAlloc : IDisposable
{
private bool _freed = false;
private IntPtr _memory;
private int _size;
public static implicit operator int(MemoryAlloc memory)
{
return memory.Memory.ToInt32();
}
public static implicit operator IntPtr(MemoryAlloc memory)
{
return memory.Memory;
}
/// <summary>
/// Creates a memory allocation from an existing pointer. The allocation
/// referenced by the pointer will be freed automatically. The Size
/// property will be set to 0.
/// </summary>
/// <param name="memory">A pointer to an existing memory allocation.</param>
/// <returns>A new instance of a memory allocation.</returns>
public static MemoryAlloc FromPointer(IntPtr memory)
{
return new MemoryAlloc(0) { _memory = memory };
}
/// <summary>
/// Creates a new memory allocation with the specified size.
/// </summary>
/// <param name="size">The amount of memory, in bytes, to allocate.</param>
public MemoryAlloc(int size)
{
_memory = Marshal.AllocHGlobal(size);
_size = size;
}
/// <summary>
/// Reads a signed integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <returns>The integer.</returns>
public int ReadInt32(int offset)
{
return this.ReadInt32(offset, 0);
}
/// <summary>
/// Reads a signed integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <param name="index">The index at which to begin reading, after the offset is added.</param>
/// <returns>The integer.</returns>
public int ReadInt32(int offset, int index)
{
return Marshal.ReadInt32(_memory,
offset + index * 4);
}
/// <summary>
/// Reads an unsigned integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <returns>The integer.</returns>
public uint ReadUInt32(int offset)
{
return this.ReadUInt32(offset, 0);
}
/// <summary>
/// Reads an unsigned integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <param name="index">The index at which to begin reading, after the offset is added.</param>
/// <returns>The integer.</returns>
public uint ReadUInt32(int offset, int index)
{
return (uint)this.ReadInt32(offset, index);
}
/// <summary>
/// Creates a struct from the memory allocation.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <returns>The new struct.</returns>
public T ReadStruct<T>()
{
return this.ReadStruct<T>(0);
}
/// <summary>
/// Creates a struct from the memory allocation.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <param name="index">The index at which to begin reading to the struct. This is multiplied by
/// the size of the struct.</param>
/// <returns>The new struct.</returns>
public T ReadStruct<T>(int index)
{
return this.ReadStruct<T>(0, index);
}
/// <summary>
/// Creates a struct from the memory allocation.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <param name="offset">The offset to add before reading.</param>
/// <param name="index">The index at which to begin reading to the struct. This is multiplied by
/// the size of the struct.</param>
/// <returns>The new struct.</returns>
public T ReadStruct<T>(int offset, int index)
{
return (T)Marshal.PtrToStructure(
new IntPtr(_memory.ToInt32() + offset + Marshal.SizeOf(typeof(T)) * index), typeof(T));
}
/// <summary>
/// Resizes the memory allocation.
/// </summary>
/// <param name="newSize">The new size of the allocation.</param>
public void Resize(int newSize)
{
_memory = Marshal.ReAllocHGlobal(_memory, new IntPtr(newSize));
_size = newSize;
}
/// <summary>
/// Gets a pointer to the allocated memory.
/// </summary>
public IntPtr Memory
{
get { return _memory; }
}
/// <summary>
/// Gets the size of the allocated memory.
/// </summary>
public int Size
{
get { return _size; }
}
~MemoryAlloc()
{
this.Dispose();
}
/// <summary>
/// Frees the allocated memory.
/// </summary>
public void Dispose()
{
lock (this)
{
if (!_freed)
{
_freed = true;
Marshal.FreeHGlobal(_memory);
}
}
}
}
}