int ICorDebug.CreateProcess( string lpApplicationName, string lpCommandLine, _SECURITY_ATTRIBUTES lpProcessAttributes, _SECURITY_ATTRIBUTES lpThreadAttributes, int bInheritHandles, uint dwCreationFlags, System.IntPtr lpEnvironment, string lpCurrentDirectory, _STARTUPINFO lpStartupInfo, _PROCESS_INFORMATION lpProcessInformation, CorDebugCreateProcessFlags debuggingFlags, out ICorDebugProcess ppProcess )
        {
            ppProcess = null;

            return Utility.COM_HResults.E_NOTIMPL;
        }
        int IDebugRemoteCorDebug.CreateProcessEx(Microsoft.VisualStudio.Debugger.Interop.IDebugPort2 pPort, string lpApplicationName, string lpCommandLine, System.IntPtr lpProcessAttributes, System.IntPtr lpThreadAttributes, int bInheritHandles, uint dwCreationFlags, System.IntPtr lpEnvironment, string lpCurrentDirectory, ref CorDebugInterop._STARTUPINFO lpStartupInfo, ref CorDebugInterop._PROCESS_INFORMATION lpProcessInformation, uint debuggingFlags, out object ppProcess)
        {
            ppProcess = null;

            try
            {
                // CreateProcessEx() is guaranteed to return a valid process object, or throw an exception
                CorDebugProcess process = CorDebugProcess.CreateProcessEx(pPort, lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, ref lpStartupInfo, ref lpProcessInformation, debuggingFlags);

                // StartDebugging() will either get a connected device into a debuggable state and start the dispatch thread, or throw.
                process.StartDebugging(this, true);
                ppProcess = process;

                return(COM_HResults.S_OK);
            }
            catch (ProcessExitException)
            {
                MessageCentre.DebugMessage(Resources.ResourceStrings.InitializeProcessFailedProcessDied);
                return(COM_HResults.S_FALSE);
            }
            catch (Exception ex)
            {
                MessageCentre.DebugMessage(Resources.ResourceStrings.InitializeProcessFailed);
                MessageCentre.InternalErrorMessage(false, ex.Message);
                return(COM_HResults.S_FALSE);
            }
        }
        public static CorDebugProcess CreateProcessEx(IDebugPort2 pPort, string lpApplicationName, string lpCommandLine, System.IntPtr lpProcessAttributes, System.IntPtr lpThreadAttributes, int bInheritHandles, uint dwCreationFlags, System.IntPtr lpEnvironment, string lpCurrentDirectory, ref _STARTUPINFO lpStartupInfo, ref _PROCESS_INFORMATION lpProcessInformation, uint debuggingFlags)
        {
            DebugPort port = pPort as DebugPort;
            if (port == null)
                throw new Exception("IDebugPort2 object passed to Micro Framework package by Visual Studio is not a valid device port");

            CommandLineBuilder cb = new CommandLineBuilder(lpCommandLine);
            string[] args = cb.Arguments;
            string deployDeviceName = args[args.Length-1];

            //Extract deployDeviceName
            if (!deployDeviceName.StartsWith(CorDebugProcess.c_DeployDeviceName))
                throw new Exception(String.Format("\"{0}\" does not appear to be a valid Micro Framework device name", CorDebugProcess.c_DeployDeviceName));

            deployDeviceName = deployDeviceName.Substring(CorDebugProcess.c_DeployDeviceName.Length);
            cb.RemoveArguments(args.Length - 1, 1);

            lpCommandLine = cb.ToString();

            CorDebugProcess process = port.IsLocalPort
                ? new CorDebugProcess(port, null)
                : port.GetDeviceProcess(deployDeviceName, 60);

            if (process == null)
                throw new Exception("CorDebugProcess.CreateProcessEx() could not create or find the device process");

            process.StoreAssemblyPaths(args);
            process.InternalCreateProcess(port, lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, ref lpStartupInfo, ref lpProcessInformation, debuggingFlags);

            return process;
        }
 private void InternalCreateProcess(
     DebugPort   port,
     string      lpApplicationName,
     string      lpCommandLine,
     IntPtr      lpProcessAttributes,
     IntPtr      lpThreadAttributes,
     int         bInheritHandles,
     uint        dwCreationFlags,
     System.IntPtr lpEnvironment,
     string      lpCurrentDirectory,
     ref _STARTUPINFO lpStartupInfo,
     ref _PROCESS_INFORMATION lpProcessInformation,
     uint        debuggingFlags
     )
 {
     if (port.IsLocalPort)
         this.CreateEmulatorProcess(port, lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, ref lpStartupInfo, ref lpProcessInformation, debuggingFlags);
     else
         this.CreateDeviceProcess(port, lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, ref lpStartupInfo, ref lpProcessInformation, debuggingFlags);
 }
        private void CreateDeviceProcess(DebugPort port, string lpApplicationName, string lpCommandLine, System.IntPtr lpProcessAttributes, System.IntPtr lpThreadAttributes, int bInheritHandles, uint dwCreationFlags, System.IntPtr lpEnvironment, string lpCurrentDirectory, ref _STARTUPINFO lpStartupInfo, ref _PROCESS_INFORMATION lpProcessInformation, uint debuggingFlags)
        {
            bool fDidDeploy = true;     //What if we did a launch but no deploy to a device...

            if (!fDidDeploy)
            {
                this.Engine.RebootDevice(Engine.RebootOption.RebootClrWaitForDebugger);
            }

            DebugAssert(m_port == port);

            lpProcessInformation.dwProcessId = m_pid;

            CreateDummyThread(out lpProcessInformation.hThread, out lpProcessInformation.dwThreadId);
        }
        private void CreateEmulatorProcess(
            DebugPort   port,
            string      lpApplicationName,
            string      lpCommandLine,
            IntPtr      lpProcessAttributes,
            IntPtr      lpThreadAttributes,
            int         bInheritHandles,
            uint        dwCreationFlags,
            System.IntPtr lpEnvironment,
            string      lpCurrentDirectory,
            ref _STARTUPINFO lpStartupInfo,
            ref _PROCESS_INFORMATION lpProcessInformation,
            uint        debuggingFlags
            )
        {
            VsPackage.MessageCentre.DeploymentMsg(String.Format(DiagnosticStrings.EmulatorCommandLine, lpCommandLine));

            try
            {
                Process emuProcess = new Process();
                emuProcess.StartInfo.FileName = lpApplicationName;
                emuProcess.StartInfo.Arguments = lpCommandLine.Substring(lpApplicationName.Length+2);
                emuProcess.StartInfo.UseShellExecute = false;

                emuProcess.StartInfo.RedirectStandardOutput = true;
                emuProcess.StartInfo.RedirectStandardError = true;
                emuProcess.StartInfo.RedirectStandardInput = false;

                // Set our event handler to asynchronously read the emulator's outputs.
                emuProcess.OutputDataReceived += new DataReceivedEventHandler(VsPackage.MessageCentre.OutputMsgHandler);
                emuProcess.ErrorDataReceived += new DataReceivedEventHandler(VsPackage.MessageCentre.ErrorMsgHandler);

                emuProcess.StartInfo.WorkingDirectory = lpCurrentDirectory;

                // Start the process.
                if(!emuProcess.Start())
                    throw new Exception("Process.Start() returned false.");

                // Start the asynchronous reads of the emulator's output streams
                emuProcess.BeginOutputReadLine();
                emuProcess.BeginErrorReadLine();

                this.PortDefinition = new PortDefinition_Emulator("Emulator", emuProcess.Id);
                this.SetPid((uint)emuProcess.Id);

                port.AddProcess(this);

                const int DUPLICATE_SAME_ACCESS = 0x00000002;
                Utility.Kernel32.DuplicateHandle(Utility.Kernel32.GetCurrentProcess(), emuProcess.Handle,
                                                 Utility.Kernel32.GetCurrentProcess(), out lpProcessInformation.hProcess,
                                                 0, false, DUPLICATE_SAME_ACCESS);
                
                lpProcessInformation.dwProcessId = (uint)emuProcess.Id;
                CreateDummyThread(out lpProcessInformation.hThread, out lpProcessInformation.dwThreadId);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(String.Format("Could not create emulator process."),ex);
            }
        }
 public static extern bool CreateProcessW(string appName, string cmdLine, IntPtr lpProcessAttrs, IntPtr lpThreadAttrs, int bInheritHandles, uint dwCreatingFlags, IntPtr lpEnvironment, string curDir, ref _STARTUPINFO info, ref _PROCESS_INFORMATION pinfo);