MC++ FAQ
Saturday 07 | 20 | 2002
Windows Headers and MC++
Sometimes, mixing the Windows SDK headers and managed code can end up in unexpected results.

Consider this simple example:

#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#include <windows.h>

using namespace System;
using namespace System::Windows::Forms;

int main()
{
   MessageBox::Show(S"This is a MessageBox");
}

If you compile it, you'll get this error:

t.cpp(10) : error C2653: 'MessageBoxA' : is not a class or namespace name
t.cpp(10) : error C2065: 'Show' : undeclared identifier

Here, what you see is the #define's inserted by the Windows headers all over the place getting in the way. Specifically, the MessageBox() function in the Win32 API is no function at all, really, but rather a pair of them: MessageBoxA() and MessageBoxW(). These are, respectively, ANSI and Unicode variants of the method. The actual MessageBox name is just a #define that maps to one or the other depending on whether the UNICODE macro is defined or not.

What happens in the sample above is that the use of the MessageBox .NET class is replaced by the preprocessor into MessageBoxA, thus leading to an error because there's no class with this name. In other words, what the compiler actually sees in the code above is:

MessageBoxA::Show(S"This is a MessageBox");

The workaround is to temporarily undefine the MessageBox macro around the problem code, by adding this after #include'ing windows.h:

#undef MessageBox

The same can be done for other problematic macro definitions.

Posted by tomasr at July 20, 2002 12:53 PM
Comments

Hey, that's exactly what I needed to know. Thanks.

Posted by: Nate on January 28, 2003 07:12 PM

Thanks toamsr,

exactly to the point and helpful

Posted by: Igor on January 1, 2004 02:55 AM

You're a hero!

Posted by: Buddy on February 15, 2004 10:03 PM

Just what I needed.......

Posted by: Del Medina on February 25, 2004 05:15 PM

Thanks a lot - this was driving me crazy. I knew mixing Win32, MFC and MC++ code would cause problems but I thought I'd get further than showing a bleeping message box!

Posted by: Phil on February 26, 2004 04:58 PM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?

Copyright(C) 2002, Tomas Restrepo, All rights reserved. Powered by Movable Type 2.63