Solved

MessageBox, AfxMessageBox; what is the difference ?

Posted on 2004-03-26
18
5,724 Views
Last Modified: 2013-11-20
Ah hello.  Quick question this...

In my app I recently implemented code that would display a messagebox on the handling of a window message.  Now this handler is in the main app (i.e. MyApp.cpp).  However, when I code

MessageBox("Some string");

I get the error

'MessageBoxA' : function does not take 1 parameters

Yet this code works fine in my view class/dialogue classes.  Why ?

I can use AfxMessageBox("Some string") which will work fine, so what is the difference between the two ?

Also, MSDN states "Use the global function AfxMessageBox instead of this member function to implement a message box in your application" for CWnd::MessageBox.  Why is this ?

TIA !
0
Comment
Question by:mrwad99
  • 6
  • 4
  • 4
  • +2
18 Comments
 
LVL 3

Accepted Solution

by:
akalmani earned 20 total points
ID: 10687549
AfxMessageBox is a MFC inbuilt wrapper which takes only the Text and buttons (ID's buttons and Icons needed) as the parameters. But MessageBox is a CWnd function and it takes Text , Caption and Buttons(ID's buttons and Icons needed) as the parameters. You if are calling MessageBox in any of the CWnd derived classes then you need not pass Handle to the Window, Caption and button ID.

SDK function ::MessageBox(HWND, Text, Caption, UINT) -> HWND can be NULL, Need to give text, Caption can be NULL, UINT is defaulted to MB_OK.
CWnd derived classes : MessageBox(Text, Caption, UINT) -> Caption can be NULL, Text has to be given, UINT is defaulted to MB_OK.

CView is derived from CWnd so it worked. In CWinApp class you need to specify all or use AfxMessageBox everywhere.
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10687578
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10687628
Right, understood the point about derivatives of CWnd; that is clear.  Thanks.

But why does MSDN state "Use the global function AfxMessageBox instead of this member function to implement a message box in your application" for CWnd::MessageBox".  Is there something wrong/less efficient with CWnd::MessageBox as opposed to the AfxMessageBox ?  I could not pick up anything to hint at that from the links given, but you may know of something....
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10687744
Try the following
In your app show a messagebox with AfxMessageBox and try to do something on your main app while the messagebox is visible.  Repeat with the MessageBox.  See the difference (at least it used to work differently, I hope it still does).
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10687881
Its the modal issue and the title of the Messagebox varies. AfxMessageBox acts as modal to main window. The default title I think it takes the executable name.
But MessageBox will act modal to CView or the view window. You can specify your own title.
Modal means you won't be able to work with your application until you close the message box.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10687917
The default title I think it takes the executable name. (AfxMessageBox)

Yes - but it is possible to change it, how is quite well hidden in the help.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10688429
AndyAinscow/akalmani -

I tried the suggestion and there is no difference in the modal-ness (?) of the AfxMessageBox compared to that of the MessageBox.  The title is the same, which is the exe name.  One subtle difference is that there is the default 'alert' icon (a yellow triangle and black shriek) with AfxMessageBox() that does not occur with the standard MessageBox() present on the dialogue.

But that still does not explain *why* to use the Afx call.  Or should I just let the fact be that it "just is" better?
0
 
LVL 1

Assisted Solution

by:GJanusz
GJanusz earned 20 total points
ID: 10689010
@ Author

I've always assumed that the MFC developers made AfxMessageBox because they thought it would be a commonly used function, and wanted to make a more MFCized version of it than ::MessageBox.  It may have been based on their experience from what was commonly needed when just writing Win32 apps.

The biggest difference doesn't affect you:  You can't use AfxMessageBox unless you are using MFC.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 20 total points
ID: 10689112
I found in MSDN that you can pass a help id to AfxMessageMap. Then - when pressing F1 - a specific help of that message box is available.

That is the only difference i could find. However, i never used this feature and i don't know any application that uses it.

Regards, Alex
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 19

Author Comment

by:mrwad99
ID: 10689445
GJanusz:

You state ::MessageBox, and the MessageBox function I was talking about was actually CWnd::MessageBox - the reason why I could not use this in my main app has already been explained by akalmani.  However, I am now aware that there are *three* types of MessageBox() functions; the new addition being this

::MessageBox()

you mention.  What is this, to what class does it belong and what is the reason for having this *and* AfxMessageBox() ?

TIA !
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10690265
Ah, well, I'm guessing you are firmly implanted in MFC.  MFC is a primarily a great big wrapper (sometimes thin and sometimes not) for direct calls to services made available by the operating system.  The direct Windows OS function calls are typically known as Win32 calls.  Win32 programming has no classes as you know them in MFC.  They belong to the classless namespace -- which is why I labeled the MessageBox function as ::MessageBox.  In other words, they are all standalone functions that typically leverage Windows-internal memory, or memory you need to provide, to keep track of its states.  HANDLEs are an example of this.

If that doesn't scare you sufficiently, then take note that some things you may want to do are not provided by the MFC wrappers, and it occasionally pays off to be familiar with the Win32 functionality it is wrapping, so you can go look it up to see if what you need is provided at the Win32 level.

If you step into almost any MFC code, you will run into Win32 functions calls that you can't step into, but will be documented (almost always, ahem).
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10690293
Clarification for 2nd to last sentence in 1st paragraph:
In other words, they are all standalone functions that typically leverage Windows-internal memory, or memory you need to provide, to keep track of its states [rather than keeping track of state information in a class instance].

Clarification for very last sentence:
Documented in MSDN, I mean.  Certainly not in the code itself!  *choke*
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10690324
Ah, those Win32 functions are hidden away in all the windows DLLs then I take it, which is why you cannot step into them ?

And I also assume that AfxMessageBox is an MFC function; ::MessageBox as you have stated is a Win32 function.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 20 total points
ID: 10690444
Some development since my last comment.
MesssageBox and ::MessageBox are identical UNLESS you have a function in your class called MessageBox which takes the same parameters.

I had the situation that a client wanted a different title than the default with AfxMessageBox, so I thought simple, replaced AfxMessageBox with MessageBox and gave it the new title.
What I found was the following.  The message box appeared with the new title BUT the user could still do things on the application - it behaved as modeless.  Now most uses of the message box are to get a response back (yes/no) so still being able to run things on the program without responding is a catastrophe waiting to happen.  I rapidly changed it back to AfxMessageBox which forces the user to respond before they could continue and found out about how to change the title of the AfxMessageBox.

To summarise the MessageBox can behave very differently than AfxMessageBox (and in a dangerous way), use AfxMessageBox at all costs.
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10690454
Yes and Yes.  There are a tiny number of classless MFC functions.  The ones I know of all start with Afx like AfxMessageBox.  Another commonly used one is AfxGetMainWnd(), which you'll be glad for eventually!
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10690502
GJanusz: Thanks.

AndyAinscow: I cannot get a messagebox to be modeless with the standard MessageBox() call.  It is always modal.  How did you achieve this ?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10690822
I cannot get a messagebox to be modeless with the standard MessageBox() call.  It is always modal.  How did you achieve this ?

I found this a few years ago - maybe it has changed now (bug fixed?)

My situation was I had an SDI app without a doc or view!
I wanted a toolbar and the SDI Frame gave support for that but I had stripped the doc/view out and replaced it with a dialog (actually property sheet).  Following from akalmani's earlier comment you might be able to reproduce it with an MDI based app.  It might not apply to a bog standard SDI or dialog.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10694425
Right I increased the points to 80 for an even split, as everyone gave relevant information.

Thanks to all for this !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now