Calling Managed functions from unmanaged class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Dedhia

    #1

    Calling Managed functions from unmanaged class

    Hi All

    I have a VC++ 2005 MFC application with all classes defined as
    unmanaged classes. I want to write my application data in xml format.
    Since ADO.NET has buit in functions available for this, I want to use
    it. Is it possible to call Managed class functions from Unmanaged
    class? How to do it?

    I did something like this.
    I declared a managed class (in C++ CLI) called as MyManagedClass whose
    public interface has functions to write application data in XML format.
    It internally uses ADO.NET classes to achieve this. This class is
    declared in a seperate dll compiled with '\clr' option.

    Now I created an unmanaged wrapper class which wraps the managed class
    (defined in the same dll as that of managed class). The unmanaged
    wrapper, call it as MyUnmanagedWrap per, has a data member declared as

    gcroot<Object^h andleToManagedC lass;

    This is declared in the top of the class. It has wrapper methods whose
    implementation is something like this:

    void MyUnmanagedWrap per::Method1()
    {
    (*this)->Method1();
    }

    and then I have...

    class MyApplicationCl ass
    {
    MyUnmanagedWrap perInstance.Met hod1();
    }

    Everything complies well. However, when the above method is run, I get
    Access Violation error. It does not even go inside the method.

    Can anyone tell why there is access violation?

    Best regards
    Amit Dedhia

  • Noah Roberts

    #2
    Re: Calling Managed functions from unmanaged class

    Your question is off topic in comp.lang.c++

    Comment

    • Ben Voigt

      #3
      Re: Calling Managed functions from unmanaged class


      "Amit Dedhia" <amitdedhia@yah oo.comwrote in message
      news:1160608594 .944640.298370@ i3g2000cwc.goog legroups.com...
      Hi All
      >
      I have a VC++ 2005 MFC application with all classes defined as
      unmanaged classes. I want to write my application data in xml format.
      Since ADO.NET has buit in functions available for this, I want to use
      it. Is it possible to call Managed class functions from Unmanaged
      class? How to do it?
      >
      I did something like this.
      I declared a managed class (in C++ CLI) called as MyManagedClass whose
      public interface has functions to write application data in XML format.
      It internally uses ADO.NET classes to achieve this. This class is
      declared in a seperate dll compiled with '\clr' option.
      >
      Now I created an unmanaged wrapper class which wraps the managed class
      (defined in the same dll as that of managed class). The unmanaged
      wrapper, call it as MyUnmanagedWrap per, has a data member declared as
      >
      gcroot<Object^h andleToManagedC lass;
      Should be
      gcroot<MyManage dClass^handleTo ManagedClass;
      >
      This is declared in the top of the class. It has wrapper methods whose
      implementation is something like this:
      >
      void MyUnmanagedWrap per::Method1()
      {
      (*this)->Method1();
      Should be
      handleToManaged Class->Method1();
      }
      >
      and then I have...
      >
      class MyApplicationCl ass
      {
      MyUnmanagedWrap perInstance.Met hod1();
      }
      >
      Everything complies well. However, when the above method is run, I get
      Access Violation error. It does not even go inside the method.
      >
      Can anyone tell why there is access violation?
      Where is handleToManaged Class initialized? Initially it will hold a
      nullptr. Testing a gcroot for nullptr is a pain, try:
      (handleToManage dClass.operator->() != nullptr)


      >
      Best regards
      Amit Dedhia
      >

      Comment

      • Duane Hebert

        #4
        Re: Calling Managed functions from unmanaged class


        "Noah Roberts" <roberts.noah@g mail.comwrote in message
        news:1160608840 .838895.315300@ m7g2000cwm.goog legroups.com...
        Your question is off topic in comp.lang.c++
        >
        I mostly prefer to call "unmanaged" native.
        Unless, of course, you prefer to not manage your
        code unless MS does it for you. <g>


        Comment

        • Amit Dedhia

          #5
          Re: Calling Managed functions from unmanaged class


          Ben Voigt wrote:
          "Amit Dedhia" <amitdedhia@yah oo.comwrote in message
          news:1160608594 .944640.298370@ i3g2000cwc.goog legroups.com...
          Hi All

          I have a VC++ 2005 MFC application with all classes defined as
          unmanaged classes. I want to write my application data in xml format.
          Since ADO.NET has buit in functions available for this, I want to use
          it. Is it possible to call Managed class functions from Unmanaged
          class? How to do it?

          I did something like this.
          I declared a managed class (in C++ CLI) called as MyManagedClass whose
          public interface has functions to write application data in XML format.
          It internally uses ADO.NET classes to achieve this. This class is
          declared in a seperate dll compiled with '\clr' option.

          Now I created an unmanaged wrapper class which wraps the managed class
          (defined in the same dll as that of managed class). The unmanaged
          wrapper, call it as MyUnmanagedWrap per, has a data member declared as

          gcroot<Object^h andleToManagedC lass;
          >
          Should be
          gcroot<MyManage dClass^handleTo ManagedClass;
          >

          This is declared in the top of the class. It has wrapper methods whose
          implementation is something like this:

          void MyUnmanagedWrap per::Method1()
          {
          (*this)->Method1();
          Should be
          handleToManaged Class->Method1();
          >
          }

          and then I have...

          class MyApplicationCl ass
          {
          MyUnmanagedWrap perInstance.Met hod1();
          }

          Everything complies well. However, when the above method is run, I get
          Access Violation error. It does not even go inside the method.

          Can anyone tell why there is access violation?
          Where is handleToManaged Class initialized? Initially it will hold a
          nullptr. Testing a gcroot for nullptr is a pain, try:
          (handleToManage dClass.operator->() != nullptr)
          Ok Ben...I got the point

          Regarding initialization of the object, I would have a static method in
          MyManagedClass which returns an instance of iteself. I will call this
          method in ctor of the MyUnmanagedWrap per. However, I still have one
          question. I have both these classes defined in the same managed dll
          (compiled with /clr option). How do I use this dll (and its classes)
          from say an MFC dialog based application client?

          >
          >

          Best regards
          Amit Dedhia

          Comment

          • Ben Voigt

            #6
            Re: Calling Managed functions from unmanaged class


            "Amit Dedhia" <amitdedhia@yah oo.comwrote in message
            news:1160608594 .944640.298370@ i3g2000cwc.goog legroups.com...
            Hi All
            >
            I have a VC++ 2005 MFC application with all classes defined as
            unmanaged classes. I want to write my application data in xml format.
            Since ADO.NET has buit in functions available for this, I want to use
            it. Is it possible to call Managed class functions from Unmanaged
            class? How to do it?
            >
            I did something like this.
            I declared a managed class (in C++ CLI) called as MyManagedClass whose
            public interface has functions to write application data in XML format.
            It internally uses ADO.NET classes to achieve this. This class is
            declared in a seperate dll compiled with '\clr' option.
            >
            Now I created an unmanaged wrapper class which wraps the managed class
            (defined in the same dll as that of managed class). The unmanaged
            wrapper, call it as MyUnmanagedWrap per, has a data member declared as
            >
            gcroot<Object^h andleToManagedC lass;
            Should be gcroot<MyManage dClass^handleTo ManagedClass;
            >
            This is declared in the top of the class. It has wrapper methods whose
            implementation is something like this:
            >
            void MyUnmanagedWrap per::Method1()
            {
            (*this)->Method1();
            Should be handleToManaged Class->Method1();

            Thought I saw this exact same question with the exact same broken code a
            week ago.
            }
            >
            and then I have...
            >
            class MyApplicationCl ass
            {
            MyUnmanagedWrap perInstance.Met hod1();
            }
            >
            Everything complies well. However, when the above method is run, I get
            Access Violation error. It does not even go inside the method.
            >
            Can anyone tell why there is access violation?
            There is an access violation because in some code you have not shown us, you
            are performing a bad cast and trying to use an object as a type it isn't.

            Does your unmanaged class constructor set handleToManaged Class or leave it
            as nullptr?
            >
            Best regards
            Amit Dedhia
            >

            Comment

            Working...