Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my C application, I have a child thread that retrieve a IUnknown interface at beginning of his life :

static struct IUnknown* punk = NULL;

void DispatcherStart(){
    CoInitialize(NULL);
    CheckHRESULT(GetActiveObject(&MY_CLSID,NULL,&punk));
}

everything is fine, it's used to invoke some activeX functions and it work ! however, when my program end, it ask the thread to terminate, so my thread call is ending function's :

void DispatcherStop(){
    if(punk) (punk)->lpVtbl->Release(punk); // BLOCK here
    punk = NULL;
    CoUninitialize();
}

my thead never return because Release on my IUnknow ptr block it. (if I remove the Release then the COUnitialize() block too)

What I am doing wrong ? (the punk iniatilisation can't be done in main thread)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

thread that does CoInitialize will fail if it hasn't message pump. So use CoInitializeEx(NULL,COINIT_MULTITHREADED); if thread doesn't have own one.
But latter case is only for mta com's.
And this doesn't mean you'll just change CoInitialize to CoInitializeEx..
mta com should have own base. And you should provide it. like i've done there https://github.com/alexeyneu/tool3/blob/00bfd2aaf2973626f166ea754b756fd0f2fa0d0b/tool3/MainFrm.cpp#L254


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...