Re: CoInitialize/CoUninitialize
* George:
Thanks Alf,
1.
I have to set manually ole32!CoInitialize, or else if I only type
CoInitialize, WinDbg will think the function resides in Kernel32.dll, and
will never stop on the breakpoint, even if it is called.
Do you think it is a bug of WinDbg?
I think you're concentrating on entirely the wrong thing.
2.
"You don't want to call CoInitialize/CoUninitialize locally anywhere, because
that doesn't work with e.g. Internet Explorer machinery." and "Don't rely on
any local calls." -- could you say the comments in some other words please?
Sorry, my English is not good.
Here is bad code, relying on local COM initialization:
void doAnHTMLDialog()
{
if( !SUCCEEDED( CoInitialize() ) )
{
throwX( "Unable to initalize COM" );
}
// Do HTML dialog using COM-based IE machinery, then
CoUninitialize();
// Dontcha know, there might still be some thread using COM! Splat!
}
int main()
{
doAnHTMLDialog();
// Perhaps do other things here, then:
return EXIT_SUCCESS;
}
Here is less bad code, which might even be counted as good if one ignores
exception safety aspects and lack of abstraction and reusability:
void doAnHTMLDialog()
{
HRESULT const initResult = CoInitialize();
if( FAILED( initResult ) )
{
throwX( "Unable to initalize COM" );
}
else if( initResult != S_OK )
{
CoUninitialize();
throwX( "COM was not initialized before calling doAnHTMLDialog." );
}
CoUninitialize(); // "Undo" the checking call of CoInitialize.
// Do HTML dialog using COM-based IE machinery.
}
int main()
{
if( !SUCCEEDED( CoInitialize ) ) { return EXIT_FAILURE; }
doAnHTMLDialog();
// Perhaps do other things here, then:
CoUninitialize();
return EXIT_SUCCESS;
// It's unclear exactly why terminating the process never invokes fatal
// errors and undefined behavior for still running COM things, but, in
// practice it's always worked. It's about the best one /can/ do with
// ugly bug-ridden MS library software that gives no means of waiting
// for finished cleanup, or of forcing cleanup.
}
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?