From 1220219fb81c0ae771d543a3d2ac3846c6408c1a Mon Sep 17 00:00:00 2001 From: Jake Date: Wed, 22 Dec 2021 14:16:07 -0500 Subject: [PATCH] OpenProcess out FailReason Added an optional out for open process failure reason. --- Memory/memory.cs | 57 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/Memory/memory.cs b/Memory/memory.cs index 29a9acf..af9a140 100644 --- a/Memory/memory.cs +++ b/Memory/memory.cs @@ -62,7 +62,8 @@ public UIntPtr VirtualQueryEx(IntPtr hProcess, UIntPtr lpAddress, out MEMORY_BAS /// /// Use process name or process ID here. /// Process opened successfully or failed. - public bool OpenProcess(int pid) + /// Show reason open process fails + public bool OpenProcess(int pid, out string FailReason) { if (!IsAdmin()) { @@ -71,29 +72,34 @@ 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"); } @@ -101,8 +107,9 @@ public bool OpenProcess(int pid) { 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; } @@ -114,16 +121,28 @@ 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; } } - + + /// + /// Open the PC game process with all security and access rights. + /// + /// Use process name or process ID here. + /// Show reason open process fails + /// + public bool OpenProcess(string proc, out string FailReason) + { + return OpenProcess(GetProcIdFromName(proc), out FailReason); + } + /// /// Open the PC game process with all security and access rights. /// @@ -131,7 +150,17 @@ public bool OpenProcess(int pid) /// public bool OpenProcess(string proc) { - return OpenProcess(GetProcIdFromName(proc)); + return OpenProcess(GetProcIdFromName(proc), out string FailReason); + } + + /// + /// Open the PC game process with all security and access rights. + /// + /// Use process name or process ID here. + /// + public bool OpenProcess(int pid) + { + return OpenProcess(pid, out string FailReason); } /// @@ -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))