防止用户附加到MS-Office进程的按开发人员运行的实例 [英] Prevent user from attaching to run-by-developer instance of MS-Office process

查看:119
本文介绍了防止用户附加到MS-Office进程的按开发人员运行的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题: 在我通过C ++代码(Word,Excel,PowerPoint,Visio)中的COM启动任何Ms-Office应用程序并将其隐藏之后-如果用户启动了该应用程序的实例,它将附加到按我的代码运行办公室流程,而我对此一无所知.最终,在执行完我的代码后,我关闭了Office Application,用户将丢失其工作(就他们附加到我的进程而未启动自己的进程而言)

I encounter the following issue: After I start any Ms-Office application via COM in my C++ code (Word, Excel, PowerPoint, Visio) and make it hidden - then if user starts its own intance of that application - it will attach to run-by-my-code office process, while I have no idea of that. Eventually after my code has executed I close Office Application and a user will lose its work (As far as they attached to my process and didn't start its own one)

所以

1)有什么方法可以防止用户附加到我的Office Application实例并启动其自己的进程?也许是CoCreateInstance的参数,还是其他?

1) is there any way to prevent a user from attaching to my instance of Office Application and start its own process? Maybe a parameter for CoCreateInstance, or something else?

2)或(另一种选择)-如何检测该用户刚刚附加到按需运行办公室流程?

2) Or (Another option) - how to detect that user has just attached to run-by-me office process ?

将感谢您的帮助.

这是一段有关如何创建Office应用程序的代码

Here is a piece of code on how I create Office Application

CComPtr<IDispatch> pOffApp;
hr = pOfficeApp.CoCreateInstance(L"Word.Application", NULL, CLSCTX_LOCAL_SERVER); //may also be "Excel.Application", "Visio.Application" etc.

推荐答案

曾经有一篇不错的知识库文章,但是已经消失了……该要点在MSDN论坛的答案中由Bessie Zhao发表,我将其复制到此处,并从下面的KB说明中可以记住:

There used to be a good KB article on this, but it's disappeared... The essence was posted in an answer on the MSDN forums, by Bessie Zhao, which I copy here, with what I can remember from the KB explanation below it:

您是否尝试过KB 188546的变通办法: http://support.microsoft.com/kb/188546/EN-US/?它介绍了一种方法 以下.在创建Word对象之前,请先创建一个临时对象 字对象.创建对象后,关闭临时对象. 当您通过控制Word时,这会使Word正常运行 自动化(也就是说,如果用户以交互方式启动Word, 已为用户打开Word实例).自动化实例 保持隐藏和分离.这样的代码,

Have you tried the Workaround of KB 188546: http://support.microsoft.com/kb/188546/EN-US/? It introduces a way as below. Before you create your Word object, first create a temporary Word object. After you create your object, close the temporary object. This causes Word to act properly when you control it through Automation (that is, if a user interactively starts Word, a new instance of Word is opened for the user). The automation instance remains hidden and separate. Code like this,

        object missing = Type.Missing;
        Word.Application temp = new Word.Application();
        Word.Application wordApp = new Word.Application();
        wordApp.Visible = true;
        temp.Quit(ref missing, ref missing, ref missing);
        temp = null;
        ...

其原因是Office应用程序如何使用ROT(运行对象表). ROT中将仅存在Office应用程序的一个实例-第一个启动.

The reason for this is how Office applications use the ROT (Running Object Table). Only one instance of an Office application will ever be present in the ROT - the first one started.

如果Office以外的其他应用程序创建了该应用程序的实例,那么该实例就是ROT中的那个. Office旨在在ROT中查找正在运行的实例,如果存在,则在用户启动应用程序或打开文档时使用它.这就是为什么问题中描述的情况是可能的.

If some other application than Office creates an instance of that application, then that's the one in the ROT. Office is designed to look for a running instance in the ROT and, if one is present, use that when the user starts the application or opens a document. Which is why the situation described in the question is possible.

解决方法基本上是说:创建两个实例.使用第二个(不会在ROT中),然后销毁第一个.此时,ROT中没有Office应用程序的实例,因此,当用户调用该应用程序而不在ROT中找到任何内容时,Office应用程序会创建一个自己的新实例,而与软件使用的实例无关.

The work-around basically says: create two instances. Use the second, which won't be in the ROT, then destroy the first. At this point, no instance of the Office application is in the ROT, so when the user invokes it, not finding anything in the ROT, the Office application creates a new instance of itself, independent of that used by the software.

这篇关于防止用户附加到MS-Office进程的按开发人员运行的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆