Word application hijacking

  • Thread starter Jeroen ter Hofstede
  • Start date
J

Jeroen ter Hofstede

[Posted to microsoft.public.word.programming and
microsoft.public.word.oleinterop. If there is a better place to post
this, please let me know.]


Our application uses and controls Word in order to have the user edit
documents of the application. To that end, it creates a Word object
using OLE, which starts up a WINWORD.EXE process.
It now appears that some other applications, most notably the Word
browser plug-in, will use that same WINWORD.EXE process to open their
documents. Furthermore, if the document opened in that application (the
browser, say) is active, the calls our application makes to Word are no
more handled correctly.

We've tried several workarounds to no avail (trying to activate Word
from within our app fails, starting a "decoy" word first may foil the
browser, but is also used when the user double-clicks a document and
can thus be closed, etc). What we actually would like to achieve is
that the WINWORD.EXE process that we start by instantiating a Word
application object via OLE cannot be hijacked by any other program.

Does anybody here know whether such a thing is possible?

Thanks in advance,
Jeroen
 
T

Tony Jollans

Not really much help but nobody else has chimed in so ...

Somebody will probably correct me but if you give control to the user
(which, of course you must if they are to edit the document) I don't think
there is any easy way you can stop other applications (or the user) from not
just using the instance but also, possibly, closing it.

You can keep a reference to your document so that you don't need to worry
which document is active - which may solve your immediate problem - but for
a more general solution you will need to have event code running in the Word
instance which reacts to documents being opened or closed; you could
possibly do this with a global template which you install when you first
start the Word instance (and uninstall when you end it). Although this might
solve _your_ problem, it could, of course, cause problems for other
applications which try to use the Word instance.

Normally the instance returned via GetObject is indeterminable in advance if
there are multiple instances of an application running so you don't gain
(much) by creating dummy instances. There may be another way with Windows
API to do something a bit smarter but I'm afraid I have no idea what it
might be.
 
J

Jeroen ter Hofstede

Tony Jollans schreef:
Not really much help but nobody else has chimed in so ...

Somebody will probably correct me but if you give control to the user
(which, of course you must if they are to edit the document) I don't think
there is any easy way you can stop other applications (or the user) from not
just using the instance but also, possibly, closing it.

There is actually no problem in giving control to the user. I can track
what is happening there where the user is doing things by way of events
from our global template.
One of our work-arounds (starting a "decoy" Word instance first, which
the browser plug-in would hijack instead of our "real" Word instance)
fails because the user can open this by double-clicking a Word
document, and then close. So, that is not the core problem, it is only
a failure of the workaround.
You can keep a reference to your document so that you don't need to worry
which document is active - which may solve your immediate problem - but for
a more general solution you will need to have event code running in the Word
instance which reacts to documents being opened or closed; you could
possibly do this with a global template which you install when you first
start the Word instance (and uninstall when you end it). Although this might
solve _your_ problem, it could, of course, cause problems for other
applications which try to use the Word instance.

We are using this approach, and it works fine when the user works
normally with it. Unfortunately, when another application (again, the
main culprit is opening a Word document in the browser) tries to use
Word, it may use the instance that we started. And it appears to happen
then that Word is thoroughly confused on what is happening and who is
in control. Calling functions in the template fails. Trying to activate
our own document before that fails. Currently our only working
workaround is informing the user to either close the browser, or bring
our applications document to the foreground manually... After which
things resume to work like they should.

Normally the instance returned via GetObject is indeterminable in advance if
there are multiple instances of an application running so you don't gain
(much) by creating dummy instances.

It seems to be the first one started that is selected by GetObject, I
even believe that this is documented.

There may be another way with Windows
API to do something a bit smarter

Yes, I was hoping for something like that.

but I'm afraid I have no idea what it
might be.

Pity. Still, thanks a lot for your interest and effort!

Regards,
Jeroen
 
H

Howard Kaikow

I vaguely recall a discussion a number of years ago that indicated that the
safest way to proceed would be to first create a NEW instance of Word as a
decoy, the create another NEW instance that you actually use.
 
J

Jeroen ter Hofstede

Howard Kaikow schreef:
I vaguely recall a discussion a number of years ago that indicated that the
safest way to proceed would be to first create a NEW instance of Word as a
decoy, the create another NEW instance that you actually use.

Yes, thats exactly what we've tried. It is not waterproof, however. The
decoy Word instance is not only used by other applications like the
browser plug-in, but it is also used when the user opens a Word
document manually (e.g. by double-clicking a Word document). When the
user closes that one, the decoy Word instance is gone, too.

Thanks anyway,
Jeroen
 
H

Howard Kaikow

Jeroen ter Hofstede said:
Howard Kaikow schreef:


Yes, thats exactly what we've tried. It is not waterproof, however. The
decoy Word instance is not only used by other applications like the
browser plug-in, but it is also used when the user opens a Word
document manually (e.g. by double-clicking a Word document). When the
user closes that one, the decoy Word instance is gone, too.

Yes, but you are not using the decoy instance.

If necessary, start more than one decoy instance.
 
T

Tony Jollans

No matter how many decoy instances you start you can not guarantee anything.

Think what happens when you are a user. If you want to open a Word Document
you can press Alt+Tab one or more times until you find an instance of Word
and use it to open the document. If you have multiple instances of Word you
don't care which instance you use - the first one you find is good enough.
The order in which you scroll through instances of applications depends on
the order in which you have used them previously and you cannot set any
particular instance as not for such use (this is not technically true as it
is possible programmatically to start hidden applications which don't appear
in the Alt+Tab list - but you cannot do this from the UI).

When a program (Browser, Explorer, etc.) wants to open a Word document it
scrolls through all the available applications until it finds a Word one and
uses it. Just like the user, it doesn't care which instance it uses - the
first one it finds is good enough. Just as with the user, the order of
application instances checked depends on what has gone before. Programs,
however, may check hidden application instances as well as visible ones
(although if they want a visible one they should only check visible ones)
and, AFAIK, there is no way to set an application 'very hidden' and even if
there were it wouldn't suit because you need your instance of Word to be
visible for the user.

In theory, if you create multiple instances, you might think you are
reducing the odds but, in practice, I don't think that's the case. You also
have the problem of extra resource usage, and of tidying up afterwards. I
don't think there is anything you can do to stop Word instances being
hijacked as you describe. It might be possible, however, to force your
browser to create a new instance rather than trying to find an existing one,
I don't know - if not, I fear you are stuck with the issue. It should not,
however, interfere with your program and if you have object pointers set
they should remain valid. Can you give any specific examples of something
which fails?
 
H

Howard Kaikow

Where possible, code should be using hidden instances.
And, tho i've never tried it, there might be a way to prevent a particular
instance from closing by using Application events on that instance.
 
J

Jeroen ter Hofstede


No, I hadn't seen that one yet. Judging from the abstract it may be
of help. I'll look into it. Thanks a lot!

Not sure what your app is/does but this control will host any MS Office app
exclusively.
Hope it helps
Richard

Jeroen ter Hofstede said:
[Posted to microsoft.public.word.programming and
microsoft.public.word.oleinterop. If there is a better place to post
this, please let me know.]


Our application uses and controls Word in order to have the user edit
documents of the application. To that end, it creates a Word object
using OLE, which starts up a WINWORD.EXE process.
It now appears that some other applications, most notably the Word
browser plug-in, will use that same WINWORD.EXE process to open their
documents. Furthermore, if the document opened in that application (the
browser, say) is active, the calls our application makes to Word are no
more handled correctly.

We've tried several workarounds to no avail (trying to activate Word
from within our app fails, starting a "decoy" word first may foil the
browser, but is also used when the user double-clicks a document and
can thus be closed, etc). What we actually would like to achieve is
that the WINWORD.EXE process that we start by instantiating a Word
application object via OLE cannot be hijacked by any other program.

Does anybody here know whether such a thing is possible?

Thanks in advance,
Jeroen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top