如何判断本地服务器进程是否由 COM 启动?

How to tell if a local server process was launched by COM?

我有一个支持进程外 COM 自动化的应用程序。现在,我希望它在由用户启动时和由 COM 启动以服务于自动化请求时能够有不同的行为。我如何从应用程序中区分这些情况?

谷歌搜索一无所获,到目前为止我唯一的想法是确定父进程,但我不确定它会有多脆弱。

当 COM 启动本地 out-of-process 服务器时,它会在注册表中查找托管进程路径并附加“-Embedding”命令行参数。这是您应该查看的地方,分别检测 COM 启动。

记录了行为 here:

COM appends the "-Embedding" flag to the string, so the application that uses flags needs to parse the whole string and check for the -Embedding flag.