The problem is related to the inability of COM to marshal an outgoing COM call while processing a SendMessage
request. The error that appears is RPC_E_CANTCALLOUT_ININPUTSYNCCALL (0x8001010D)
that you are talking about. It seemed to me that this applies only to SendMessage calls, which are part of the interthread incoming COM requests, however this could be a false assumption.
A typical workaround would be to replace SendMessage
with PostMessage
, followed by waiting for an object, event, or synchronization semaphore. Thus, the background thread of your caller does not support messaging to synchronize calls and waits offline, in the main thread, the message is sent through a regular message queue and ultimately reaches the same handler.
As a bonus, you have the option to safely terminate the background thread. If it is currently blocked by the SendMessage API waiting for a modal dialog, the proposed change will allow you to signal a synchronization object from the main thread and continue to work, for example. if you want to complete it safely.
An alternative solution could be to call the InSendMessage
function, and if true, cancel the modal user interface, for example. sending the message to yourself again to put the form in another message handler later.
source share