Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / Windows Forms

A tiny application to launch a suspended Process

Rate me:
Please Sign up or sign in to vote.
3.95/5 (9 votes)
24 Jul 2011CPOL2 min read 66.7K   3.9K   16  
A tiny application to launch a process suspended to allow, e.g., attaching a remote debugger.
using System;

namespace StartSuspended
{
    class ProcessLauncher
    {
        IntPtr ThreadHandle = IntPtr.Zero;

        public bool LaunchProcessSuspended(string processpath, int initialResumeTime, out uint PID)
        {
            STARTUPINFO si = new STARTUPINFO();
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
            bool success = NativeMethods.CreateProcess(processpath, null, IntPtr.Zero, IntPtr.Zero, false, ProcessCreationFlags.CREATE_SUSPENDED, IntPtr.Zero, null, ref si, out pi);
            ThreadHandle = pi.hThread;
            PID = pi.dwProcessId;

            if (initialResumeTime > 0)
            {
                NativeMethods.ResumeThread(ThreadHandle);
                System.Threading.Thread.Sleep(initialResumeTime);
                NativeMethods.SuspendThread(ThreadHandle);
            }

            return success;
        }

        public void ResumeProcess()
        {
            NativeMethods.ResumeThread(ThreadHandle);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions