问题
I've got a set of tasks that I slaved to the NT threadpool using QueueUserWorkItem
. I need to make some calls to COM from these separate threads to access data inside WMI. I'm unsure, however, how the correct calls to CoInitializeEx
need to be made.
Basically, the CoInitializeEx
docs say that the call should be made once per thread. But I don't own these threads—NT does. I don't know when they get created or destroyed, or anything of that nature. Do I basically call ::CoInitializeEx()
(with COINIT_MULTITHREADED
) at the beginning of the thread routine, and then ::CoUninitialize()
at the end of my thread routine?
回答1:
You can call CoInit
and CoUninit
multiple nested times, they just have to be balanced. Your proposed solution of doing it at the beginning and end of your thread proc is reasonable. Just make sure you don't have any early exits that skip the CoUninit
call, and don't call CoUninit
if CoInit
fails.
来源:https://stackoverflow.com/questions/7811118/how-does-one-call-into-com-from-worker-thread-created-with-nts-queueuserworkite