Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C++/CLI C MFC ATL WTL STL
I'm automating Word and when I call a function that doesn't return for a long time (for example a call to
app.Run(_T("FilePrint"));) it tends to freeze the UI of my app until it returns from the call, so I tried to run this code from a worker thread, but then COM complains and throws an exception RPC_E_WRONG_THREAD - "0x8001010E: The application called an interface that was marshalled for a different thread."

How can I solve this problem?

btw, there's an explanation about RPC_E_WRONG_THREAD in MSDN - INFO: Explanation of RPC_E_WRONG_THREAD Error[^], but I couldn't figure out anything from it.

Posted 13-Aug-09 7:43am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Could you do all of the automation, including creation of the Word object, on the worker thread?

Basically you're running foul of COM single-threaded apartments and things, I think. This article[^] explains them well.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You need to marshal calls across threads/apartments. See this[^] article for different ways to perform cross thread marshalling.
CoMarshalInterThreadInterfaceInStream() would probably be the easiest approach. Look it up in the MSDN documentation. It's quite easy to use.
  Print Answers RSS
0 Maciej Los 475
1 OriginalGriff 425
2 Sascha Lefévre 289
3 CPallini 285
4 CHill60 260
0 Sergey Alexandrovich Kryukov 9,773
1 OriginalGriff 9,290
2 Peter Leow 5,137
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,036

Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 4 Aug 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100