1. Potential issue with message_box() function
- Posted by LarryMiller Feb 17, 2016
- 1498 views
Internally the message_box() function uses the API function GetActiveWindow() to get the handle to the active window and uses that as it's parent. There are some potentially serious issues with this. The correct way is to pass the handle of your applications window, not that of a window that just happens to be active at the time.
Raymond Chen, a long time Microsoft developer, talks about this issue here: https://blogs.msdn.microsoft.com/oldnewthing/20070727-00/?p=25823/
What if anything should be done about this I will leave to the developers.
2. Re: Potential issue with message_box() function
- Posted by jimcbrown (admin) Feb 17, 2016
- 1476 views
Thank you for looking into and reporting this!
3. Re: Potential issue with message_box() function
- Posted by Spock Feb 17, 2016
- 1441 views
Internally the message_box() function uses the API function GetActiveWindow() to get the handle to the active window and uses that as it's parent. There are some potentially serious issues with this..
This is what happens in Arwen. The reason being that I just might want to invoke the message box before the main window handle is even created. Oh I know I shouldn't but it is just so darn convenient! The alternatives were to issue some crummy console message or invest in a custom message box window. I see Andy Drummond has already posted one to the contributions page.. must take a look..
Spock
4. Re: Potential issue with message_box() function
- Posted by petelomax Feb 17, 2016
- 1482 views
Hmm. Quite amazing something that old, that simple, and that heavily used could be hiding that for quite so long..
First off, clearly it is a serious omission that you cannot supply a better hwnd to message_box() if you know one. I was thinking of a 4th defaulted parameter, and a new set_mb_hwnd() routine to allow an app to make the top-level window (or whatever) the default in all future message_box() calls.
Secondly, I'm a bit shocked and dismayed that Raymond Chen - the Raymond Chen, mind you, (bit of a big-time fan of his) did not mention MB_TASKMODAL, as that seems to be the thing you need when hwnd is NULL, assuming you want it to be modal. According to the docs, it suspends all top-level windows associated with the current thread. I've even gone so far as to (add said 4th param, rip out GetActiveWindow,) and add
if hWnd=NULL then style = or_bits(style,MB_TASKMODAL) end if
to my copy of msgbox.e - and I guess I'll find out in the next few days if there's anything amiss with that.
Pete
5. Re: Potential issue with message_box() function
- Posted by andi49 Feb 20, 2016
- 1450 views
Hallo
Hmm. Quite amazing something that old, that simple, and that heavily used could be hiding that for quite so long..
I'am really sure nobody ever used this include for a real program...
First off, clearly it is a serious omission that you cannot supply a better hwnd to message_box() if you know one. I was thinking of a 4th defaulted parameter, and a new set_mb_hwnd() routine to allow an app to make the top-level window (or whatever) the default in all future message_box() calls.
A include in win32 that only exports MessageBox() is completly useless (from my point of view) and needs to be moved to demo or something like this.
Secondly, I'm a bit shocked and dismayed that Raymond Chen - the Raymond Chen, mind you, (bit of a big-time fan of his) did not mention MB_TASKMODAL, as that seems to be the thing you need when hwnd is NULL, assuming you want it to be modal. According to the docs, it suspends all top-level windows associated with the current thread. I've even gone so far as to (add said 4th param, rip out GetActiveWindow,) and add
if hWnd=NULL then style = or_bits(style,MB_TASKMODAL) end if </eucode> to my copy of msgbox.e - and I guess I'll find out in the next few days if there's anything amiss with that.
Pete
The problem with msgbox.e is:\
ret = c_func(msgbox_id, {c_func(get_active_id, {}),text_ptr, title_ptr, or_style}) -- get_active_id() should be NULL or a valid hwnd -- grabbing the foregrund window is just bad style
In a single threaded App there is not much difference if you use MB_TASKMODAL or MB_APPSMODAL (default).
The call to Messagebox() will block your program (no matter if you pass MB_APPSMODAL or MB_TASKMODAL) , even Mr. Petzold used it this way (with NULL as hwnd and MB_APPSMODAL).
You may play with this code. tinEWG passes WinHwnd to MessageBox() as default, so setting it to zero gives you a test situation.
include gui/tin/tinewg.exw atom handle=Window() -- Window() returns the handle that is stored in WinHwnd atom b1=Control(Button,"Button",0,0,80,20) WinHwnd=0 -- InfoMsg() uses WinHwnd for the MessageBox handle InfoMsg("MyText","MyTitle") WinHwnd=handle -- restore the handle for WinHwnd WinMain()
Andreas