应用程序退出时终止进程外 dll ('dllhost.exe' 'COM Surrogate') 的最佳方法?

Best way to kill an out of process dll ('dllhost.exe' 'COM Surrogate') when an App exits?

我正在使用 'DllSurrogate' 方法允许 64 位 C# exe 与 32 位 C# dll 通信。 Hosting a .NET DLL as an Out-Of-Process COM Server (EXE).

中描述了该方法

当应用程序运行一个名为 'dllhost.exe *32' 的额外进程时,它会自动显示在任务管理器中,并带有描述 'COM Surrogate'。 这是App和32位dll之间的link

我遇到的问题是应用程序退出时没有杀死这个进程。 有人可以建议推荐的处理方法吗?

我可以找到进程并在我的应用程序关闭时终止它,但我需要确保:

我的应用程序和我可以检查的这个过程之间是否有更直接的link?

拒绝终止的 COM 代理通常表示对尚未释放的 CoClass 或资源的未完成引用。您可以诉诸 ReleaseComObject or FinalReleaseComObject. However, when using this approach, you should be aware of the associated risks, as described here 强制释放。

如果除了手动终止 COM 代理之外仍然看不到其他方法,则需要枚举计算机上的所有 dllhost.exe 进程。提取每个候选人的命令行并查找 /ProcessId 参数。如果它与您的 CoClassGUID 相匹配,那么您就找到了匹配项。