(1)基于MFC生成行程外組件;
(2)基于對話框生成的MFC應用程式中呼叫進行外組件,在對話框類的OnInitDialog中初始化組件,接著在一個新的執行緒中(也在對話框類中定義的靜態成員函式)呼叫該組件的相關介面函式,沒有問題,并且其資料可以與主執行緒共享;
(3)基于單檔案生成的MFC應用程式中呼叫進行外組件,回應一個選單命令,彈出一個對話框,在對話框類的OnInitDialog中初始化組件,接著在一個新的執行緒中(也在對話框類中定義的靜態成員函式)呼叫該組件的相關介面函式,就出現問題;問題顯示為未初始化組件。
uj5u.com熱心網友回復:
執行緒中需要初始化com環境uj5u.com熱心網友回復:
每個執行緒都要初始化com組件。uj5u.com熱心網友回復:
CoInitializeExInitializes 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
);
Parameters
pvReserved
[in] Reserved; must be NULL.
dwCoInit
[in] Flags specifying the concurrency model and initialization options for the thread. Values for this parameter are taken from the COINIT enumeration. This may contain any combination of values from the COINIT enumeration, except that the COINIT_APARTMENTTHREADED and COINIT_MULTITHREADED flags cannot both be set.
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 different concurrency model for this thread.
Remarks
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 would typically implement some form of concurrency control in a multithreaded object's code using Win32 synchronization primitives such as critical sections, semaphores, or mutexes to protect the object's data.
Objects created in a single-threaded apartment (STA) receive method calls only from their apartment's thread, so calls are serialized and arrive only at message-queue boundaries (PeekMessage, SendMessage).
Apartments must call CoInitializeEx or CoInitialize before calling any other COM library functions except CoGetMalloc function and other memory allocation calls (CoTaskMemAlloc, CoTaskMemFree, CoTaskMemReAlloc, and the IMalloc 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, including calls that return S_FALSE, must be balanced by a corresponding call to CoUninitialize.
Once the concurrency model for an apartment is set, it cannot be changed. A call to CoInitializeEx on an apartment that was previously initialized with a different concurrency model will fail and return RPC_E_CHANGED_MODE.
Because OLE technologies are not thread-safe, the OleInitialize function calls CoInitializeEx with the COINIT_APARTMENTTHREADED flag. As a result, an apartment that is initialized for multithreaded object concurrency cannot use the features enabled by OleInitialize.
Windows CE: The CoInitializeEx function initializes the Component Object Model (COM), including 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. 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 instead.
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.
Windows: Use Windows 95 or later. Available as a redistributable for Windows 95.
Windows CE: Use version 2.0 or later.
Header: Declared in objbase.h.
Import Library: Included as a resource in ole32.dll.
See Also
COINIT, CoInitialize, Processes and Threads
uj5u.com熱心網友回復:
為什么在基于對話框生成的MFC應用程式中,只需要在對話框類的OnInitDialog中初始化組件一次就可以了(ps:我的組件變數是全域的):CoInitializeEx(NULL, COINIT_MULTITHREADED),在新開的執行緒中使用該組件變數時無需初始化。
uj5u.com熱心網友回復:
幫頂!期待大神解答轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/79631.html
