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?
You can call
CoInit
andCoUninit
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 theCoUninit
call, and don't callCoUninit
ifCoInit
fails.