0

我想使用 shell 执行显示一个 tiff 文件。我假设默认应用程序是照片查看器。我的问题是,当我想用​​ photoviewer.Kill() 终止进程时,我得到一个 System.InvalidOperationException。在 photoViewer.Start() 之后设置断点时,我意识到 photoviewer 不包含 ID。我有足够的方法杀死它吗?当它通过 dllhost.exe 运行时,我不想重新运行所有名为 dllhost 的进程并将它们全部杀死,因为我不知道 dllhost 还运行什么。

Process photoViewer = new Process();
  private void StartProcessUsingShellExecute(string filePath)
        {
            photoViewer.StartInfo = new ProcessStartInfo(filePath);
            photoViewer.StartInfo.UseShellExecute = true;
            photoViewer.Start();
        }

我有另一种没有 shell 执行的方法,但这种方法似乎有 dpi 问题。 没有shell执行的方法

4

1 回答 1

0

找到了解决方案,可以帮助任何有类似问题的人。当我查看任务管理器时,我发现 Windows 10 photoviewer 通过 dllhost 与应用程序分离运行。因此,由于我有 4 个 dllhost 进程启动并运行,并且只想关闭窗口。我愿意:

        private void StartProcessAsShellExecute(string filePath)
    {
        photoViewer.StartInfo = new ProcessStartInfo(filePath);
        photoViewer.StartInfo.UseShellExecute = true;
        photoViewer.Start();

        Process[] processes = Process.GetProcessesByName("dllhost");

        foreach (Process p in processes)
        {
            IntPtr windowHandle = p.MainWindowHandle;
            CloseWindow(windowHandle);
            // do something with windowHandle
        }


        viewerOpen = true;
    }


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    //I'd double check this constant, just in case
    static uint WM_CLOSE = 0x10;

    public void CloseWindow(IntPtr hWindow)
    {
        SendMessage(hWindow, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
    }

关闭所有 dllhost 窗口(其中我只有 1 个,即照片查看器)

于 2020-11-11T08:16:57.920 回答