I have the following very simple code (edited):
public void MyFunction(string inPath, string inArguments)
{
// Configure the process using the StartInfo properties
Process process = new Process();
process.StartInfo.FileName = inPath;
process.StartInfo.Arguments = inArguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
uint ENABLE_QUICK_EDIT = 0x0040;
IntPtr handle;
UInt32 consoleMode;
// Attempt type 1
// (get external process handle via window name - fails, QuickEdit mode still active)
handle = FindWindowByCaption(IntPtr.Zero, "Window_Name");
GetConsoleMode(handle, out consoleMode); // fails
consoleMode &= ~ENABLE_QUICK_EDIT;
SetConsoleMode(handle, consoleMode); // fails
// Attempt type 2
// (get external process handle via explicit process ID - fails, QuickEdit mode still active)
AttachConsole((uint)process.Id);
handle = GetConsoleWindow(); // fails
GetConsoleMode(handle, out consoleMode);
consoleMode &= ~ENABLE_QUICK_EDIT;
SetConsoleMode(handle, consoleMode); // fails
process.WaitForExit(); // Waits here for the process to exit.
}
With the relevant native functions being (edited):
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FreeConsole();
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
static extern bool AttachConsole(uint dwProcessId);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
This code opens a command console as a call to the external .exe file referenced in inPath. What do I need to add to ensure that the new console window instance is created with QuickEdit mode turned off? Do I need to somehow map this property onto the object before it instantiates the console window?
I have been asking myself this question for several days at this point. I have found some solutions on the net, e.g. these:
- Script commands to disable quick edit mode
- How to programmatic disable C# Console Application's Quick Edit mode?
EDIT links:
- Good reference for native functions: http://www.pinvoke.net/default.aspx/kernel32.GetConsoleWindow
Getting a process handle for external processes: How do I get the handle of a console application's window
and others....
But still, nothing has worked for my case so far. Any help would be very much appreciated.
Thank you all very much in advance!