Skip to content

Commit 7c54a48

Browse files
committed
Added: Usage warning for ExternalMemory
1 parent 2d6ed4d commit 7c54a48

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Reloaded.Memory/ExternalMemory.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ namespace Reloaded.Memory;
4747

4848
/// <summary>
4949
/// Creates an instance of the <see cref="ExternalMemory" /> class used to read from an
50-
/// external process with a specified handle.
50+
/// external process with a specified handle. <br/>
51+
/// Note: See warning in <see cref="ExternalMemory(Process)" />.
5152
/// </summary>
52-
/// <param name="process">The individual process to read/write memory from.</param>
53+
/// <param name="process">
54+
/// The individual process to read/write memory from.<br/>
55+
/// Warning!! <see cref="ExternalMemory"/> does not hold a reference to the process.<br/> <br/>
56+
/// In order to ensure the handle stays valid, you MUST MANUALLY prevent this parameter from being
57+
/// garbage collected/disposed by storing it somewhere else.
58+
/// </param>
59+
/// <remarks>
60+
/// On Linux, please insert Process ID here.
61+
/// </remarks>
5362
public ExternalMemory(Process process) : this()
5463
{
5564
if (Polyfills.IsWindows())

src/Reloaded.Memory/Native/Windows/Kernel32.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public static partial class Kernel32
5050
/// <para>The function fails if the requested read operation crosses into an area of the process that is inaccessible.</para>
5151
/// </returns>
5252
[SuppressUnmanagedCodeSecurity]
53+
#if NET5_0_OR_GREATER
54+
[SuppressGCTransition]
55+
#endif
5356
#if NET7_0_OR_GREATER
5457
[LibraryImport("kernel32.dll", SetLastError = true)]
5558
[return: MarshalAs(UnmanagedType.Bool)]
@@ -97,6 +100,9 @@ public static extern bool ReadProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddre
97100
/// </para>
98101
/// </returns>
99102
[SuppressUnmanagedCodeSecurity]
103+
#if NET5_0_OR_GREATER
104+
[SuppressGCTransition]
105+
#endif
100106
#if NET7_0_OR_GREATER
101107
[LibraryImport("kernel32.dll", SetLastError = true)]
102108
[return: MarshalAs(UnmanagedType.Bool)]
@@ -281,6 +287,9 @@ public static extern bool WriteProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddr
281287
/// </para>
282288
/// </returns>
283289
[SuppressUnmanagedCodeSecurity]
290+
#if NET5_0_OR_GREATER
291+
[SuppressGCTransition]
292+
#endif
284293
#if NET7_0_OR_GREATER
285294
[LibraryImport("kernel32.dll", SetLastError = true)]
286295
public static partial nuint VirtualAllocEx(IntPtr hProcess, UIntPtr lpAddress, UIntPtr dwSize,
@@ -372,6 +381,9 @@ public static extern nuint VirtualAllocEx(IntPtr hProcess, UIntPtr lpAddress, UI
372381
/// </para>
373382
/// </returns>
374383
[SuppressUnmanagedCodeSecurity]
384+
#if NET5_0_OR_GREATER
385+
[SuppressGCTransition]
386+
#endif
375387
#if NET7_0_OR_GREATER
376388
[LibraryImport("kernel32.dll")]
377389
[return: MarshalAs(UnmanagedType.Bool)]
@@ -426,6 +438,9 @@ public static extern bool VirtualFreeEx(IntPtr hProcess, UIntPtr lpAddress, UInt
426438
/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
427439
/// </returns>
428440
[SuppressUnmanagedCodeSecurity]
441+
#if NET5_0_OR_GREATER
442+
[SuppressGCTransition]
443+
#endif
429444
#if NET7_0_OR_GREATER
430445
[LibraryImport("kernel32.dll", SetLastError = true)]
431446
[return: MarshalAs(UnmanagedType.Bool)]
@@ -610,6 +625,9 @@ public static extern bool VirtualProtectEx(IntPtr hProcess, UIntPtr lpAddress, U
610625
/// </para>
611626
/// </returns>
612627
[SuppressUnmanagedCodeSecurity]
628+
#if NET5_0_OR_GREATER
629+
[SuppressGCTransition]
630+
#endif
613631
#if NET7_0_OR_GREATER
614632
[LibraryImport("kernel32.dll", SetLastError = true)]
615633
public static partial nuint VirtualAlloc(UIntPtr lpAddress, UIntPtr dwSize,
@@ -699,6 +717,9 @@ public static extern nuint VirtualAlloc(UIntPtr lpAddress, UIntPtr dwSize,
699717
/// </para>
700718
/// </returns>
701719
[SuppressUnmanagedCodeSecurity]
720+
#if NET5_0_OR_GREATER
721+
[SuppressGCTransition]
722+
#endif
702723
#if NET7_0_OR_GREATER
703724
[LibraryImport("kernel32.dll")]
704725
[return: MarshalAs(UnmanagedType.Bool)]
@@ -752,6 +773,9 @@ public static extern nuint VirtualAlloc(UIntPtr lpAddress, UIntPtr dwSize,
752773
/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
753774
/// </returns>
754775
[SuppressUnmanagedCodeSecurity]
776+
#if NET5_0_OR_GREATER
777+
[SuppressGCTransition]
778+
#endif
755779
#if NET7_0_OR_GREATER
756780
[LibraryImport("kernel32.dll", SetLastError = true)]
757781
[return: MarshalAs(UnmanagedType.Bool)]

src/Reloaded.Memory/Reloaded.Memory.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<RootNamespace>Reloaded.Memory</RootNamespace>
77
<PackageProjectUrl>https://github.com/Reloaded-Project/Reloaded.Memory</PackageProjectUrl>
88
<Description>High performance, near zero overhead utilities for working with native memory.</Description>
9-
<Version>9.0.0</Version>
9+
<Version>9.0.1</Version>
1010
<Authors>Sewer56</Authors>
1111
<Product>Reloaded Memory Library</Product>
1212
<PackageId>Reloaded.Memory</PackageId>

0 commit comments

Comments
 (0)