|
Hi,
unfortunately I had forgotten all the things I had once learned (and barely understood) about COM, STAs, Windows message loops and console apps in my former life as developer, so it took me some unnerving hours until I could finally find the simple solution how to make a simple thing work. So I want to share my stupidity - and what I learned about COM (again, the hard way) - here with you:
I was trying to use IltmmConvert->StartConv() in a simple file conversion scenario without windows gui (windows service), i.e. without windows handle, and therefore without the ability to receive notification messages (conv->SetNotifyWindow(0,0)). This should be possible by calling StartConv() and then by repeatedly polling get_State() until the state reaches ltmmConvert_State_Stopped.
However, when I tried it from my C++ console app, the state did never change - it always remained ltmmConvert_State_Running, but the conversion was still done successfully. Well, the solution and explanation is the following basic COM fact you should always keep in mind: any COM object in a Single Threaded Appartment (STA) can only work correctly if there is a message loop - otherwise it can lock up or may not work at all. So I had to include a very simple message loop into my polling loop, and voila!
Here is a short code snippet from my basic example:
hr = mediaconv->StartConvert();
LONG perccomplete = 0; LONG convstate = ltmmConvert_State_Running; while (SUCCEEDED(hr) && (convstate == ltmmConvert_State_Running)) { MSG msg; while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage( &msg ); DispatchMessage( &msg ); }
Sleep( 1000 ); // sleep for 1 secs hr = mediaconv->get_State( &convstate ); if (SUCCEEDED(hr)) hr = mediaconv->get_PercentComplete( &perccomplete ); ... now do something with percomplete if you like .... }
mediaconv->StopConvert(); // stop anyway...
Cheers!
Wolfgang
|