Home > Software engineering >  In MFC single document under multiple threads COM components outside the calling process
In MFC single document under multiple threads COM components outside the calling process

Time:09-26

(1) based on the MFC generated process components;
(2) based on the generated MFC dialog application calls for external components, the dialog class initializes the component in OnInitDialog, then in a new thread (also in the dialog class defined in a static member function) to invoke the component related interface function, there is no problem, and its data can be Shared with the main thread;
(3) based on single document generated MFC applications call for external components, in response to a menu command, pop up a dialog, the dialog class initializes the component in OnInitDialog, then in a new thread (also in the dialog class defined in a static member function) to invoke the component of relevant interface functions, problems; Problems displayed as uninitialized components,

CodePudding user response:

Threads need to initialize the com environment

CodePudding user response:

Each thread to initialize the com component,

CodePudding user response:

CoInitializeEx
Initializes the COM library for use by the current apartment and specifies the apartment 's concurrency model.

HRESULT CoInitializeEx (
Void * pvReserved,//Reserved
DWORD dwCoInit//COINIT value
);

The Parameters
PvReserved
[in] Reserved; Must be NULL.
DwCoInit
[in] Flags specifying the concurrency model and initialization options for the thread. The Values for this parameter are seems the from the COINIT enumeration. This may contain any combination of Values from the COINIT enumeration, except that the COINIT_APARTMENTTHREADED and COINIT_MULTITHREADED Flags always to both be set.
The Return Values
This function supports the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following:

S_OK
The COM library was initialized successfully.
S_FALSE
The COM library is already initialized.
RPC_E_CHANGED_MODE
A previous call to CoInitializeEx specified A company's concurrency model for this thread.
Few
If neither concurrency model is specified by the dwCoInit parameter, the default is COINIT_APARTMENTTHREADED.

Objects created on a COM thread in a multithread apartment (MTA) must be able to receive method calls from other threads at any time. You order typically implement some form of concurrency control in a multithreaded object 's code using Win32 synchronization primitives to as critical sections, semaphores, or mutexes to protect the object' s data.

Objects created in a single - threaded apartment (STA) the receive method calls only from their apartment 's thread, so calls are serialized and arrive only at the message - the queue boundaries (PeekMessage, SendMessage).

Apartments must call CoInitializeEx or CoInitialize before calling any other COM library functions provides the except CoGetMalloc function and other memory allocation calls (CoTaskMemAlloc CoTaskMemFree, CoTaskMemReAlloc, and the IMalloc the methods on the task allocator supplied by CoGetMalloc).

CoInitializeEx provides the same functionality as CoInitialize and also provides a parameter to explicitly specify the thread 's concurrency model. The current implementation of CoInitialize calls CoInitializeEx and specifies the concurrency model as single - thread apartment. Applications developed today should call CoInitializeEx rather than CoInitialize.

Typically, CoInitializeEx is called only once by each apartment in the process that USES the COM library. For a multithread apartment, one call is sufficient For all threads in the apartment.

Multiple calls to CoInitializeEx by the same thread are allowed as long as they pass the same concurrency flag, but subsequent valid calls return S_FALSE. To close the library gracefully, each successful call to CoInitialize or CoInitializeEx, o calls that return S_FALSE, must be balanced by a corresponding call to CoUninitialize.

Once the concurrency model for an apartment is set, it always be changed. A call to CoInitializeEx on an apartment that was previously initialized with A company's concurrency model will fail and return RPC_E_CHANGED_MODE.

Because OLE technologies are not thread - safe, the OleInitialize function calls CoInitializeEx have the COINIT_APARTMENTTHREADED flag. As a result, the an apartment that is initialized for multithreaded object concurrency always use the features enabled by OleInitialize.

Windows CE: The CoInitializeEx function initializes The Component Object Model (COM), o The OLE library, for use by The current thread. The dwCoInit parameter specifies The type of concurrency control required by objects created by this thread. The Windows CE supports only The multi - threaded concurrency control. Thus, COINIT_MULTITHREADED is The only option permitted for The dwCoInit parameter.

Compound document applications must call CoInitializeEx before calling any other function in the OLE library.

Windows CE does not support the CoInitialize (NULL) or OleInitialize function. Use CoInitializeEx home.

Passing into this function any invalid and, under some circumstances, NULL Pointers will result in unexpected termination of the application. For more information about handling exceptions, see Programming Considerations.

QuickInfo
Windows NT: Use version 4.0 or later.
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related