예제 #1
0
        /// <summary>
        /// Initializes a spy from an executable file.
        /// </summary>
        /// <param name="path">Path to executable file.</param>
        public void Init(string path)
        {
            string pathDir = Path.GetDirectoryName(path);

            NativeMethods.STARTUPINFO         startupInfo = new NativeMethods.STARTUPINFO();
            NativeMethods.PROCESS_INFORMATION processInfo;

            if (!NativeMethods.CreateProcess(path, null, IntPtr.Zero, IntPtr.Zero, false,
                                             NativeMethods.CreationFlag.DEBUG_PROCESS, IntPtr.Zero, pathDir, ref startupInfo, out processInfo))
            {
                throw new Win32Exception();
            }

            NativeMethods.CloseHandle(processInfo.hThread);

            m_Process       = Process.GetProcessById((int)processInfo.dwProcessId);
            m_ProcessHandle = processInfo.hProcess;

            InitBreakpoints();
        }
        // --------------------------------------------------------------
        #region PUBLIC FUNCTIONS
        // --------------------------------------------------------------

        /// <summary>
        /// Initializes a spy from an executable file.
        /// </summary>
        /// <param name="fileName">Path to executable file.</param>
        public void Init(string fileName)
        {
            // reset the safe to stop flag
            SafeToStop = false;

            // make sure the dictionary is empty before we start
            m_Dictionary.Clear();

            // if the file doesn't exist, throw an exception
            if (!File.Exists(fileName))
            {
                throw new Exception(string.Format("Cannot find {0}!", Path.GetFileName(fileName)));
            }

            // get the executable path
            string pathDir = Path.GetDirectoryName(fileName);

            // create a temporary startup info events tracker
            NativeMethods.STARTUPINFO startupInfo = new NativeMethods.STARTUPINFO();

            // try to create the process, if it fails, throws an exception
            if (!NativeMethods.CreateProcess(fileName, null, IntPtr.Zero, IntPtr.Zero, false, NativeMethods.CreationFlag.DEBUG_PROCESS, IntPtr.Zero, pathDir, ref startupInfo, out NativeMethods.PROCESS_INFORMATION processInfo))
            {
                throw new Win32Exception();
            }

            // get the thread process data
            NativeMethods.CloseHandle(processInfo.hThread);

            // get the process
            m_Process = Process.GetProcessById((int)processInfo.dwProcessId);

            // get the process handle
            m_ProcessHandle = processInfo.hProcess;

            // initialize the breakpoints
            InitBreakpoints();
        }