Run Enabling Area (RCW) - process or application domain?

What is the scope of a Runtime Callable Wrapper (RCW) when referencing unmanaged COM objects? According to the docs:

Runtime creates exactly one RCW for each COM object, regardless of the number of links that exist on that object.

If I had to "guess" - this explanation should mean "one for the process", but is it really? Any additional documentation would be very welcome.

My application runs in its own application domain (this is an Outlook application) and I would like to know what will happen if I use Marshal.ReleaseComObject (x) in a loop until it reaches 0 (as recommended). Will links from other add-ons work (works in a different application domain in the same Outlook process)?

EDIT: Perfect - now the confusion is even greater. Based on two answers (from Lette and Ilya) we have 2 different answers. The official MSDN document talks about each process (for version 2.0+), but this sentence is missing for ver. 1.1 documents .

At the same time, an article in Mason Bendiken's article talks about this.

Since his article is outdated (April 2007), I will send him an email asking him to clarify, but if someone else adds something, do it.

thanks

+6
interop marshalling com appdomain
source share
3 answers

In managed mode, we have a domain for each cache mapping application of canonical IUnknowns back to RCWS. When IUnknown enters the system (by calling the marshal, through activation, like returning a parameter from a method call, etc.), we check the cache to see if RCW already exists for the COM object. If a mapping exists, a link to an existing RCW is returned. Otherwise, a creates a new RCW and cache mapping.

from Mason's blog

+3
source share

The Mason Bendixen blog article quoted by Ilya is correct: RCW is tied to the AppDomain, not the process. I can only guess that the Runtime Callable Wrapper (MSDN 2.0) article said "by accident." This article is not necessarily incorrect in the general sense, because it is most typical to execute using only one AppDomain, but this proposal is not technically accurate.

Regarding your specific question:

“I would like to know what happens if I use Marshal.ReleaseComObject (x) in until the counter reaches 0 (as recommended). Will it free the links from other additions (works in a different application domain in the same process Outlook)?

The answer to this depends on how you configure the add-in. In general, if you do not take precautions, then the answer is yes, this will affect the links in other add-ins working from the same AppDomain. But since you claim that you are working from a separate AppDomain, then no, it is not.

There is a COM Shim Wizard Version 2.3.1 that you can use to isolate your add-in. The documentation for the COM-Shim wizard can be found here: Insulating Microsoft Office Extensions with the COM-Shim Wizard Version 2.3.1 .

The COM Shim Wizard uses reflection to create a custom loader with a COM interface that loads your add-in into a separate AppDomain. This creates security in two ways:

(1) Using a separate, custom COM entry point, your add-in is correctly identified separately by Microsoft Office from all other add-ins. Otherwise, by default, all add-ons use the same mscoree.dll bootloader by default. The problem with sharing the same bootloader is that if any add-in fails, then mscoree.dll will be identified by Microsoft Office as the source of the problem and will not automatically load it the next time. You can enable it again manually, but your add-in will not automatically load the next time due to a problem in someone else's add-in!

(2) When loading its assembly into a separate AppDomain, the shells called by the call (RCW) are isolated from other add-ins loaded into the same process. In this case, if you call Marshal.ReleaseComObject (object) or Marshal.FinalReleaseComObject (object), then you will not affect other add-ons. More importantly, if any of these additional add-ons make such calls, your add-in will be protected from damage. :-)

The disadvantage of using the COM-Shim wizard is that when working from a separate AppDomain, there is additional overhead. I do not think this should be noticeable for the Microsoft Outlook add-in. However, this can be a factor for some intensive procedures that have many object model calls, for example, sometimes this can be the case for the Microsoft Excel add-in.

You stated that you are already using your add-in from a separate AppDomain. If so, then you are already isolated from the Marshal.ReleaseComObject (object) and Marshal.FinalReleaseComObject (object) objects in relation to other AppDomains applications. (I'm curious how you do this, by the way ... Do you explicitly create your own AppDomain? The default extension template in Visual Studio does not start in a separate AppDomain and loads using mscoree.dll.)

If you create your own AppDomain, your code is isolated, but its identifier may not be separate from other add-ins, however, since your add-in will still use the mscoree.dll bootloader by default if you do not use some other means to solve this problem.

Hope this helps ...

+3
source share

In accordance with the same documents:

Runtime supports one RCW for each process for each object.

I think we can safely assume that object = instance, so if addins / AppDomains does not contain a reference to the same instance, calling ReleaseComObject will not issue references to instances created elsewhere.

Edit: the wording of the documents may be incorrect, as indicated elsewhere . If so, since your add-in works in a separate AppDomain, you're in luck. Even if different add-ins refer to the same instance (for example, a message object in Outlook), the ReleaseComObject called in your application will not cause RCW in other AppDomains to lose the link to this instance.

+1
source share

All Articles