IMessageFilter

An executable component can implement the IMessageFilter interface in each STA. This only works in an STA, which is tuned for use with UI applications.

IMessageFilter* pMF = new CMessageFilter;
IMessageFilter* pOldMF;
CoRegisterMessageFilter(pMF, &pOldMF);

References

IMessageFilter (objidl.h) - Win32 apps | Microsoft Learn