MMC process immediately closing, can't link to windows forms

主宰稳场 提交于 2021-02-19 05:36:05

问题


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.


回答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://docs.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.



来源:https://stackoverflow.com/questions/47083917/mmc-process-immediately-closing-cant-link-to-windows-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!