Skip to content

Commit

Permalink
removed mProc.Modules, FreezeTokenSrcs is now thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
erfg12 committed Jan 24, 2022
1 parent 762c821 commit c4ef051
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
9 changes: 5 additions & 4 deletions Memory/Methods/Write.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -12,7 +13,7 @@ namespace Memory
{
public partial class Mem
{
Dictionary<string, CancellationTokenSource> FreezeTokenSrcs = new Dictionary<string, CancellationTokenSource>();
ConcurrentDictionary<string, CancellationTokenSource> FreezeTokenSrcs = new ConcurrentDictionary<string, CancellationTokenSource>();

/// <summary>
/// Freeze a value to an address.
Expand All @@ -33,7 +34,7 @@ public void FreezeValue(string address, string type, string value, string file =
try
{
FreezeTokenSrcs[address].Cancel();
FreezeTokenSrcs.Remove(address);
FreezeTokenSrcs.TryRemove(address, out _);
}
catch
{
Expand All @@ -43,7 +44,7 @@ public void FreezeValue(string address, string type, string value, string file =
else
Debug.WriteLine("Adding Freezing Address " + address + " Value " + value);

FreezeTokenSrcs.Add(address, cts);
FreezeTokenSrcs.TryAdd(address, cts);
}

Task.Factory.StartNew(() =>
Expand All @@ -69,7 +70,7 @@ public void UnfreezeValue(string address)
lock (FreezeTokenSrcs)
{
FreezeTokenSrcs[address].Cancel();
FreezeTokenSrcs.Remove(address);
FreezeTokenSrcs.TryRemove(address, out _);
}
}
catch
Expand Down
3 changes: 2 additions & 1 deletion Memory/Structures/Process.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
Expand All @@ -13,7 +14,7 @@ public class Proc
public Process Process { get; set; }
public IntPtr Handle { get; set; }
public bool Is64Bit { get; set; }
public Dictionary<string, IntPtr> Modules { get; set; }
//public ConcurrentDictionary<string, IntPtr> Modules { get; set; } // Use mProc.Process.Modules instead
public ProcessModule MainModule { get; set; }
}
}
49 changes: 31 additions & 18 deletions Memory/memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System.Threading.Tasks;
using System.ComponentModel;
using static Memory.Imps;
using System.Collections.Concurrent;
using System.Threading;

namespace Memory
{
Expand Down Expand Up @@ -118,7 +120,7 @@ public bool OpenProcess(int pid, out string FailReason)

mProc.MainModule = mProc.Process.MainModule;

GetModules();
//GetModules();

Debug.WriteLine("Process #" + mProc.Process + " is now open.");
FailReason = "";
Expand Down Expand Up @@ -181,14 +183,14 @@ public bool OpenProcess(int pid)
}*/

/// <summary>
/// Builds the process modules dictionary (names with addresses).
/// Builds the process modules dictionary (names with addresses). Use mProc.Process.Modules instead.
/// </summary>
public void GetModules()
/*public ConcurrentDictionary<string, IntPtr> GetModules()
{
if (mProc.Process == null)
{
Debug.WriteLine("mProc.Process is null so GetModules failed.");
return;
return null;
}
if (mProc.Is64Bit && IntPtr.Size != 8)
Expand All @@ -203,25 +205,26 @@ public void GetModules()
if (mProc.Process.Modules == null)
{
Debug.WriteLine("mProc.Process.Modules is null so GetModules failed.");
return;
return null;
}
if (mProc.Modules != null)
mProc.Modules.Clear();
else
mProc.Modules = new Dictionary<string, IntPtr>();
mProc.Modules = new ConcurrentDictionary<string, IntPtr>();
foreach (ProcessModule Module in mProc.Process.Modules)
{
if (Module.ModuleName == null || Module.BaseAddress == null)
continue;
if (!string.IsNullOrEmpty(Module.ModuleName) && !mProc.Modules.ContainsKey(Module.ModuleName))
mProc.Modules.Add(Module.ModuleName, Module.BaseAddress);
mProc.Modules.TryAdd(Module.ModuleName, Module.BaseAddress);
}
Debug.WriteLine("Found " + mProc.Modules.Count() + " process modules.");
}
return mProc.Modules;
}*/

public void SetFocus()
{
Expand Down Expand Up @@ -421,12 +424,12 @@ public UIntPtr GetCode(string name, string path = "", int size = 8)
{
try
{
altModule = mProc.Modules[moduleName[0]];
altModule = GetModuleAddressByName(moduleName[0]);
}
catch
{
Debug.WriteLine("Module " + moduleName[0] + " was not found in module list!");
Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
//Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
}
}
ReadProcessMemory(mProc.Handle, (UIntPtr)((int)altModule + offsets[0]), memoryAddress, (UIntPtr)size, IntPtr.Zero);
Expand Down Expand Up @@ -466,21 +469,31 @@ public UIntPtr GetCode(string name, string path = "", int size = 8)
{
try
{
altModule = mProc.Modules[moduleName[0]];
altModule = GetModuleAddressByName(moduleName[0]);
}
catch
{
Debug.WriteLine("Module " + moduleName[0] + " was not found in module list!");
Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
//Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
}
}
}
else
altModule = mProc.Modules[theCode.Split('+')[0]];
altModule = GetModuleAddressByName(theCode.Split('+')[0]);
return (UIntPtr)((int)altModule + trueCode);
}
}

/// <summary>
/// Retrieve mProc.Process module baseaddress by name
/// </summary>
/// <param name="name">name of module</param>
/// <returns></returns>
private IntPtr GetModuleAddressByName (string name)
{
return mProc.Process.Modules.Cast<ProcessModule>().SingleOrDefault(m => string.Equals(m.ModuleName, name, StringComparison.OrdinalIgnoreCase)).BaseAddress;
}

/// <summary>
/// Convert code from string to real address. If path is not blank, will pull from ini file.
/// </summary>
Expand Down Expand Up @@ -545,12 +558,12 @@ public UIntPtr Get64BitCode(string name, string path = "", int size = 16)
{
try
{
altModule = mProc.Modules[moduleName[0]];
altModule = GetModuleAddressByName(moduleName[0]);
}
catch
{
Debug.WriteLine("Module " + moduleName[0] + " was not found in module list!");
Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
//Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
}
}
ReadProcessMemory(mProc.Handle, (UIntPtr)((Int64)altModule + offsets[0]), memoryAddress, (UIntPtr)size, IntPtr.Zero);
Expand Down Expand Up @@ -589,17 +602,17 @@ public UIntPtr Get64BitCode(string name, string path = "", int size = 16)
{
try
{
altModule = mProc.Modules[moduleName[0]];
altModule = GetModuleAddressByName(moduleName[0]);
}
catch
{
Debug.WriteLine("Module " + moduleName[0] + " was not found in module list!");
Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
//Debug.WriteLine("Modules: " + string.Join(",", mProc.Modules));
}
}
}
else
altModule = mProc.Modules[theCode.Split('+')[0]];
altModule = GetModuleAddressByName(theCode.Split('+')[0]);
return (UIntPtr)((Int64)altModule + trueCode);
}
}
Expand Down

0 comments on commit c4ef051

Please sign in to comment.