Multitasking in a Word COM Addin

  • Thread starter Scott McPhillips [MVP]
  • Start date
S

Scott McPhillips [MVP]

I am developing a COM Addin for Word (in C++ with ATL). The basics are
working: Word starts me and I can do things on events from Word OK. But
I also need to access Word objects at times when there is no event
coming in.

A periodic timer call would be great, but there does not seem to be one
in the Word interface.

So I am trying a secondary thread within my addin. If it calls
CoCreateInstance that creates a new instance of Word, not the instance I
need access to. So in desparation I tried copying the
Word::_Application CComPtr from main thread to the secondary thread. It
seems to work with some simple tests but I don't know if it is safe.

- Is there some way, like a timer, I can get Word to call me periodically?

- Is there some way to get a pointer to the currently running
Word::_Application object from a secondary (but in-process) thread?

- Or a way to safely use the pointer I copied from the main thread?
 
I

Igor Tandetnik

Scott McPhillips said:
I am developing a COM Addin for Word (in C++ with ATL). The basics
are working: Word starts me and I can do things on events from Word
OK. But I also need to access Word objects at times when there is no
event coming in.

A periodic timer call would be great, but there does not seem to be
one in the Word interface.

But there's one in Windows API: SetTimer.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
 
S

Scott McPhillips [MVP]

Igor said:
But there's one in Windows API: SetTimer.

That would be great, but I don't see a way to use that. My code is a
DLL and only gets main thread execution time when Word calls it. I
don't see a way for me to receive messages from Word's message loop, or
a way to have my own message loop in Word's main thread.
 
I

Igor Tandetnik

Scott McPhillips said:
That would be great, but I don't see a way to use that. My code is a
DLL and only gets main thread execution time when Word calls it. I
don't see a way for me to receive messages from Word's message loop

Word's message pump will deliver messages to any and all windows created
on the main UI thread, not just ones it created itself.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
 
A

Alexander Nickolov

Using a hidden window within an STA object is a standard technique
for bringing control into the STA. I've used it for my asynchronous
event triggering from a worker thread framework (item 11 in the
VC COM FAQ) for example. It can equally well be used with SetTimer
as Igor suggested.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: (e-mail address removed)
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
 
S

Scott McPhillips [MVP]

Igor said:
Word's message pump will deliver messages to any and all windows created
on the main UI thread, not just ones it created itself.

Wonderful! It's working.

While knee-deep in COM, ATL, MFC, Word and multithreading I neglected to
analyze the situation in terms of good-old Win32 :(

Thanks Igor.
 

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