【问题标题】:Can't kill a still running Outlook instance无法终止仍在运行的 Outlook 实例
【发布时间】:2013-09-04 14:27:07
【问题描述】:

我正在尝试杀死仍在运行的 Outlook 实例(用于我的应用程序而不是 Outlook 本身),我在解构器中调用以下内容

        //_app = Microsoft.Office.Interop.Outlook.Application
        _app.Quit();
        Marshal.FinalReleaseComObject(_app);
        Marshal.FinalReleaseComObject(_app.Session);
        GC.WaitForPendingFinalizers();
        GC.Collect();

但没有任何帮助,还有更多的方法可以杀死它吗?如果有,怎么做?

【问题讨论】:

    标签: c# outlook


    【解决方案1】:

    首先导入 user32.dll 以便能够使用 GetWindowThreadProcessId

    然后Kill方法通过参数接收outlookapp获取进程并杀死它

    public static class OutlookKiller
    {
            [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
    CharSet = CharSet.Unicode, ExactSpelling = true,
    CallingConvention = CallingConvention.StdCall)]
        private static extern long GetWindowThreadProcessId(long hWnd, out long lpdwProcessId);
    
        public static void Kill(ref Microsoft.Office.Interop.Outlook.Application app)
        {
            long processId = 0;
            long appHwnd = (long)app.Hwnd;
    
            GetWindowThreadProcessId(appHwnd, out processId);
    
            Process prc = Process.GetProcessById((int)processId);
            prc.Kill();
        }
    }
    

    【讨论】:

    • 你能解释一下代码吗?如果你从某个地方得到它,在哪里?了解这一点很重要,这样其他遇到它的人才能真正知道 a) 它来自哪里,b) 它的一般 概念是什么。
    • @Mauricio Gracia 感谢您的努力,但(长)app.Hwnd 似乎不正确
    • @Daapii "似乎" 意味着你测试过并且它失败了?
    • @Daapii 我在 stackoverflow 的另一个答案中发现了该代码,但你是对的 .HWND 属性不存在(或缺少某些东西)
    猜你喜欢
    • 2020-03-26
    • 2013-12-23
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2017-05-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多