Skip to content

Multiple Game Process Switching

New Age Soldier edited this page Oct 4, 2020 · 16 revisions

These examples assumes you have several game clients all with the same process name (Ex: WoW)

Example 1

Read/Write to a selected process in a list.

Build a ListView (Ex: procBox) for storing our running game process IDs, and an infinite loop for dynamic list updating.

            // infinite loop, do this in a different thread. Maybe in a background worker.
            while (true)
            {
                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.");
                        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.");
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }

        // If the listview selected item changes, swap the process
        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 process to " + selectedProc);
                    m.OpenProcess(selectedProc);
                    // we have now successfully swapped to another game process!
                    // use m.theProc to reference the newly selected process.
                }
            }
        }

Example 2

Read/write to ALL processes with the same name

    // infinite loop, do this in a different thread. Maybe in a background worker.
    while (true)
            {
                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")
                    {
                        m.OpenProcess(theprocess.Id); // open for reading
                        // place your read/write functions here.
                    }
                }
            }

Example 3

Read/write to game window that is curently in focus

    using System.Runtime.InteropServices;

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    string GetActiveProcessID()
        {
            IntPtr hwnd = GetForegroundWindow();
            uint pid;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid.ToString();
        }

    // infinite loop, do this in a different thread. Maybe in a background worker.
    while (true)
            {
                Process[] processlist = Process.GetProcesses();
                string activeProc = GetActiveProcessID();

                // iterate through all our processes
                foreach (Process theprocess in processlist)
                {
                    string activeProc = GetActiveProcessID();
                    if (theprocess.Id == activeProc && theprocess.ProcessName == "WoW")
                    { // our WoW proc list contains our focused window process ID
                        // place your read/write functions here
                    }
                }
            }