473,698 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble displaying MessageBox from VC++ form

I'm having a strange problem (or at least it seems strange to me) trying to
display a MessageBox in a VC++ .NET forms application.

If I put the call to MessageBox::Sho w in the form's .h file, it works just
fine.

If I put the call in the .cpp file, I get the following two errors:

error C2653: 'MessageBoxA': is not a class or namespace name
error C2660: 'System::Window s::Forms::Contr ol::Show': function does not take
2 arguments

I have tested this out as follows:

Create a .NET Windows Forms application with the default form, Form1, and
add two buttons to it, button1 and button2. Double-click on each button to
add the default event handlers. In the class declaration in Form1.h, I added
this:

// Declaration and definition together in .h file
private: System::Void button1_Click(S ystem::Object * sender,
System::EventAr gs * e)
{
MessageBox::Sho w(S"Clicked Button 1",S"Title") ;
}

// Declaration only in .h file, definition in the .cpp file
private: System::Void button2_Click(S ystem::Object * sender,
System::EventAr gs * e);

Then in the class definition in Form1.cpp, I added:

// Definition in .cpp file corresponding to declaration in .h file
System::Void Form1::button2_ Click(System::O bject * sender,
System::EventAr gs * e)
{
MessageBox::Sho w(S"Clicked Button 2",S"Title") ;
}

When I'm typing the code, I get the expected auto-completion after the
MessageBox:: so it seems that the System::Windows ::Forms namespace is known,
as is the specific MessageBox object. System.Windows. Forms is included as a
reference in the project. I have also tried explicitly including

#using <System.Windows .Forms.dll>
using namespace System::Windows ::Forms;

in the .cpp file, but that has no effect.

I tried to figure out what is going on with the reference to MessageBoxA in
the first error message. Searching in the help for MessageBoxA turns up a
reference to "Macros and the Preprocessor" which says

#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif // !UNICODE

So apparently UNICODE is not defined in my .cpp file, but it is in my .h
file?

Going on this I experimented -- adding

#include <windows.h>

to the .h file (not something you'd normally do -- it's an experiment)
causes the same two errors to occur in the .h file as in the .cpp file. So
now it looks like this is what's happening: Inclusion of windows.h in the
..cpp file causes MessageBox to be expanded into MessageBoxA which is
unknown. Absence of windows.h from the .h file causes MessageBox to be
expanded into MessageBoxW, which works. However, attempting to call
MessageBoxW explicity from the .cpp file gets me the same two errors again,
except for a different class:

error C2653: 'MessageBoxW': is not a class or namespace name
error C2660: 'System::Window s::Forms::Contr ol::Show': function does not take
2 arguments

In short -- there doesn't seem to be any way to display a MessageBox from
the .cpp code of a form. Which leaves the option of displaying it from the
..h file, which is just wrong in so many ways, but gets the job done.

Suggestions? Thanks in advance.
Nov 17 '05 #1
20 8895
If you are doing a #include <windows.h", MessageBox is a macro which expands
to either MessageBoxA or MessageBoxW depending in whether you have UNICODE
defined. So your MessageBox::Sho w then expands to MessageBoxA::Sh ow and the
compiler says correctly there is no MessageBoxA class or namespace in .NET,
which is correct. As a workaround try an #undef MessageBox after your
#include <windows.h> .

Peter E. Granger wrote:
I'm having a strange problem (or at least it seems strange to me)
trying to display a MessageBox in a VC++ .NET forms application.

If I put the call to MessageBox::Sho w in the form's .h file, it works
just fine.

If I put the call in the .cpp file, I get the following two errors:

error C2653: 'MessageBoxA': is not a class or namespace name
error C2660: 'System::Window s::Forms::Contr ol::Show': function does
not take 2 arguments

Nov 17 '05 #2
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:Ol******** ******@TK2MSFTN GP12.phx.gbl...
If you are doing a #include <windows.h", MessageBox is a macro which expands to either MessageBoxA or MessageBoxW depending in whether you have UNICODE
defined. So your MessageBox::Sho w then expands to MessageBoxA::Sh ow and the compiler says correctly there is no MessageBoxA class or namespace in ..NET, which is correct. As a workaround try an #undef MessageBox after your
#include <windows.h> .


Thanks, Edward, for confirming what I'd determined, and for the workaround.
Doing the #undef MessageBox works, although it seems really
counterintuitiv e, undefining the specific thing that I want defined. (I
know, I'm just removing a prior redefinition of it, but it still seems odd.)

Just to be thorough, I also checked what happened if I switched the
character set to Unicode in the project configuration. As expected,
MessageBox is now expanded to MessageBoxW, which is also unknown.

Can you shed any light on the reason behind this? It seems strange that the
macro that's defined in the windows.h header prevents use of the MessageBox
when it's such a common object in Windows applications. Or is there perhaps
a different class that can be used to do the same job, that doesn't have
this problem? I see quite a few different dialog controls in the toolbox,
but no simple message box control.

Thanks again.

- Peter
Nov 17 '05 #3
Peter E. Granger wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:Ol******** ******@TK2MSFTN GP12.phx.gbl...
If you are doing a #include <windows.h", MessageBox is a macro which
expands to either MessageBoxA or MessageBoxW depending in whether
you have UNICODE defined. So your MessageBox::Sho w then expands to
MessageBoxA::Sh ow and the compiler says correctly there is no
MessageBoxA class or namespace in .NET, which is correct. As a
workaround try an #undef MessageBox after your #include <windows.h> .


Thanks, Edward, for confirming what I'd determined, and for the
workaround. Doing the #undef MessageBox works, although it seems
really counterintuitiv e, undefining the specific thing that I want
defined. (I know, I'm just removing a prior redefinition of it, but
it still seems odd.)

Just to be thorough, I also checked what happened if I switched the
character set to Unicode in the project configuration. As expected,
MessageBox is now expanded to MessageBoxW, which is also unknown.

Can you shed any light on the reason behind this?


Macros in C++ get textually substituted before the compile sees
MessageBox::Sho w. So what the compiler sees, if you #include <windows.h>
before invoking MessageBox::Sho w is MessageBoxA::Sh ow. Since there is no
MessageBoxA::Sh ow the compilation fails. If you use the VC++ precompiler to
precompile your code to produce a .i file and view the .i file, you will see
this yourself.
Nov 17 '05 #4
Peter E. Granger wrote:
error C2653: 'MessageBoxA': is not a class or namespace name
error C2660: 'System::Window s::Forms::Contr ol::Show': function does not
take 2 arguments


I discovered this on my first day of VC++.NET. It's astonishing that
M$ did not find such an obvious fault in their own extensive battery
of tests. ;-)

What more proof could you need that macros are evil?
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp

Nov 17 '05 #5
We knew of this before we even needed to have the first test fail. The issue
is just that this is simply the way the preprocessor works and that the kind
of extensive change needed to the windows headers to avoid this is extremely
scary.

We have several possible solutions we are investigating for future versions.

Ronald Laeremans
Visual C++ team

"Arnold the Aardvark" <no@way.com> wrote in message
news:bv******** ***********@new s.demon.co.uk.. .
Peter E. Granger wrote:
error C2653: 'MessageBoxA': is not a class or namespace name
error C2660: 'System::Window s::Forms::Contr ol::Show': function does not
take 2 arguments


I discovered this on my first day of VC++.NET. It's astonishing that
M$ did not find such an obvious fault in their own extensive battery
of tests. ;-)

What more proof could you need that macros are evil?
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp

Nov 17 '05 #6
Ronald Laeremans [MSFT] wrote:
We knew of this before we even needed to have the first test fail. The
issue is just that this is simply the way the preprocessor works.


I'm grateful you replied but is this response intended to inspire
me with confidence? Now I wonder what else is lurking in VC++.NET
that the compiler may not be good enough to complain about...

Simple solution - don't use names that are already *commonly used*
macros.
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp

Nov 17 '05 #7
Ronald Laeremans [MSFT] <ro*****@online .microsoft.com> wrote:
We knew of this before we even needed to have the first test fail. The issue
is just that this is simply the way the preprocessor works and that the kind
of extensive change needed to the windows headers to avoid this is extremely
scary.
While the change would certainly impact
a lot of declarations in the Windows
headers, it would also be fairly simple.
Replacing
#if define(UNICODE)
# define SomeFunc SomeFuncW
#else
# define SomeFunc SomeFuncW
#endif
with
#if define(UNICODE)
inline void SomeFunc() {SomeFuncW();}
#else
inline void SomeFunc() {SomeFuncA();}
#endif
doesn't seem very sophisticated even
when different argument lists and return
types come into play.
We have several possible solutions we are investigating for future versions.

Ronald Laeremans
Visual C++ team
[...]


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #8
"Hendrik Schober" <Sp******@gmx.d e> wrote in message
news:up******** ******@TK2MSFTN GP10.phx.gbl...
While the change would certainly impact
a lot of declarations in the Windows
headers, it would also be fairly simple.
Replacing
#if define(UNICODE)
# define SomeFunc SomeFuncW
#else
# define SomeFunc SomeFuncW
#endif
with
#if define(UNICODE)
inline void SomeFunc() {SomeFuncW();}
#else
inline void SomeFunc() {SomeFuncA();}
#endif
doesn't seem very sophisticated even
when different argument lists and return
types come into play.


Do all compilers understand inline?

Regards,
Will
Nov 17 '05 #9
William DePalo [MVP VC++] wrote:
"Hendrik Schober" <Sp******@gmx.d e> wrote in message
news:up******** ******@TK2MSFTN GP10.phx.gbl...
While the change would certainly impact
a lot of declarations in the Windows
headers, it would also be fairly simple.
Replacing
#if define(UNICODE)
# define SomeFunc SomeFuncW
#else
# define SomeFunc SomeFuncW
#endif
with
#if define(UNICODE)
inline void SomeFunc() {SomeFuncW();}
#else
inline void SomeFunc() {SomeFuncA();}
#endif
doesn't seem very sophisticated even
when different argument lists and return
types come into play.


Do all compilers understand inline?


It is a keyword of the C++ standard language. If a compiler doesn't
understand it, I don't think MS should worry about it. Your point may be
that 'inline' doesn't force the compiler to actually inline the call, which
I believe is true. But if it doesn't inline the call, then I believe it must
create a function which represents it, and to which any call of SomeFunc
will be directed, so Mr. Schober's solution should still be OK.
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3540
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET) template. Briefly -------------------------------------------------------------------------------------------- I need to create dynamically some controls on the forms, and display these
11
541
by: Rich Tasker | last post by:
I have a strange situation. A simple call to MessageBox.Show("XXX", "YYY") does not display the message in the messagebox with a visible font. Based on the content of the messagebox, the box sizes properly so I know there is a message in there. I also found a keyboard shortcut where <ctrl>+<insert> will copy the contents of the messagebox to the clipboard in plain text. I see my messagebox text in the clipboard contents but not in the...
3
3696
by: Cybertof | last post by:
Hello, I have a strange behavior. On a MyForm form, i have a button with this code ----> MessageBox.Show("hello"); When i start the project with this single form ----> Application.Run(new MyForm()); It works : when i click then i have the messagebox and the form still continues its message loop.
4
10005
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
3
7424
by: Sin | last post by:
I'm currently evaluating VC.NET as the new platform for the company I work for and things are looking grim... We're up against another IDE which took me about 5 minutes to master and I've been bitching at .NET for the whole day now not being able to do something as simple as showing a textbox's content in a damn MessageBox... The TextBox.Text is a String... MessageBox takes a LPCSTR (plain single byte char*)... All our current code base...
1
1734
by: joye | last post by:
Hi, I know how the show a MessageBox in Form.h module by using VC.NET. But I don't know how to show the MessageBox at another class such as task.cpp which called by the Form.h; my question is how to pass the arguments from Form.h to a MessageBox::Show() which included in a class method. Thanks. Tsung-Yu Liu
0
1585
by: Craig | last post by:
Hello, I have a web form that allows the user to move a unit on a manufacturing router. The user click one of five buttons which each represent a step on the router. When the user clicks any of the five buttons, I do a SQL select to gather some data about this unit and populate a datagrid w/ the resulting data. I want the data in the datagrid to be displayed to the user, and THEN I want to prompt the user w/ a messagebox(), that ask...
1
1296
by: Flack | last post by:
Hey guys, Here is the scenario: I have the main form open and a background thread is running. The background thread sometimes needs access to the main forms graphics object and gets it by calling Graphics g = CreateGraphics(). Now, I close my main form while the background thread is running and when the background thread gets to the CreateGraphics() line, it throws an
2
5485
by: condolesaP | last post by:
Hello everyone, I m customozing AutoCAD using VC++ I want to display a messageBox in AutoCAD window can anybody please help me through it
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.