-
-
Notifications
You must be signed in to change notification settings - Fork 207
Multiple Game Process Switching
This example assumes you have several game clients all with the same process name (Ex: WoW).
We will be building a ListView (Ex: procBox) for storing our running game process IDs, and a BackgroundWorker (Ex: BackgroundWorker1) for our infinite loop for process open/close detection. When the user has focus on a game window, the trainer's ListView selected item will switch to the currently focused game window process ID.
Because the backgroundWorker speaks with some main thread UI elements we will have to use an invoke method. I recommend using the backgroundWorker RunWorkerAsync in the form's _Shown event function instead of the _Load event function because the _Load event can happen before the UI elements have loaded in, which would cause an error. For ease of use, I'm going to leave this example with the _Load event.
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(); // start background worker, this should really be in the Form's Shown event
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (true) // infinite loop
{
Process[] processlist = Process.GetProcesses();
// check for new processes to add to our list
foreach (Process theprocess in processlist)
{
// only add processes that are not currently in the list
if (theprocess.ProcessName == "WoW" && !procBox.Items.Contains(theprocess.Id.ToString()))
{
m.OpenProcess(theprocess.Id); // open for reading
Debug.WriteLine("procID " + theprocess.Id.ToString() + " has been added to the list.");
if (m.readString("0x12345678", "", 6).Contains("1.12.1"))
{ // check if it's actually a vanilla WoW 1.12.1 client
procBox.Invoke(new MethodInvoker(delegate
{
procBox.Items.Add(theprocess.Id.ToString()); // _SelectedIndexChanged will do the rest
}));
}
}
}
// store our procBox list in another list prior to removing the item from the procBox list to prevent an error
List<string> procBoxList = new List<string>();
foreach (string listItem in procBox.Items)
{
procBoxList.Add(listItem);
}
// check for dead processes and remove them
foreach (string listItem in procBoxList)
{
try
{
Process.GetProcessById(Convert.ToInt32(listItem));
}
catch
{
procBox.Invoke(new MethodInvoker(delegate
{
procBox.Items.RemoveAt(procBox.FindString(listItem));
}));
Debug.WriteLine("procID " + listItem + " has been removed from the list.");
}
}
// check if the game is currently active
string activeProc = GetActiveProcessID();
if (procBox.Items.Contains(activeProc))
{ // our WoW proc list contains our active process ID
procBox.Invoke(new MethodInvoker(delegate
{
procBox.SelectedIndex = procBox.FindString(activeProc); // select it from our list
}));
}
}
}
private void procBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (procBox.Items.Count > 0 && procBox.SelectedItems.Count > 0)
{
int selectedProc = Convert.ToInt32(procBox.SelectedItems[0]);
if (selectedProc > 0)
{
Debug.WriteLine("Now changing processes to " + selectedProc + "...");
m.OpenProcess(selectedProc);
// we have now successfully swapped to another game process!
}
}
}