Tuesday, August 12, 2008

Word and IOleContainer.LockContainer

I am developing an OLE link source in C# and I am having trouble determining when to automatically exit when it runs as a hidden out-of-process server. Based on the documentation I would expect to exit when the IOleContainer.LockContainer() count reaches zero. Unfortunately Word 2000 and 2002 (but not 2007) calls LockContainer(false) while it still has IOleObject.Advise() and IDataObject.DAdvise() sinks registered with my application, which it is still trying to use. Therefore I cannot use LockContainer() as the signal to shut down my hidden out-of-process COM server. How should I decide when to shut down?

(I tried waiting until all advise connections were closed, but that means my application closes and re-opens for each linked object in the Word document. I tried adding a delay before shutdown to address this problem, but Word then stopped calling LockContainer(false) as before, leaving me with a positive lock count!)

Friday, August 08, 2008

Enhanced metafile size in Word

My application creates enhanced metafiles to paste into Word. When I create a 100x100mm metafile, it appears in Word as 89x90mm. It so happens that this discrepancy is the difference between my screen's real DPI (~89 horizontal, ~90 vertical) and the notional 96DPI it reports itself as. (My metafiles use my screen as their reference device.)

The metafile contains the original size and resolution of the original output device so Word certainly has the information required. I guess it is trying to ensure that its real size on screen is reflected in print.

I have observed that Visio metafiles paste into Word at the correct size. When I examine them I can see that Visio has intentionally made them larger. (That is, ENHMETAFILEHEADER.rclBounds is exagerated.)

Word metafiles also paste correctly into Word. When I examine them I see that they have been rendered using a printer as the reference device. I am not sure whether this is a real printer, or some ideal DEVMODE that will produce nice output.

Which is the correct strategy? Can anyone help explain what Word is doing/thinking?