In Windows 10, Control Panel isn't considered a separate process. It shares the process name and process id (PID) of the File Explorer (explorer) which can be seen in the Task Manager. Because of this, it's not possible to programmatically terminate the Control Panel without terminating the File Explorer.Is there a command or a code to get this done?
There were two approaches I tried.
The first one is to get a list of open windows and match the window.LocationName with "Control Panel". This doesn't work when I'm inside one of the settings, say, "System and Security" because the window.LocationName will be changed to "System and Security". Here's the gist of code:
Shell32.Shell shell = new Shell32.Shell();
System.Collections.IEnumerable windows = shell.Windows() as System.Collections.IEnumerable;
if(windows != null) {
foreach (SHDocVw.InternetExplorer window in windows) {
object doc = window.Document;
if (doc != null && doc is Shell32.ShellFolderView) {
if(window.LocationName== "Control Panel") {
window.Quit();
}}}}
The second approach I tried goes something like this:
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr hWndTargetWindow = FindWindow("CabinetWClass", null);
SendMessage(hWndTargetWindow, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
Environment.Exit(0);
So, the CabinetWClass is the classname for explorer and FindWindow() returns a pointer to the last opened window of the explorer. If Control Panel is the last window opened, then it'll successfully be closed. But if there's a File Explorer window opened after Control Panel, then it's the File Explorer that'll get closed first and only after it's closed will the pointer point to the Control Panel instance which can then be terminated. But I don't want to close the File Explorer window.
Is there a way to programmatically identify if the Control Panel window is open and terminate it? Any help is appreciated