2

I am developing a Windows app with WebBrowser control (IWebBrowser2) embedded.

Things look good if I initialize COM apartment as single threaded:

CoInitialize(NULL);

However, if I change it to be multithreaded:

CoInitializeEx(NULL, COINIT_MULTITHREADED);

then it starts to fail all over the places with return value of:

An outgoing call cannot be made since the application is dispatching an input-synchronous call.

from calls to IWebBrowser2 methods.

Can someone please tell me how so solve the problem? I have to use multithreaded apartment as a requirement. Please help!

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
TopQ
  • 175
  • 3
  • 13
  • Why exactly do you need to use MTA? – sharptooth Sep 15 '10 at 05:04
  • Actually we are not so sure...the app actually uses both iTunes COM and IWebBrowse2 COM and when we use STA, the execution seems out of order unless we use MTA. However, MTA breaks IWebBrowser2 as it seems. – TopQ Sep 15 '10 at 08:26
  • You know you can do multi-threading with STA just fine? – wqw Sep 15 '10 at 17:26
  • Have you read this article http://www.codeguru.com/cpp/com-tech/activex/apts/article.php/c5529 ? Maybe you're just solving the wrong problem. – sharptooth Sep 16 '10 at 05:43

2 Answers2

3

You can't initialize a visual activex control in a multithreaded apartment.

In theory you can create a separate thread, initialize it as STA, create your IWebBrowser2 interface there and marshal it across to the MTA thread using CoMarshalInterThreadInterfaceInStream / CoGetInterfaceAndReleaseStream. However it's disgustingly fiddly and I wouldn't recommend trying it if you want to escape with your sanity intact.

I spent several days looking at this exact problem and gave up in disgust (and used Chromium Embedded Framework instead as it happens).

Work out whether you really need to use MTA. Chances are you don't, you're just doing something wrong with iTunes.

JamesT
  • 2,988
  • 23
  • 29
  • I, too, have given up trying to get it to work in MTA. Thanks for mentioning Chromium Embedded Framework as I had no clue we actually have an alternative How is it compared to IWebBrowser2 in you opinions? Thanks. – TopQ Oct 19 '10 at 17:51
  • So far, excellent. It depends what you're trying to use it for though. IWebBrowser2 exposes a lot of functionality of playing around behind the scenes that Chromium doesn't. Chromium also adds 23MB of dlls to your application footprint, whereas IWebBrowser2 comes with Windows. The only thing I've found lacking is the inability to manually set the zoom level. – JamesT Oct 20 '10 at 12:51
-1

I have spent one whole week trying these STA and MTA and gave up.Instead there is a simpler approach.Use the system("call iexplore.exe http://www.example.com"); method within a button_click method.This works beatifully.Only problem is this creates the web browser on a separate tag.I think these complications or intentionally(or idiotically) created by microsoft researchers so that we always come in circles and remember microsoft at every approach of our codings. regards kvinvisibleguy

Bishan
  • 15,211
  • 52
  • 164
  • 258