COM object methods are not executed on the thread that CoInitialize-d and created the object

后端 未结 5 1436
春和景丽
春和景丽 2021-01-01 05:26

I am developing an UI application that creates a COM object along the way. The problem is, I want to \"move\" this COM object entirely on a different thread.

What I

5条回答
  •  被撕碎了的回忆
    2021-01-01 05:34

    Looks like your problem is that your COM component threading model is not specified in registry key InprocServer32. This means that object is considered as STA (single-threaded apartment) but will be loaded to main (or host) STA, not the STA that created it. This is the first thread that called CoInitialize. To be created in same STA that called CoCreateInstance you must create HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{Your CLSID}\InprocServer32@ThreadingModel registry value and set it to Apartment.

    Quote from MSDN (InprocServer32 registry key documentation):

    If ThreadingModel is not present or is not set to a value, the server is loaded into the first apartment that was initialized in the process. This apartment is sometimes referred to as the main single-threaded apartment (STA). If the first STA in a process is initialized by COM, rather than by an explicit call to CoInitialize or CoInitializeEx, it is called the host STA. For example, COM creates a host STA if an in-process server to be loaded requires an STA but there is currently no STA in the process.

提交回复
热议问题