Problems with MessageBox.Show

K

Kevin Westhead

I'm experiencing a problem where I have a non-modal toolwindow that contains
a control derived from System.Windows.Forms.ComboBox displayed in an MDI
application. Whenever a .NET message box is displayed in the application the
derived combo boxes on the toolwindow appear to lose their base control,
i.e. base.WndProc(ref m) in the derived control's WndProc results in a
NullReferenceException. Admittedly this could be a problem with the derived
controls, however after looking at how System.Windows.Forms.MessageBox works
it seems like there are some "oddities". Also, if I call the Win32
MessageBox function directly via p/invoke, specifying the correct owner
window, then everything works fine.

Here is the section of System.Windows.Forms.MessageBox (ShowCore) that I've
been looking at:

if ((owner != null) && ((options & 2228224) != 0))
throw new ArgumentException(SR.GetString("CantShowMBServiceWithOwner"),
"style");

IntSecurity.SafeSubWindows.Demand();
num1 = (((buttons | icon) | defaultButton) | options);
ptr1 = IntPtr.Zero;

if ((options & 2228224) == 0)
{

if (owner == null)
ptr1 = UnsafeNativeMethods.GetActiveWindow();
else
ptr1 = owner.Handle;

}

Application.BeginModalMessageLoop();
result1 = MessageBox.Win32ToDialogResult(SafeNativeMethods.MessageBox(new
HandleRef(owner, ptr1), text, caption, num1));
Application.EndModalMessageLoop();
return result1;

The value of 2228224 corresponds to MessageBoxOptions.DefaultDesktopOnly |
MessageBoxOptions.ServiceNotification. The interesting thing here is that
the owner parameter is always ignored since owner must be null when options
contains the above flags. If owner is not null, the handle is never
retrieved because options cannot also contain the above flags and therefore
ptr1 remains as IntPtr.Zero. The other interesting thing is the
BeginModalMessageLoop/EndModalMessageLoop block. It appears as though this
disables all windows in the current thread, although I haven't looked at
this too deeply, and this further implies that the owner parameter plays no
part in establishing modality. Does anyone have any thoughts on this,
particularly with reference to the fact that the
BeginModalMessageLoop/EndModalMessageLoop block appears to be causing severe
problems for my non-modal windows? A single call to the Win32 MessageBox
function with a correctly specified owner window does not exhibit the same
problems.

TIA.
 
Y

Ying-Shen Yu[MSFT]

Hi Kevin,

As my understanding now, you have an MDI Program and you have a child tool
window in it , there is a derived combobox on the toolwindow. When you use
the MessageBox.Show, you got an NullReferenceException.
However, I'm not sure what do you mean that the ComboBox loses their base
control?
Could you show me the exception stacktrace output?

I'm trying to repro the problem to look into it, however it's not clear
that how to repro it, could you send me a repro sample to let me know more
about the problem you meet?

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
K

Kevin Westhead

"Ying-Shen Yu[MSFT]" said:
As my understanding now, you have an MDI Program and you have a child tool
window in it , there is a derived combobox on the toolwindow. When you use
the MessageBox.Show, you got an NullReferenceException.
However, I'm not sure what do you mean that the ComboBox loses their base
control?
Could you show me the exception stacktrace output?

I'm trying to repro the problem to look into it, however it's not clear
that how to repro it, could you send me a repro sample to let me know more
about the problem you meet?


I'm afraid it might be difficult to give you a true repro case since the MDI
app in this case is actually a dongle protected MFC app, and the modal
windows are also MFC windows that can host ActiveX and Windows Forms
controls. The NullReferenceException occurs in the overridden WndProc method
of the derived ComboBox, at the point where the derived class calls
"base.WndProc(ref m);". This only happens after I call
System.Windows.Forms.MessageBox.Show, and one of the things I noticed about
this call is that the IWin32Window parameter is never used. Instead
Application.BeginModalMessageLoop is used to disable the top-level windows
on the current thread and Application.EndModalMessageLoop is used to
re-enable them. If I use p/invoke to make my own MessageBox call in
conjunction with GetActiveWindow to get a parent HWND, then the application
works fine.

I'll try to post some stack information at the point of the exception
tomorrow.
 
K

Kevin Westhead

Kevin Westhead said:
I'm afraid it might be difficult to give you a true repro case since the MDI
app in this case is actually a dongle protected MFC app, and the modal
windows are also MFC windows that can host ActiveX and Windows Forms
controls. The NullReferenceException occurs in the overridden WndProc method
of the derived ComboBox, at the point where the derived class calls
"base.WndProc(ref m);". This only happens after I call
System.Windows.Forms.MessageBox.Show, and one of the things I noticed about
this call is that the IWin32Window parameter is never used. Instead
Application.BeginModalMessageLoop is used to disable the top-level windows
on the current thread and Application.EndModalMessageLoop is used to
re-enable them. If I use p/invoke to make my own MessageBox call in
conjunction with GetActiveWindow to get a parent HWND, then the application
works fine.

I'll try to post some stack information at the point of the exception
tomorrow.

Here is the call stack at the point of the exception, the '>' indicates the
point in the stack that my code is calling 'base.WndProc(ref m)' (I believe
the message is WM_SETFOCUS, which corresponds to the user clicking on the
control after a message box has been displayed):


system.windows.forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(System
..Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0xd4 bytes

system.windows.forms.dll!System.Windows.Forms.Control.DefWndProc(System.Wind
ows.Forms.Message m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!System.Windows.Forms.Control.WmSetFocus(System.Wind
ows.Forms.Message m = {System.Windows.Forms.Message}) + 0x121 bytes

system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows
..Forms.Message m = {System.Windows.Forms.Message}) + 0x747 bytes
system.windows.forms.dll!System.Windows.Forms.ComboBox.WndProc(System.Wind
ows.Forms.Message m = {System.Windows.Forms.Message}) + 0x143 bytesmywindowsforms.dll!mywindowsforms.ComboBox.WndProc(System.Windows.Forms.Mess
age m = {System.Windows.Forms.Message}) Line 476 + 0xc bytes C#

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.
Message m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Me
ssage m = {System.Windows.Forms.Message}) + 0xda bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallbac
k(int hWnd = 0xc085c, int msg = 0x7, int wparam = 0xa088c, int lparam = 0x0)
+ 0x3d bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(System
..Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0xd4 bytes

system.windows.forms.dll!System.Windows.Forms.Control.DefWndProc(System.Wind
ows.Forms.Message m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!System.Windows.Forms.Control.WmMouseDown(System.Win
dows.Forms.Message m = {System.Windows.Forms.Message},
System.Windows.Forms.MouseButtons button = Left, int clicks = 0x1) + 0x4e
bytes

system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows
..Forms.Message m = {System.Windows.Forms.Message}) + 0x484 bytes

system.windows.forms.dll!System.Windows.Forms.ComboBox.WndProc(System.Window
s.Forms.Message m = {System.Windows.Forms.Message}) + 0x2ca bytes

mywindowsforms.dll!mywindowsforms.ComboBox.WndProc(System.Windows.Forms.Mess
age m = {System.Windows.Forms.Message}) Line 476 + 0xc bytes C#

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.
Message m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Me
ssage m = {System.Windows.Forms.Message}) + 0xda bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallbac
k(int hWnd = 0xc085c, int msg = 0x201, int wparam = 0x1, int lparam =
0xc0008) + 0x3d bytes

The exception itself is:

An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object.

I've also added calls to Application.BeginModalMessageLoop() and
Application.EndModalMessageLoop() either side of my p/invoke call to
MessageBox, and this reproduces exactly the same behaviour as
System.Windows.Forms.MessageBox.Show, i.e. the above exception occurs.
Without these calls the message box has no adverse effect. My understanding
is that the disabling of top-level windows on the current thread is only
necessary when the parent HWND of the message box is NULL, which in a way
makes sense because the owner parameter is never used the current
implementation. Can you confirm whether or not this is a bug in the
MessageBox class?
 
Y

Ying-Shen Yu[MSFT]

Hi Kevin,

I apologize for the long delay,
I have read the trace log, it does seem the problem is occurred in the
NativeWindow.DefWndProc. However, the stack trace shows very limited
information, I'm unable to give suggestions nor to do further research on
it.

A suggestion would be to contact Microsoft Product Support Services via
telephone so that a dedicated Support Professional can assist with your
request.

To obtain the phone numbers for specific technology request please take a
look at the web site listed below.

http://support.microsoft.com/default.aspx?scid=fh;EN-US;PHONENUMBERS

Thank you for your patience.

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top