0

This is a fairly obscure problem but hoping someone will have seen it before or have an idea of why it would occurs.

I've created a C#, WPF application which uses interop COM dlls to query data from another application. I'm using async & await (with Task.Run) to query data without blocking the UI thread. Up till recently this worked fine on any machine I deployed my app to. Now however I'm receiving an error message:

Error creating an instance of the COM component with CLSID {......} from the IClassFactory failed due to the following error: 800a01e Exception from HRESULT 0x800A01AE

A couple of points to note:

  • This error only occurs on other machines (i.e It Works On My Machine)
  • The error does not occur if I remove async and await (i.e. block the UI thread)
  • I have older versions of my app which work fine on other machines. It's only newly compiled versions that have stopped working.

I'm not sure exactly when this error started to occur but I did recently install VS2017.

OZ10
  • 29
  • 7
  • 2
    Is the COM object you're using a free-threaded object ([`ThreadingModel=Free`](https://support.microsoft.com/en-us/help/150777/info-descriptions-and-workings-of-ole-threading-models))? – noseratio May 10 '17 at 10:32
  • 1
    It is an application-specific error code, it could mean anything. It is raised too early to get a better description, that doesn't help. It does match an error code that is common in Microsoft scripting interfaces, it then means "Class does not support Automation". We can't see your code so we don't know if you are trying to use it. Not that sensible that it only is raised on some machines, it could be an install problem with a missing registry key in the Interface key. Needed because you are trying to use a component that is not thread-safe. Talk to the owner for support. – Hans Passant May 10 '17 at 10:59
  • Thanks for the replies. @Noseratio - I'm pretty new to threading so don't really understand the in-and-out. How would I check if the COM object is free-threaded? – OZ10 May 10 '17 at 13:08
  • @HansPassant What's confusing is that it did work and now doesn't, it does work if I remove async and older versions of the app still work. So the COM app installation should be fine. It seems like something has changed on my machine (VS2017?) that now compiles my app differently. – OZ10 May 10 '17 at 13:13
  • You need to run it in the single threaded apartment. – Ben May 10 '17 at 16:45
  • 1
    @OZ10, try using Stephen Toub's [`StaTaskScheduler`](https://blogs.msdn.microsoft.com/pfxteam/2010/04/07/parallelextensionsextras-tour-5-stataskscheduler/) instead of `Task.Run`. Here's also a [related question](http://stackoverflow.com/questions/21211998/stataskscheduler-and-sta-thread-message-pumping) which might help. – noseratio May 10 '17 at 19:58
  • Thanks for the suggestions. I tried creating a thread and setting it's apartment state to STA but it made no difference. I still don't know why it would require STA given that it's working on my machine and has worked in the past. – OZ10 May 17 '17 at 10:03
  • Wonder if someone can shed some light on a work-around I've found. This problem occurs when I 'new' one of the classes while running in a thread. If I move the variable to be at class level (so a class-level private variable outside of the thread) and 'new' it before I start the thread everything works. Does anybody know why this would be? – OZ10 May 17 '17 at 10:11

0 Answers0