The interop marshaler marshals data between the common language runtime heap and the unmanaged heap. Marshaling occurs whenever the caller and callee cannot operate on the same instance of data. The interop marshaler makes it possible for both the caller and callee to appear to be operating on the same data even though the caller and callee have their own copy of the data.
COM also has a marshaler that marshals data between COM apartments or different COM processes. When calling between managed and unmanaged code within the same COM apartment, the interop marshaler is the only marshaler involved. When calling between managed code and unmanaged code in a different COM apartment or a different process, both the interop marshaler and the COM marshaler are involved.
COM Client and .NET Server
An exported managed server with a type library registered by the Assembly Registration Tool (Regasm.exe) has a ThreadingModel registry entry set to Both. This value indicates that the server can be activated in a single-threaded apartment (STA) or a multithreaded apartment (MTA). The server object is created in the same apartment as its caller, as shown in the following table.
COM client | .NET server | Marshaling requirements |
|---|
STA | Both becomes STA. | Same-apartment marshaling. |
MTA | Both becomes MTA. | Same-apartment marshaling. |
Because the client and server are in the same apartment, the interop marshaling service automatically handles all data marshaling. The following illustration shows the interop marshaling service operating between managed and unmanaged heaps within the same COM-style apartment.
Same-apartment marshaling process
.gif)
If you plan to export a managed server, be aware that the COM client determines the apartment of the server. A managed server called by a COM client initialized in an MTA must ensure thread safety.
.NET Client and COM Server
The default setting for .NET client apartments is MTA; however, the application type of the .NET client can change the default setting. For example, a Visual Basic 2005 client apartment setting is STA. You can use the STAThreadAttribute, the MTAThreadAttribute, the Thread..::.ApartmentState property, or the Page..::.AspCompatMode property to examine and change the apartment setting of a managed client.
The author of the component sets the thread affinity of a COM server. The following table shows the combinations of apartment settings for .NET clients and COM servers. It also shows the resulting marshaling requirements for the combinations.
.NET client | COM server | Marshaling requirements |
|---|
MTA (default) | MTA STA | Interop marshaling. Interop and COM marshaling. |
STA | MTA STA | Interop and COM marshaling. Interop marshaling. |
When a managed client and unmanaged server are in the same apartment, the interop marshaling service handles all data marshaling. However, when client and server are initialized in different apartments, COM marshaling is also required. The following illustration shows the elements of a cross-apartment call.
Cross-apartment call between a .NET client and COM object
.gif)
For cross-apartment marshaling, you can do the following:
Accept the overhead of the cross-apartment marshaling, which is noticeable only when there are many calls across the boundary. You must register the type library of the COM component for calls to successfully cross the apartment boundary.
Alter the main thread by setting the client thread to STA or MTA. For example, if your C# client calls many STA COM components, you can avoid cross-apartment marshaling by setting the main thread to STA.
Note: |
|---|
Once the thread of a C# client is set to STA, calls to MTA COM components will require cross-apartment marshaling. |
For instructions on explicitly selecting an apartment model, see Managed and Unmanaged Threading.