Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Managed ( .net framework ) and unmanaged clash!

Managed ( .net framework ) and unmanaged clash!

Scheduled Pinned Locked Moved Managed C++/CLI
c++csharpdotnetgraphicsjson
7 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    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;
    }

    N 1 Reply Last reply
    0
    • M Maximilien

      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;
      }

      N Offline
      N Offline
      Nick Hodapp
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • N Nick Hodapp

        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.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Nick Hodapp (MSFT) wrote: You must #undef MessageBox. That did the trick, but it's not something I really like ... Thanks. Max.

        N 1 Reply Last reply
        0
        • M Maximilien

          Nick Hodapp (MSFT) wrote: You must #undef MessageBox. That did the trick, but it's not something I really like ... Thanks. Max.

          N Offline
          N Offline
          Nick Hodapp
          wrote on last edited by
          #4

          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.

          M S 2 Replies Last reply
          0
          • N Nick Hodapp

            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.

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            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.

            N 1 Reply Last reply
            0
            • M Maximilien

              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.

              N Offline
              N Offline
              Nick Hodapp
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • N Nick Hodapp

                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.

                S Offline
                S Offline
                Stephane Rodriguez
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups