Skip to content

Commit

Permalink
Program can now destroy the game and helper processes
Browse files Browse the repository at this point in the history
  • Loading branch information
BradF-99 committed Jun 19, 2019
1 parent f95db9c commit bfcf007
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 27 deletions.
46 changes: 31 additions & 15 deletions MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 67 additions & 12 deletions MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public static extern bool RegisterHotKey(IntPtr hWnd,
public static extern bool UnregisterHotKey(IntPtr hWnd,
int id);

private const int hotkeyID = 1;
private const int hotkeyNum = 0x0312;
private const int hotkeySuspend = 1;
private const int hotkeyKill = 2;

[Flags]
public enum ThreadAccess : int
Expand Down Expand Up @@ -48,13 +50,24 @@ public MainWindow()
{
InitializeComponent();

try
{
Process process = Process.GetProcessesByName("GTA5")[0];
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("The GTA game process could not be found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}

// Keycodes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8 (add together to change modifier)
RegisterHotKey(this.Handle, hotkeyID, 6, (int)Keys.F12);
RegisterHotKey(this.Handle, hotkeyKill, 6, (int)Keys.F11);
RegisterHotKey(this.Handle, hotkeySuspend, 6, (int)Keys.F12);
}

protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == hotkeyID)
if (m.Msg == hotkeyNum && m.WParam.ToInt32() == hotkeySuspend)
{
if (!isSuspended)
{
Expand All @@ -65,41 +78,45 @@ protected override void WndProc(ref Message m)
isSuspended = false;
}
}
else if (m.Msg == hotkeyNum && m.WParam.ToInt32() == hotkeyKill)
{
KillGTASocialClubProcess();
}

base.WndProc(ref m);
}

private static void SuspendProcess()
{
var process = Process.GetProcessesByName("GTA5")[0]; // there's probably only going to be one instance
var gtaProcess = Process.GetProcessesByName("GTA5")[0]; // there's probably only going to be one instance

if (process.ProcessName == string.Empty)
if (gtaProcess.ProcessName == string.Empty)
return;

foreach (ProcessThread pT in process.Threads)
foreach (ProcessThread thread in gtaProcess.Threads)
{
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);

if (pOpenThread == IntPtr.Zero)
{
continue;
}

SuspendThread(pOpenThread);

CloseHandle(pOpenThread);
}
}

public static void ResumeProcess()
{
var process = Process.GetProcessesByName("GTA5")[0];
Process gtaProcess = Process.GetProcessesByName("GTA5")[0];

if (process.ProcessName == string.Empty)
if (gtaProcess.ProcessName == string.Empty)
return;

foreach (ProcessThread pT in process.Threads)
foreach (ProcessThread thread in gtaProcess.Threads)
{
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);

if (pOpenThread == IntPtr.Zero)
{
Expand All @@ -116,9 +133,47 @@ public static void ResumeProcess()
}
}

public static void KillGTASocialClubProcess()
{
try
{
Process gtaProcess = Process.GetProcessesByName("GTA5")[0];
Process gtaLauncherProcess = Process.GetProcessesByName("GTAVLauncher")[0];
Process[] socialClubProcesses = Process.GetProcessesByName("SocialClubHelper");

gtaProcess.Kill();
gtaLauncherProcess.Kill();

if (socialClubProcesses.Length <= 0)
return;

foreach (Process process in socialClubProcesses)
{
process.Kill();
}
}
catch (NotSupportedException)
{
// this shouldnt even happen???
}
catch (InvalidOperationException)
{
// process has already exited or doesn't exist
}
catch
{
MessageBox.Show("Something went wrong.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void Label1_Click(object sender, EventArgs e)
{

}

private void Label2_Click(object sender, EventArgs e)
{

}
}
}

0 comments on commit bfcf007

Please sign in to comment.