Tech Off Post

Single Post Permalink

View Thread: WPF + WindowsFormsHost + .Net Forms + ( COM interfaces + Forms Invoke ) = E_NOINTERFACE Exception
  • User profile image

    pompomJuice said:
    Dexter said:



    The COM object was indeed created in that thread, which was actually a worker thread. That explains why I could access that interface before the invoke, but not after. I say this because MapObject.Instance was created outside of that worker thread, which would indicate that its contained COM interfaces were also created outside of the worker thread. But not in this case, that interface was created in the worker thread as you predicted!


    What is interesting is that I just went to test what happens on a Windows CE device, as the code runs there. I set up the same test and debugging the app while running in CE seems to have no problem with accessing COM objects between threads. So there I think we can learn something, that sometimes things are not so portable as you think when working with COM. The original implementer never got hit with the problem of marshalling between threads, so he clearly just fired away. 


    I might be able to fix this instance, but I get a sneaky suspicion the entire program is going to be riddled with this issue. I am not looking forward to this.

     You mention STA Com objects, can I instantiate them in a way that they are not single thread apartment objects? Which would mean I assume I can access them from multiple "apartments"? I am guessing here, I lack theory.



    PS- I will probably check out marshalling/proxy you mentioned, but I have never done that before so. We never get a break in this world of computers ;'(


    On Windows CE it probably works because the "CE" COM does not implement synchronization, all COM objects are free threaded (the client code has the reponsability to synchronize access to a COM object).

    Now I'm not sure why that object doesn't have a proxy registered. One possibility is that it is actually a free threaded COM object and it doesn't have a proxy because that's not needed (I assume it's only used as an inproc server). However, if the object is created on the main UI thread it will be subject to apartment threading rules.

    You'll have to figure out what threading model the COM object supports (see its registration key in HKCR\CLSID\{guid}\InProcServer32). If it's indeed free threaded you could try to create the object on a different thread than the main UI thread.