The information in this article applies to:
SUMMARYMarshal.exe is a sample that shows the different ways of marshalling an interface across apartments. MORE INFORMATIONThe following files are available for download from the Microsoft
Download Center. Click the file names below to download the files: http://www.microsoft.com/downloads/search.aspand then click How to use the Microsoft Download Center. A Single-Threaded Apartment (STA) is a thread that was initialized with CoInitialize() or CoInitializeEx (NULL, COINIT_APARTMENTTHREADED). Also, any other threads in the same process that use COM must also call CoInitialize() or CoInitializeEx() to initialize COM for its thread. If you create a COM object in one STA thread, you cannot pass an interface pointer to another STA thread and call out on that pointer. Since calls to STA objects are supposed to be serialized, COM enforces this by only allowing one thread to call into the STA object (the thread where it was created). If the interface pointer you pass to the second STA thread is a pointer to a proxy, you will get an error HRESULT of 0x8001010E or RPC_E_WRONG_THREAD (the application called an interface that was marshalled for a different thread). If the interface pointer is a direct pointer to the object, COM will not enforce serialization, you will not get the above error, and the interface method call will be made. However, this is still illegal behavior on the part of the client. You can still call methods on the STA object from a different STA thread as long as you do it through a proxy. A proxy is a copy of the interface that you get via marshaling/unmarshaling. When you make a call through the proxy, COM makes a thread switch and the call ends up executing in the context of the thread where the STA object was created. There are three ways to marshal/unmarshal an interface to another STA thread:
REFERENCESQ150777 INFO: Descriptions and Workings of OLE Threading Models Additional query words: kbDSupport kbsample
Keywords : kbfile kbsample kbCOMt kbThread kbATL300 kbGrpMFCATL kbArchitecture |
Last Reviewed: December 13, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |