Nelson J

Hi,

I have a scenario using VSTO 2005 SE that requires multiple AppDomains, each of which could manipulate the application's object model using the PIAs. The initial VSTO add-in is obviously passed a local instance of the application object (Microsoft.Office.Interop.Excel.Application). If I pass that instance along to any of the other AppDomains, transparent proxies and remoting gets involved, eventually causing problems when the lease expires (less than 10 minutes).

What is the correct way to obtain a local reference to the Excel application object from a separate AppDomain

I've tried creating a local instance of the Excel application by reaching into the Running Object Table (ROT), but this only seems to grab the first instance of Excel registered in the ROT. This works fine for one instance of Excel but fails when multiple instances are launched. If there were a consistent way to identify the current instance of Excel in the ROT, this might work, but I've not found a foolproof way and I'm not sure it's even recommended either.

Thanks!

Nelson



Re: Visual Studio Tools for Office Obtaining a local application object from a separate AppDomain

Thomas Quinn - MSFT

Hi-

I beleive what is happening is that you are remoting the RCW and this is causing the issue. Leasing shouldn't get involved when talking through interop. I would try unwrapping the object down to a raw pointer, pass that across the boundary and then rewrap it. Something like this in the first appdomain

IntPtr ptr = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(appObj);

Then pass that intptr across the boundary, and get the object back

Excel.Application app = (Excel.Application)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(ptr);

Let me know if this helps.

thanks

tquinn





Re: Visual Studio Tools for Office Obtaining a local application object from a separate AppDomain

Nelson J

Worked like a charm!

The only additional caveat is the need to call System.Runtime.InteropServices.Marshal.Release(ptr) on the host pointer when through, otherwise, the Excel instance doesn't shut down cleanly.

Thanks!

Nelson