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. problem with unmanaged pointers

problem with unmanaged pointers

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++debuggingannouncement
8 Posts 2 Posters 0 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.
  • A Offline
    A Offline
    Arris74
    wrote on last edited by
    #1

    Hello, I have an MFC application that i would like to mix with Managed C++. I changed the compiler settings and everything compiles fine. For the moment I did not write any managed code but I have the message as follows at execution: "An unhandled exception of type 'System.TypeLoadException' occurred in ApplicationName.exe Additional information: Could not load type _TREEITEM from assembly ApplicationName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null." This message occurs before execution of the function that returns a pointer. I have put a breakpoint in it but it is never called. I think it is unmanaged pointer problem because I have modified the function to return a reference to CWnd and everything was Ok. below is the code snipped, Could please help me because I am 100% newbee with C++/CLI:confused: Declaration

    // Attributes
    private:
    CWnd* m_pView;

    //Declaration of the function
    CWnd* CreateTreeView();

    Definition

    //Constructor definition
    BnBPaneView::BnBPaneView():m_pView(NULL){}

    //definition of the function
    int BnBPaneView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CWnd::OnCreate(lpCreateStruct) == -1)
    return -1;

    m\_pView = CreateTreeView();// error message here. The function is not called at all.
    

    //Create view is a function that creates a treeview and uses a CTreeCtrl object.

    etc..
    .
    .
    .
    }

    M 1 Reply Last reply
    0
    • A Arris74

      Hello, I have an MFC application that i would like to mix with Managed C++. I changed the compiler settings and everything compiles fine. For the moment I did not write any managed code but I have the message as follows at execution: "An unhandled exception of type 'System.TypeLoadException' occurred in ApplicationName.exe Additional information: Could not load type _TREEITEM from assembly ApplicationName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null." This message occurs before execution of the function that returns a pointer. I have put a breakpoint in it but it is never called. I think it is unmanaged pointer problem because I have modified the function to return a reference to CWnd and everything was Ok. below is the code snipped, Could please help me because I am 100% newbee with C++/CLI:confused: Declaration

      // Attributes
      private:
      CWnd* m_pView;

      //Declaration of the function
      CWnd* CreateTreeView();

      Definition

      //Constructor definition
      BnBPaneView::BnBPaneView():m_pView(NULL){}

      //definition of the function
      int BnBPaneView::OnCreate(LPCREATESTRUCT lpCreateStruct)
      {
      if (CWnd::OnCreate(lpCreateStruct) == -1)
      return -1;

      m\_pView = CreateTreeView();// error message here. The function is not called at all.
      

      //Create view is a function that creates a treeview and uses a CTreeCtrl object.

      etc..
      .
      .
      .
      }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      i don't see anything involving managed code at all here so I'm not sure how the problem could be with unmanaged pointers. BnBPaneView is an unmanaged class, right (derived from an MFCclass)? Mark

      "Go that way, really fast. If something gets in your way, turn."

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        i don't see anything involving managed code at all here so I'm not sure how the problem could be with unmanaged pointers. BnBPaneView is an unmanaged class, right (derived from an MFCclass)? Mark

        "Go that way, really fast. If something gets in your way, turn."

        A Offline
        A Offline
        Arris74
        wrote on last edited by
        #3

        Yes it is unmanaged, derived from CWnd: class BnBPaneView : public CWnd Actually all what I have done is to change the compiler settings recommended in the MSDN: Use managed extensions = yes Enable minimum Rebuild = no basic runtime checks = default And before changing those settings, the application was working fine.

        M 1 Reply Last reply
        0
        • A Arris74

          Yes it is unmanaged, derived from CWnd: class BnBPaneView : public CWnd Actually all what I have done is to change the compiler settings recommended in the MSDN: Use managed extensions = yes Enable minimum Rebuild = no basic runtime checks = default And before changing those settings, the application was working fine.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          hmm OK. I sure forgot about this - I personally used #pragma managed/unmanaged to compile all my MFC code to native early on (when I first switched to mixed-mode) otherwise I'd see this all the time :) Here's a few links, all with the same fix... TypeLoadException in code using common controls[^] BUG: HTREEITEM May Cause TypeLoadException in a Managed C++ Application[^] Could not loa type _TREEITEM[^]

          "Go that way, really fast. If something gets in your way, turn."

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            hmm OK. I sure forgot about this - I personally used #pragma managed/unmanaged to compile all my MFC code to native early on (when I first switched to mixed-mode) otherwise I'd see this all the time :) Here's a few links, all with the same fix... TypeLoadException in code using common controls[^] BUG: HTREEITEM May Cause TypeLoadException in a Managed C++ Application[^] Could not loa type _TREEITEM[^]

            "Go that way, really fast. If something gets in your way, turn."

            A Offline
            A Offline
            Arris74
            wrote on last edited by
            #5

            Does that mean that those who want to mix their applications must specify #pragma unmanaged before all their functions that are implemented with native code? It is a hudge work to pass through thousands of functions. While reading some articles on the subject, I understood that thanks to JIW ( just it works), there is nothing to do except changing the compiler settings. And regarding MFC applications, following directives #using #using using namespace System; using namespace System::Windows::Forms; are to be added in the stdafx.h file.

            M 1 Reply Last reply
            0
            • A Arris74

              Does that mean that those who want to mix their applications must specify #pragma unmanaged before all their functions that are implemented with native code? It is a hudge work to pass through thousands of functions. While reading some articles on the subject, I understood that thanks to JIW ( just it works), there is nothing to do except changing the compiler settings. And regarding MFC applications, following directives #using #using using namespace System; using namespace System::Windows::Forms; are to be added in the stdafx.h file.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Arris7 wrote:

              While reading some articles on the subject, I understood that thanks to JIW ( just it works), there is nothing to do except changing the compiler settings.

              That's correct.

              Arris7 wrote:

              Does that mean that those who want to mix their applications must specify #pragma unmanaged before all their functions that are implemented with native code?

              No. You can, but it's not necessary. If you decide to use #pragma unmanaged/unmanaged, they effect code from that point on, until another is encountered, or the end of file is reached. That means they only need to be at the top of a source file (under the #includes), not around every function. This worked well for me going from a large MFC code base to adding managed code. That also means you can mix native/CLR compilation in the same source file.

              Arris7 wrote:

              And regarding MFC applications, following directives #using #using using namespace System; using namespace System::Windows::Forms; are to be added in the stdafx.h file.

              I suppose you could put those in the precompiled header. I'm not sure what most people do. I personally include just the ones I need on a per-.cpp/.h file basis, especially with namespaces. Did the TreeView struct stuff work out ok? Mark

              "Go that way, really fast. If something gets in your way, turn."

              A 1 Reply Last reply
              0
              • M Mark Salsbery

                Arris7 wrote:

                While reading some articles on the subject, I understood that thanks to JIW ( just it works), there is nothing to do except changing the compiler settings.

                That's correct.

                Arris7 wrote:

                Does that mean that those who want to mix their applications must specify #pragma unmanaged before all their functions that are implemented with native code?

                No. You can, but it's not necessary. If you decide to use #pragma unmanaged/unmanaged, they effect code from that point on, until another is encountered, or the end of file is reached. That means they only need to be at the top of a source file (under the #includes), not around every function. This worked well for me going from a large MFC code base to adding managed code. That also means you can mix native/CLR compilation in the same source file.

                Arris7 wrote:

                And regarding MFC applications, following directives #using #using using namespace System; using namespace System::Windows::Forms; are to be added in the stdafx.h file.

                I suppose you could put those in the precompiled header. I'm not sure what most people do. I personally include just the ones I need on a per-.cpp/.h file basis, especially with namespaces. Did the TreeView struct stuff work out ok? Mark

                "Go that way, really fast. If something gets in your way, turn."

                A Offline
                A Offline
                Arris74
                wrote on last edited by
                #7

                Mark, Thank you so much for those great details, I have added #pragma unmanaged on the top of all my classes under #include. Now it compiles and execute very fine. I just noticed that compilation and buid process has slow down. This time I agree it is JIW;)

                M 1 Reply Last reply
                0
                • A Arris74

                  Mark, Thank you so much for those great details, I have added #pragma unmanaged on the top of all my classes under #include. Now it compiles and execute very fine. I just noticed that compilation and buid process has slow down. This time I agree it is JIW;)

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  You're welcome! I hope I could help. The only real slowdown I've noticed is when starting mixed-mode apps in the debugger :) Cheers! Mark

                  "Go that way, really fast. If something gets in your way, turn."

                  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