/// <summary> /// Starts the shell. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName">Name of the file.</param> /// <param name="arguments">The arguments.</param> /// <param name="targetShell">The target shell.</param> /// <param name="workingFolder">The working folder.</param> /// <param name="tempCsFile">The temporary cs file.</param> /// <param name="reference">The reference.</param> /// <returns></returns> private T StartShell <T>(string fileName, string arguments, string targetShell, string workingFolder, string tempCsFile, out object reference) { reference = null; var retval = default(T); using (var shellProcess = new Process() { StartInfo = new ProcessStartInfo() { FileName = $@"{GetClr4Path()}csc.exe", Arguments = $"/target:winexe \"{workingFolder}{tempCsFile} \"", WindowStyle = ProcessWindowStyle.Hidden, WorkingDirectory = workingFolder, UseShellExecute = false, RedirectStandardOutput = true } }) { if (shellProcess.Start()) { // Let's start recently created shell ApplicationLauncher.ProcessInformation pi; #if DEBUG var output = shellProcess.StandardOutput.ReadToEnd(); #endif if (ApplicationLauncher.Current.StartProcessInteractively(new AutomationRequest() { FileName = targetShell }, out pi)) { var winsta = new List <string>(); ApplicationLauncher.EnumWindowStations((x, s) => { winsta.Add(x); return(true); }, IntPtr.Zero); var sessions = ApplicationLauncher.Current.ListSessions(Environment.GetEnvironmentVariable("ComputerName")); if (!ApplicationLauncher.EnumWindows(EnumChildWindowProcedure, (int)pi.dwProcessId)) { int z = 0; } //var targetProcess = Process.GetProcesses().First(_ => _.Id == pi.dwProcessId); //var shell = Form.FromHandle(targetProcess.MainWindowHandle); //Thread.Sleep(1500); //var hwnd = ApplicationLauncher.FindWindow(null, "UnifiedShell" /*new StringBuilder("UnifiedShell")*/); } } } return(retval); }
/// <summary> /// Initializes a new instance of the <see cref="WtsOperator"/> class. /// </summary> public WtsOperator() { uint pSessionId = 0; var channelName = "SIF"; if (ApplicationLauncher.ProcessIdToSessionId((uint)Process.GetCurrentProcess().Id, ref pSessionId)) { // hVirtualChannel = ApplicationLauncher.WTSVirtualChannelOpen(IntPtr.Zero, -1 /* (int)pSessionId */, channelName); hVirtualChannel = ApplicationLauncher.WTSVirtualChannelOpenEx(0, channelName, 1); // WTS_CHANNEL_OPTION_DYNAMIC var err = Marshal.GetLastWin32Error(); System.Diagnostics.Debugger.Launch(); } }
/// <summary> /// Enums the child window procedure. /// </summary> /// <param name="hWnd">The h WND.</param> /// <param name="lParam">The l parameter.</param> /// <returns></returns> private bool EnumChildWindowProcedure(IntPtr hWnd, int lParam) { var retval = true; long processId = 0; ApplicationLauncher.GetWindowThreadProcessId(hWnd, out processId); System.Diagnostics.Trace.WriteLine($"Selected hWnd:{hWnd.ToInt32()} - Target PID:{lParam} - Selected PID:{processId}"); if (/* ApplicationLauncher.GetWindowThreadProcessId(hWnd, out processId) > 0 && */ processId == lParam) { ShellhWnd = hWnd; retval = false; } return(retval); }