问题
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)
So,
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) Or (Another option) - how to detect that user has just attached to run-by-me office process ?
Will appreciate any help.
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.
回答1:
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:
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; ...
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.
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.
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.
回答2:
Get the Application
Object. Check the Application.Visible
property. If the application is visible than you should not exit the application.
来源:https://stackoverflow.com/questions/54324083/prevent-user-from-attaching-to-run-by-developer-instance-of-ms-office-process