locked
How to force all calls from unmanaged host app into managed plugin to MTA RRS feed

  • Question

  • Hi

    I am writing a managed plugin for an 3rd party unmanaged non-COM host application. But because all the interfaces that are used to communicate between host and plugin are COM compatible, I use COM-interop to help me with the interop between unmanaged and managed. The entry point of the plugin is an exported function that is called by the host (DllExportAttribute). But the host app is not COM, it does not call CoInitialize or CoCreateInstance or any of that. So it's not a plain vanilla COM-interop scenario!

    I am making good progress in implementing this plugin - however I ran into a problem that I think may have to do with the type of apartment the CLR has assigned to the calling unmanaged threads (I know for sure that there are more than one threads used in the unmanaged host to call into the plugin). I want all calls from the unmanaged host to run in the MTA (free threading) so I don't have the overhead of sync/pumping of the STA. 

    My question is: how can I make the CLR assign MTA to incoming threads, instead of STA?

    Thanx, Marc


    Marc Jacobi

    Sunday, December 29, 2013 6:07 PM

All replies

  • Hello,

    >> how can I make the CLR assign MTA to incoming threads, instead of STA?

    There is a enum named ApartmentState which has STA and MTA selections,

    If we want to the thread to be STA, we just need to assign it to be STA:

    t.SetApartmentState(ApartmentState.STA);

    If we want to the thread to be STA, we just need to assign it to be MTA:

    t.TrySetApartmentState(ApartmentState.MTA);

    For more information, please refer to link below:

    http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate(v=vs.110).aspx

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 30, 2013 6:53 AM
  • The problem with those methods is that you cannot change the apartment once it is set. Those methods are nice when you are about to start a managed thread. That is not my scenario.

    The first call that is made into my managed plugin - already has the apartment set to STA. Because the unmanaged host does not use COM, I figured the CLR made the decision on what type of apartment the thread that call the managed plugin will become. I wan't to change that decision (to MTA).


    Marc Jacobi

    Tuesday, December 31, 2013 11:49 AM
  • Hi,

    Unfortunately, it is impossible since the thread has already been STA. We can not change a STA thread to be a MTA thread.

    If I misunderstan, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 1, 2014 6:58 AM
  • I know, but can I change the behavior of the CLR - assuming it makes the decision what apartment to create?

    Marc Jacobi

    Wednesday, January 1, 2014 10:58 AM
  • >>but can I change the behavior of the CLR - assuming it makes the decision what apartment to create?

    We can change the CLR behavior, it has been there.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 2, 2014 3:37 AM
  • Any more info on HOW to do that?

    Marc Jacobi

    Thursday, January 2, 2014 7:15 PM
  • Hello,
    As Fred mentioned, we cannot change the change the CLR behavior, it is a released framework. 


    Friday, January 3, 2014 8:02 AM