Skip to content

Commit

Permalink
OpenProcess out FailReason
Browse files Browse the repository at this point in the history
Added an optional out for open process failure reason.
  • Loading branch information
erfg12 committed Dec 22, 2021
1 parent a1bbab0 commit 1220219
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions Memory/memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public UIntPtr VirtualQueryEx(IntPtr hProcess, UIntPtr lpAddress, out MEMORY_BAS
/// </summary>
/// <param name="pid">Use process name or process ID here.</param>
/// <returns>Process opened successfully or failed.</returns>
public bool OpenProcess(int pid)
/// <param name="FailReason">Show reason open process fails</param>
public bool OpenProcess(int pid, out string FailReason)
{
if (!IsAdmin())
{
Expand All @@ -71,38 +72,44 @@ public bool OpenProcess(int pid)

if (pid <= 0)
{
Debug.WriteLine("ERROR: OpenProcess given proc ID 0.");
FailReason = "OpenProcess given proc ID 0.";
Debug.WriteLine("ERROR: OpenProcess given proc ID 0.");
return false;
}


if (mProc.Process != null && mProc.Process.Id == pid)
return true;
{
FailReason = "mProc.Process is null";
return true;
}

try
{
mProc.Process = System.Diagnostics.Process.GetProcessById(pid);
mProc.Process = Process.GetProcessById(pid);

if (mProc.Process != null && !mProc.Process.Responding)
{
Debug.WriteLine("ERROR: OpenProcess: Process is not responding or null.");
FailReason = "Process is not responding or null.";
return false;
}

mProc.Handle = Imps.OpenProcess(0x1F0FFF, true, pid);

try {
System.Diagnostics.Process.EnterDebugMode();
} catch (Win32Exception) {
Process.EnterDebugMode();
} catch (Win32Exception) {
//Debug.WriteLine("WARNING: You are not running with raised privileges! Visit https://github.com/erfg12/memory.dll/wiki/Administrative-Privileges");
}

if (mProc.Handle == IntPtr.Zero)
{
var eCode = Marshal.GetLastWin32Error();
Debug.WriteLine("ERROR: OpenProcess has failed opening a handle to the target process (GetLastWin32ErrorCode: " + eCode + ")");
System.Diagnostics.Process.LeaveDebugMode();
Process.LeaveDebugMode();
mProc = null;
FailReason = "failed opening a handle to the target process(GetLastWin32ErrorCode: " + eCode + ")";
return false;
}

Expand All @@ -114,24 +121,46 @@ public bool OpenProcess(int pid)
GetModules();

Debug.WriteLine("Process #" + mProc.Process + " is now open.");

FailReason = "";
return true;
}
catch (Exception ex) {
Debug.WriteLine("ERROR: OpenProcess has crashed. " + ex);
FailReason = "OpenProcess has crashed. " + ex;
return false;
}
}



/// <summary>
/// Open the PC game process with all security and access rights.
/// </summary>
/// <param name="proc">Use process name or process ID here.</param>
/// <param name="FailReason">Show reason open process fails</param>
/// <returns></returns>
public bool OpenProcess(string proc, out string FailReason)
{
return OpenProcess(GetProcIdFromName(proc), out FailReason);
}

/// <summary>
/// Open the PC game process with all security and access rights.
/// </summary>
/// <param name="proc">Use process name or process ID here.</param>
/// <returns></returns>
public bool OpenProcess(string proc)
{
return OpenProcess(GetProcIdFromName(proc));
return OpenProcess(GetProcIdFromName(proc), out string FailReason);
}

/// <summary>
/// Open the PC game process with all security and access rights.
/// </summary>
/// <param name="pid">Use process name or process ID here.</param>
/// <returns></returns>
public bool OpenProcess(int pid)
{
return OpenProcess(pid, out string FailReason);
}

/// <summary>
Expand Down Expand Up @@ -583,6 +612,12 @@ public bool InjectDll(String strDllName, bool Execute = false, string LoadLibrar
{
IntPtr bytesout;

if (mProc.Process == null)
{ // check if process is open first
Debug.WriteLine("Inject failed due to mProc.Process being null. Is the process not open?");
return false;
}

foreach (ProcessModule pm in mProc.Process.Modules)
{
if (pm.ModuleName.StartsWith("inject", StringComparison.InvariantCultureIgnoreCase))
Expand Down

0 comments on commit 1220219

Please sign in to comment.