2

The end goal of what I am trying to do is get the MMC (Microsoft Management Console) Computer Management snap-in (compmgmt.msc) process to be embedded into a Windows Form, or a workaround that will treat it like a modal pop-up menu.

Right now, I am just trying to get mmc.exe itself working, before I try to load a snap-in. The first part of the problem is the mmc.exe process almost exits immediately.

Edit: mmc.exe only exits immediately if the application is built as 32-bit (my machine is 64-bit). If the application is built to be 64-bit, the first process stays, which is what the expected behavior is. However, I am still curious for an explanation as to why the strange temporary process behavior occurs. Note that the temporary mmc.exe process that is launched is 32-bit, but the final mmc.exe launched is 64-bit. Strange.

The following code will successfully embed iexplore.exe inside a Windows Form, but it fails to embed mmc.exe. The reason it fails is an exception that occurs at the call to p.WaitForInputIdle();

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: Cannot process request because the process has exited.

As you can see from the error message, the process exits within milliseconds, but from a user's point of view, the MMC's GUI does still pop up as a separate, unrelated process to the original one I started.

This means that another mmc.exe process is being created which seems to have no connection to the original process created.

So the question is: Why is the MMC process immediately closing, with another MMC process opening almost immediately?

Relevant Windows Form code, similar to this question.

[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

private void Form1_KeyPress(object sender, EventArgs e)
{
    /// Uncomment *one* of these:
    //Process p = Process.Start("mmc.exe");
    //Process p = Process.Start("iexplore.exe");
    Thread.Sleep(500);

    p.WaitForInputIdle();

    Console.WriteLine("MainWindowHandle: " + p.MainWindowHandle);
    Console.WriteLine("Handle: " + p.Handle);

    Thread.Sleep(5000);
    SetParent(p.MainWindowHandle, this.Handle);
}

Related, but question seems to be more about the Console GUI itself closing, not allowing for editing, as opposed to some underlying process closing. https://superuser.com/questions/194252/mmc-exe-starts-and-then-immediately-stops



A subsequent issue to this was that even when I located the new mmc process that pops up, it appears to have MainWindowHandle set to null, possibly meaning Windows doesn't recognize it as having a GUI.

The workaround to this was as simple adding sleep (a pause) between creating the "temporary" mmc process, and waiting for the new process to actually be ready. Note that process.WaitForInputIdle(); did not serve as a long-enough wait.

For anyone that might be having the same trouble as I had:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Environment.SystemDirectory + "\\" + "mmc.exe";
startInfo.Arguments = "\"" + Environment.SystemDirectory + "\\compmgmt.msc\" /s";
Process tempProcess = Process.Start(startInfo);
tempProcess.WaitForExit();

Thread.Sleep(500); // Added pause!
// Better alternative is to use a while loop on (MainWindowHandle == null)
// with some sort of timeout

Process[] processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(startInfo.FileName));
foreach (Process process in processes)
{
    // do what you want with the process
    Console.WriteLine("MainWindowHandle: " + process.MainWindowHandle);
    // Set Computer Management window on top
    SetWindowPos(process.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
    SetParent(process.MainWindowHandle, this.Handle);
    SetWindowLong(process.MainWindowHandle, GWL_STYLE, WS_VISIBLE);

    process.WaitForExit();
}

But the main issue is figuring out why the first MMC process exits.

Talset
  • 61
  • 2
  • 18
  • Are you launching the process as an Administrator? – John Wu Nov 02 '17 at 20:39
  • If you mean the compiled MyWinForm.exe itself, yes I still get same behavior when running as Administrator. If you mean the Process.Start("mmc.exe") portion of the code, then I'm not sure -- are there extra arguments I should add to make sure it's started at admin level? Thanks for reading! – Talset Nov 02 '17 at 20:51
  • So, I realize my question may not have been the most well-formed, because it's almost a two-part question. I found a workaround to the second part of my question, about MainWindowHandle being null, and will edit to make things more clear. The first question about why the process exits in the first place is what still baffles me, however. – Talset Nov 02 '17 at 23:21
  • the only workaround was Thread.Sleep(5000); for me – Daniel B Nov 03 '17 at 00:03
  • Update: This behavior of the first mmc.exe instance closing only happens if the application is run as 32-bit. If I set the windows form to build as 64-bit, the "tempProcess" stays active, like you would normally expect when doing Process.Start() – Talset Nov 09 '17 at 22:26

1 Answers1

1

It exits because it may need to use a different bit-depth of MMC to run the snapin. As I am just learning now, snapins can be 32-bit or 64-bit. Windows may need to restart the snapin using either C:\Windows\SysWOW64\mmc.exe (1.34 MB (1,409,024 bytes)) or using C:\Windows\System32\mmc.exe (1.71 MB (1,802,240 bytes)). https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms692753(v=vs.85)

So, for me, the task at hand seems to be how to discover, before launching a snapin, that snapin's bit-depth.