Managed ( .net framework ) and unmanaged clash!
-
I'm trying to use some unmanaged legacy code with my managed application and I need to include some windows headers ( afxwin.h, afxtempl.h, mmsystem.h ) in stdafx.h but there are some conflicts with some .net framework API, for example, the System::Windows::Forms::MessageBox can't be compiled :
...
#using using namespace System::Windows::Forms;
...
System::Windows::Forms::MessageBox::Show(s, S"Received String");gives :
c.cpp(130) : error C2039: 'MessageBoxA' : is not a member of 'System::Windows::Forms' c.cpp(130) : error C2660: 'System::Windows::Forms::Control::Show' : function does not take 2 parameters
If I removed the legacy code and the headers, everything is ok ... Any thought ? nish ? ( as you are the expert ! ) Thanks. Max. here's the sample code... ( from basic generated managed project. )//stdafx.h
#include #include // MFC template support#ifndef _INC_MMSYSTEM
#pragma message("To avoid this message, please put mmsystem.h in your PCH")
#include #endif// xxx.cpp
#include "stdafx.h"#using #using #using #using #include using namespace System;
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::ComponentModel;// This is the entry point for this application
int _tmain(void)
{
MessageBox::Show(S"Allo", S"Request");
// TODO: Please replace the sample code below with your own.
Console::WriteLine(S"Hello World");
return 0;
} -
I'm trying to use some unmanaged legacy code with my managed application and I need to include some windows headers ( afxwin.h, afxtempl.h, mmsystem.h ) in stdafx.h but there are some conflicts with some .net framework API, for example, the System::Windows::Forms::MessageBox can't be compiled :
...
#using using namespace System::Windows::Forms;
...
System::Windows::Forms::MessageBox::Show(s, S"Received String");gives :
c.cpp(130) : error C2039: 'MessageBoxA' : is not a member of 'System::Windows::Forms' c.cpp(130) : error C2660: 'System::Windows::Forms::Control::Show' : function does not take 2 parameters
If I removed the legacy code and the headers, everything is ok ... Any thought ? nish ? ( as you are the expert ! ) Thanks. Max. here's the sample code... ( from basic generated managed project. )//stdafx.h
#include #include // MFC template support#ifndef _INC_MMSYSTEM
#pragma message("To avoid this message, please put mmsystem.h in your PCH")
#include #endif// xxx.cpp
#include "stdafx.h"#using #using #using #using #include using namespace System;
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::ComponentModel;// This is the entry point for this application
int _tmain(void)
{
MessageBox::Show(S"Allo", S"Request");
// TODO: Please replace the sample code below with your own.
Console::WriteLine(S"Hello World");
return 0;
}The problem is that windows.h #define's "MessageBox" to be either "MessageBoxA" (ANSI) or "MessabeBoxW" (Unicode). The preprocessor is replacing all instances of "MessageBox" in your code to "MessageBoxA", and of course that is not a valid type in the Forms namespace. You must #undef MessageBox. If you intend to use both the Windows MessageBox() and the .NET Framework's MessageBox(), you can get tricky and store the MessageBox macro on the preprocessor's stack:
#pragma push_macro(“MessageBox”)
#undef MessageBox...
#pragma pop_macro(“MessageBox”)
This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
-
The problem is that windows.h #define's "MessageBox" to be either "MessageBoxA" (ANSI) or "MessabeBoxW" (Unicode). The preprocessor is replacing all instances of "MessageBox" in your code to "MessageBoxA", and of course that is not a valid type in the Forms namespace. You must #undef MessageBox. If you intend to use both the Windows MessageBox() and the .NET Framework's MessageBox(), you can get tricky and store the MessageBox macro on the preprocessor's stack:
#pragma push_macro(“MessageBox”)
#undef MessageBox...
#pragma pop_macro(“MessageBox”)
This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
Nick Hodapp (MSFT) wrote: You must #undef MessageBox. That did the trick, but it's not something I really like ... Thanks. Max.
-
Nick Hodapp (MSFT) wrote: You must #undef MessageBox. That did the trick, but it's not something I really like ... Thanks. Max.
Me neither, but it's the reality of programming with libraries that have conflicting names... Nick This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
-
Me neither, but it's the reality of programming with libraries that have conflicting names... Nick This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
I don't think the problem is the conflicting named, I can live with that, but the problem is with the preprocessor that rewrite code for us ... Max.
-
I don't think the problem is the conflicting named, I can live with that, but the problem is with the preprocessor that rewrite code for us ... Max.
Well, that's just C++. :) This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
-
Me neither, but it's the reality of programming with libraries that have conflicting names... Nick This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
Wouldn't solving conflicting names with simple rules a real interesting stuff to come up with ? If I remember well, the Eiffel language has this feature, when deriving classes.
And I swallow a small raisin.