Repository navigation
Expand file tree
/
Copy pathLocalUnmanagedMemory.cs
More file actions
108 lines (98 loc) · 3.56 KB
/
Copy pathLocalUnmanagedMemory.cs
File metadata and controls
108 lines (98 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Runtime.InteropServices;
namespace Process.NET.Memory
{
/// <summary>
/// Class representing a block of memory allocated in the local process.
/// </summary>
public class LocalUnmanagedMemory : IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="LocalUnmanagedMemory" /> class, allocating a block of memory in the
/// local process.
/// </summary>
/// <param name="size">The size to allocate.</param>
public LocalUnmanagedMemory(int size)
{
// Allocate the memory
Size = size;
Address = Marshal.AllocHGlobal(Size);
}
/// <summary>
/// The address where the data is allocated.
/// </summary>
public IntPtr Address { get; private set; }
/// <summary>
/// The size of the allocated memory.
/// </summary>
public int Size { get; }
/// <summary>
/// Releases the memory held by the <see cref="LocalUnmanagedMemory" /> object.
/// </summary>
public virtual void Dispose()
{
// Free the allocated memory
Marshal.FreeHGlobal(Address);
// Remove the pointer
Address = IntPtr.Zero;
// Avoid the finalizer
GC.SuppressFinalize(this);
}
/// <summary>
/// Frees resources and perform other cleanup operations before it is reclaimed by garbage collection.
/// </summary>
~LocalUnmanagedMemory()
{
Dispose();
}
/// <summary>
/// Reads data from the unmanaged block of memory.
/// </summary>
/// <typeparam name="T">The type of data to return.</typeparam>
/// <returns>The return value is the block of memory casted in the specified type.</returns>
public T Read<T>()
{
// Marshal data from the block of memory to a new allocated managed object
return (T) Marshal.PtrToStructure(Address, typeof (T));
}
/// <summary>
/// Reads an array of bytes from the unmanaged block of memory.
/// </summary>
/// <returns>The return value is the block of memory.</returns>
public byte[] Read()
{
// Allocate an array to store data
var bytes = new byte[Size];
// Copy the block of memory to the array
Marshal.Copy(Address, bytes, 0, Size);
// Return the array
return bytes;
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
public override string ToString()
{
return $"Size = {Size:X}";
}
/// <summary>
/// Writes an array of bytes to the unmanaged block of memory.
/// </summary>
/// <param name="byteArray">The array of bytes to write.</param>
public void Write(byte[] byteArray)
{
// Copy the array of bytes into the block of memory
Marshal.Copy(byteArray, 0, Address, Size);
}
/// <summary>
/// Write data to the unmanaged block of memory.
/// </summary>
/// <typeparam name="T">The type of data to write.</typeparam>
/// <param name="data">The data to write.</param>
public void Write<T>(T data)
{
// Marshal data from the managed object to the block of memory
Marshal.StructureToPtr(data, Address, false);
}
}
}