views:

69

answers:

1

I am working on a Windows program which is completely single-threaded and has no protection to any data structure. However, the program use DirectShow API which open their own internal message-dispatching, IVideoWindow::put_Visible for example. So the event-handling function which invokes IVideoWindow::put_Visible Method give other event-handling function chance to ruin its data.

Is there anyway to prevent such message-dispatching within an API?

A: 

The question is a little unclear to me. Are you talking about an API that enters a modal message loop internally? Is this creating some sort of re-entrancy problem for you? If that's the case, then there are probably better ways to solve your problem. If an API is entering a modal loop, then there's doubtless a good reason, and you should allow it to do so. (In your case, I'd guess that DirectShow is communicating with another COM object internally.) If it's properly written, a modal loop will still dispatch messages to the other windows on the same thread.

In any case, if you really want to do this, here's how:

PostQuitMessage( 0 ); // Signal quit to force the upcoming loop to exit
APIFunc(); // Enters modal loop internally
MSG msg;
PeekMessage( &msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE ); // Eat the next WM_QUIT

The last step is important. When a modal loop exits due to a WM_QUIT message, it must post a quit message of its own after it exits (so that the app knows to quit). If you don't eat it, your application will exit.

Peter Ruderman