Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
public static class ProcessExtension
{
    [DllImport("kernel32.dll")]
    static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
    [DllImport("kernel32.dll")]
    static extern uint SuspendThread(IntPtr hThread);
    [DllImport("kernel32.dll")]
    static extern int ResumeThread(IntPtr hThread);

    public static void Suspend(this Process process)
    {
        foreach (ProcessThread thread in process.Threads)
        {
            var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
            if (pOpenThread == IntPtr.Zero)
            {
                break;
            }
            SuspendThread(pOpenThread);
        }
    }
    public static void Resume(this Process process)
    {
        foreach (ProcessThread thread in process.Threads)
        {
            var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
            if (pOpenThread == IntPtr.Zero)
            {
                break;
            }
            ResumeThread(pOpenThread);
        }
    }
}



Hello friends. I need to use pause process in my program and found this class on the net. But I do not know how to use it.
The processes that I will need to pause when I have PID are from the function below. Thanks for the help.

What I have tried:

C#
<pre> private void button3_Click_1(object sender, EventArgs e)
        {

            try
            {
                BIT64.Text = "";
                int pid = browser.Chrome();
                if (pid > 0)
                {

                    Aobscan.OpenProcessid(pid);
                    label1.Text = "Google Chrome = PID " + pid + "--" + BIT64.Text;
                    System.Media.SystemSounds.Beep.Play();
                }
                else
                {
                    pid = browser.Opera();

                    if (pid > 0)
                    {
                        Aobscan.OpenProcessid(pid);
                        label1.Text = "Opera = PID " + pid + "--" + BIT64.Text;
                        System.Media.SystemSounds.Beep.Play();
                    }
                    else
                    {
                        pid = browser.Gameroom_Flash();
                        if (pid > 0)
                        {
                            Aobscan.OpenProcessid(pid);
                            label1.Text = "FBGameroom = PID " + pid + "--" + BIT64.Text;
                            System.Media.SystemSounds.Beep.Play();
                        }
                        else
                        {
                            pid = browser.Mustang();
                            if (pid > 0)
                            {
                                Aobscan.OpenProcessid(pid);
                                label1.Text = "Mustang = PID " + pid + "--" + BIT64.Text;
                                System.Media.SystemSounds.Beep.Play();
                            }
                            else
                            {
                                pid = browser.Firefox_Flash();
                                if (pid > 0)
                                {
                                    Aobscan.OpenProcessid(pid);
                                    label1.Text = "Firefox_Flash = PID " + pid + "--" + BIT64.Text;
                                    System.Media.SystemSounds.Beep.Play();
                                }
                                else
                                {
                                    pid = browser.Psafe();
                                    if (pid > 0)
                                    {
                                        Aobscan.OpenProcessid(pid);
                                        label1.Text = "PsafeIternet = PID " + pid + "--" + BIT64.Text;
                                        System.Media.SystemSounds.Beep.Play();
                                    }
                                    else
                                    {
                                        pid = browser.Maxthon();
                                        if (pid > 0)
                                        {
                                            Aobscan.OpenProcessid(pid);
                                            label1.Text = "Maxthon = PID " + pid + "--" + BIT64.Text;
                                            System.Media.SystemSounds.Beep.Play();
                                        }
                                        else
                                        {
                                            pid = browser.Edge_Flash();
                                            if (pid > 0)
                                            {
                                                Aobscan.OpenProcessid(pid);
                                                label1.Text = "Microsoft = PID " + pid + "--" + BIT64.Text;
                                                System.Media.SystemSounds.Beep.Play();
                                            }
                                            else
                                            {
                                                label1.Text = "OPS-Error Game open??";
                                                System.Media.SystemSounds.Hand.Play();
                                                Aobscan.processhandle = IntPtr.Zero;

                                            }
                                        }
                                    }



I need to use the pause for the pid that is in the mount. It has a simple way of doing it. How can I call the function to pause. Sorry, I'm an apprentice.


UPDATE i use this class

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    


public static class Pause

{

   
    

    [DllImport("kernel32.dll")]
    static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
    [DllImport("kernel32.dll")]
    static extern uint SuspendThread(IntPtr hThread);
    [DllImport("kernel32.dll")]
    static extern int ResumeThread(IntPtr hThread);

    public static void Suspend(this Process process)
    {
        foreach (ProcessThread thread in process.Threads)
        {
            var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
            if (pOpenThread == IntPtr.Zero)
            {
                break;
            }
            SuspendThread(pOpenThread);
        }
    }
    public static void Resume(this Process process)
    {
        foreach (ProcessThread thread in process.Threads)
        {
            var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
            if (pOpenThread == IntPtr.Zero)
            {
                break;
            }
            ResumeThread(pOpenThread);
        }
    }
    public static void Print(this Process process)
    {
        Console.WriteLine("{0,8}    {1}", process.Id, process.ProcessName);
    }


    public enum Options
    {
        List,
        Kill,
        Suspend,
        Resume
    }

    [Flags]
    public enum ThreadAccess : int
    {
        TERMINATE = (0x0001),
        SUSPEND_RESUME = (0x0002),
        GET_CONTEXT = (0x0008),
        SET_CONTEXT = (0x0010),
        SET_INFORMATION = (0x0020),
        QUERY_INFORMATION = (0x0040),
        SET_THREAD_TOKEN = (0x0080),
        IMPERSONATE = (0x0100),
        DIRECT_IMPERSONATION = (0x0200)
    }

    public class Param
    {
        public int PId { get; set; }
        public string Expression { get; set; }
        public Options Option { get; set; }
    }
}


and use like this

C#
private void button5_Click(object sender, EventArgs e)
       {
          foreach (var process in Process.GetProcessesByName("firefox"))


           process.Suspend();

       }



But I need it to pause the process I've already done. Pid How do I do this?
That way I have to always find the new process to do.

Thank you.
Posted
Updated 28-Jun-17 9:45am
v2

1 solution

You won't like it I think, but here is a way to do it in C#: .net - Suspend Process in C# - Stack Overflow[^]
 
Share this answer
 
Comments
ShakalX 28-Jun-17 14:49pm    
Yes that's where I found it, but I do not know how to use = /
RickZeeland 28-Jun-17 14:56pm    
Maybe it's better for you not to get involved with such dangerous code, just try again some years later when you have built up more experience in coding :)
ShakalX 28-Jun-17 15:10pm    
There is nothing dangerous. It is only pausing a process to load a code into the process memory. A cheat for a game. Only that. But it needs to be paused to allow time to inject before the game opens. o.O
Dave Kreskowiak 28-Jun-17 16:51pm    
Keep in mind that the game you're launching may not tolerate what you're doing. If the game crashes, oh well. There's nothing you can do about it, other than forget trying to use the cheat.

RickZeeland 29-Jun-17 5:45am    
He could try reverse engineering the game with OllyDbg for example, hahaha !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900