1

I am trying to use my own C++ class to interact with a COM library that is STA.

The problem we are trying to solve is that a third party application has the STA attribute set to it. The application then creates the C++ service we created and other services (in our case a WCF service) are modifying elements in the service created by the third party STA attribute application. In essence we have Thread1 coming from STA application who creates the service, then Thread2...N coming from WCF which is not in a STA thread state manipulating data held in the service. The STA application doesn't see the changed data and acts as though nothing happened.

So in order to fix it, I am thinking that I have to ensure that all operations contained in my service need to be guaranteed to run on the same thread. The question I have is how to achieve that.

I will show an example of our current code.

Current Service (every method needs to be called on the same thread that it was initially created on)

MyService::MyService()
{
    // creates some objects and maybe some other initializing
    // Whatever thread (the STA application in this case) this was
    // created on needs to be used on all methods, how?
}

ObjectA MyService::GetObject()
{
  return anObject;
}

ObjectB MyService::CreateObjectB()
{
   // Do some tasks that eventually create object
   object = new ObjectB();
   return object;
}

void MyService::SomeVoidFunction()
{
   // Do some work
}

I am not sure if I can take advantage of the TPL and do this, or do I need to use ThreadPools or something like that?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ShaffDaddy
  • 63
  • 1
  • 9
  • STA is a property of a thread, not of a library. You probably have a library that only has single-threaded coclasses, that's pretty common. COM will automatically create a new thread to give the COM object a thread-safe home when you create such an object from a worker thread. Nice. It is not clear why that would be a problem. If it is for some reason (never leave us guessing) then the best way to take that bull by the horn is to just create your own STA thread and take care of the marshaling yourself. Sample code [is here](http://stackoverflow.com/a/21684059/17034). – Hans Passant Apr 10 '15 at 16:21
  • You need a dedicated STA apartment for your COM STA object(s) and a custom TPL task scheduler to call out to that apartment. I've answered a highly related [question](http://stackoverflow.com/q/21211998/1768303). – noseratio Apr 10 '15 at 17:48
  • @HansPassan and Noseratio, my problem statement has changed and apparently I was making the problem much harder than it really was, I am not sure if I should repost a question but I will update my original statement as it still relates to an extremely similar problem. – ShaffDaddy Apr 13 '15 at 14:38
  • You need to handle marshaling yourself, e.g. `CoMarshalInterface`/`CoUnmarshalInterface` or with the [global interface table](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682353(v=vs.85).aspx). – acelent Apr 20 '15 at 17:35

0 Answers0