Tuesday, January 5, 2010

Enabling Default Reply

Enabling Default Reply

Recently I came across situation, To respond to the pop up message boxes default button automatically without waiting for the user to do it. Some solutions came to my thought process like finding the top level window and send WM_MESSAGES and so on...

On my way to acheive this, I found an article about enabling default reply in Microsoft Developers Network for WinEmbedded5. I applied the same concept in my system while the application is running.
Okay I explain why these kind of mechanism you need?
Lets consider an example of implementing a Line drawing application. On LButton Down of the mouse you take the starting point (pX,pY) and you are rendering in the client area and On the LButton Up you take the end point (qX,qY); and calls the LineDraw method. Imagine while you are rendering on the client area meanwhile a MessageBox Alert like net Send Message box or Outlook Mail Alret Message box poped up; Basically kind of system model dialogs, you must need to respond to them and you lost the focus to your drawing co-ordinates. So the below lines are helpful while you are doing such operations.

User32 provides the functionality to intercept every MessageBox function and automatically choose the default button. Not all message box windows are produced through the standard MessageBox function; therefore, this feature might not operate on all windows.

To instruct NTUSER to intercept messages, an additional key and values are required in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control registry key. You might need to create the registry key and values on your system, as follows.

To set up your system to automatically reply to a message box without displaying it

1.In the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control registry key, create a new key named Error Message Instrument.
2.Under the Error Message Instrument key, add the values of type REG_DWORD from the following table.



ValueDataDescription
EnableDefaultReply0x00000001Enables the Enable Default Reply feature
EnableLogging 0x00000001 Enables event logging.


3.Under the Error Message Instrument registry key, add one of the LogSeverity values of type REG_DWORD from the following table.








Value DataDescription
LogSeverity0x00000000 = EMI_SEVERITY_ALLAll message box events are logged.
LogSeverity0x00000001 = EMI_SEVERITY_USERMessage box events with the dwStyle parameter defined are logged, including MB_USERICON, MB_ICONASTERISK, MB_ICONQUESTION, MB_ICONEXCLAMATION, and MB_ICONHAND.
LogSeverity0x00000002 = EMI_SEVERITY_INFORMATIONErrors, warnings, questions, and information are logged. Message box events with no dwStyle parameter or dwStyle = MB_ICONUSER are not logged.
LogSeverity0x00000003 = EMI_SEVERITY_QUESTIONErrors, warnings, and questions are logged. Information, events with no style, and user-defined severity levels are not logged.
LogSeverity0x00000004 = EMI_SEVERITY_WARNINGOnly errors and warnings are logged.
LogSeverity0x00000005 = EMI_SEVERITY_ERROR. (EMI_SEVERITY_MAX_VALUE)Only errors are logged.


To log message information to the event log

1.In the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog\Application registry key, create a new key named Error Instrument.
2.Under the Error Instrument key, add the registry values from the following table.
Value Type Value
TypesSupported REG_DWORD 0x00000007
EventMessageFile REG_EXPAND_SZ %SystemRoot%\System32\User32.dll

You must reboot your target system for the changes to take effect.

****Note: Make sure to revert back the changes beyond the scope of your application, otherwise you will loose the MessageBox alerts.

Hope you enjoyed reading this :)
yours,
Sreedhar