How does one call into COM from worker thread created with NT's QueueUserWorkItem?

末鹿安然 提交于 2019-12-11 12:25:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!