COM question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

COM question

Michael Latta
In playing with the COM support in X6 I encountered something that seems odd
and do not know if this is just the way COM works or unique to the Dolphin
framework.

If I do "MSWord_Application new" I get an instance of the Application
interface for the Word application (MSWord prefix).
If I immediately try to get the activeDocument I get an exception because
there is no active document.
Is this to be expected?  I need to learn how to catch exceptions in Dolphin
in that case.
If I create a document with "MSWord_Application new documents add" it does
create a document in Word.
If I then execute "MSWord_Application new documents count" I get 0 because
it has created a separate instance of the entire application?  I would have
thought that an instance of the application interface would refer to the
application and only create a new one if there was no active instance of the
application.  I can live with this for the most part if I just need to keep
the reference around.  My concern is that if the user closes the application
or the image is saved and reloaded with that reference still active how best
to handle this.  Should I register for notification that the image is
closing and release the reference?  Will all references to the COM object
throw exceptions if the user has closed that instance of the application?

Michael


Reply | Threaded
Open this post in threaded view
|

Re: COM question

Blair McGlashan-4
"Michael Latta" <[hidden email]> wrote in message
news:[hidden email]...

> In playing with the COM support in X6 I encountered something that seems
> odd and do not know if this is just the way COM works or unique to the
> Dolphin framework.
>
> If I do "MSWord_Application new" I get an instance of the Application
> interface for the Word application (MSWord prefix).
> If I immediately try to get the activeDocument I get an exception because
> there is no active document.
> Is this to be expected?  I need to learn how to catch exceptions in
> Dolphin in that case.
> If I create a document with "MSWord_Application new documents add" it does
> create a document in Word.
> If I then execute "MSWord_Application new documents count" I get 0 because
> it has created a separate instance of the entire application?  I would
> have thought that an instance of the application interface would refer to
> the application and only create a new one if there was no active instance
> of the application.  I can live with this for the most part if I just need
> to keep the reference around.

Michael, the underlying behaviour you are seeing here is not specific to
Dolphin - Word would behave that way whatever the host. For that reason you
should find that there is plenty of documentation in the form of articles on
the web, books, and MSDN articles, etc, that will help you with best
practice guidance on automating Word.

I would think you would only want to hold a single Word application instance
at any time.

>...My concern is that if the user closes the application or the image is
>saved and reloaded with that reference still active how best to handle
>this.  Should I register for notification that the image is closing and
>release the reference?  Will all references to the COM object throw
>exceptions if the user has closed that instance of the application?

In relation to these questions, you should find that:
1) You should not need to release references on shutdown. On shutdown
Dolphin will explicitly clean up (release) all existing COM interface
objects. You may still find that Word hangs around in memory - even the
simple sequence of "(IDispatch createObject: 'Word.Application') free"
leaves Word in memory, at least on my test machine.
2) On restart after an image save, all COM interface references will be
"dead" (null). If you attempt to use them, your program won't crash, but you
will obviously get errors occurring.
3) If the user shuts down word (in theory this should only be possible by
killing it from the task manager, or other tool, since if an automation
application has references from other applications it should close the UI
but not shut down), then attempts to use the interface references will again
not crash, but cause errors, specifically you will probably get "The RPC
Server is Unavailable".

Regards

Blair