Calling Managed functions from unmanaged class

Discussion in 'C++' started by Amit Dedhia, Oct 12, 2006.

  1. Amit Dedhia

    Amit Dedhia Guest

    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 MyUnmanagedWrapper, has a data member declared as

    gcroot<Object^> handleToManagedClass;

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

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

    and then I have...

    class MyApplicationClass
    {
    MyUnmanagedWrapperInstance.Method1();
    }

    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
    Amit Dedhia, Oct 12, 2006
    #1
    1. Advertising

  2. Amit Dedhia

    Noah Roberts Guest

    Your question is off topic in comp.lang.c++
    Noah Roberts, Oct 12, 2006
    #2
    1. Advertising

  3. Amit Dedhia

    Ben Voigt Guest

    "Amit Dedhia" <> wrote in message
    news:...
    > 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 MyUnmanagedWrapper, has a data member declared as
    >
    > gcroot<Object^> handleToManagedClass;


    Should be
    gcroot<MyManagedClass^> handleToManagedClass;

    >
    > This is declared in the top of the class. It has wrapper methods whose
    > implementation is something like this:
    >
    > void MyUnmanagedWrapper::Method1()
    > {
    > (*this)->Method1();

    Should be
    handleToManagedClass->Method1();

    > }
    >
    > and then I have...
    >
    > class MyApplicationClass
    > {
    > MyUnmanagedWrapperInstance.Method1();
    > }
    >
    > 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 handleToManagedClass initialized? Initially it will hold a
    nullptr. Testing a gcroot for nullptr is a pain, try:
    (handleToManagedClass.operator->() != nullptr)



    >
    > Best regards
    > Amit Dedhia
    >
    Ben Voigt, Oct 12, 2006
    #3
  4. Amit Dedhia

    Duane Hebert Guest

    "Noah Roberts" <> wrote in message
    news:...
    > 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>
    Duane Hebert, Oct 12, 2006
    #4
  5. Amit Dedhia

    Amit Dedhia Guest

    Ben Voigt wrote:
    > "Amit Dedhia" <> wrote in message
    > news:...
    > > 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 MyUnmanagedWrapper, has a data member declared as
    > >
    > > gcroot<Object^> handleToManagedClass;

    >
    > Should be
    > gcroot<MyManagedClass^> handleToManagedClass;
    >
    > >
    > > This is declared in the top of the class. It has wrapper methods whose
    > > implementation is something like this:
    > >
    > > void MyUnmanagedWrapper::Method1()
    > > {
    > > (*this)->Method1();

    > Should be
    > handleToManagedClass->Method1();
    >
    > > }
    > >
    > > and then I have...
    > >
    > > class MyApplicationClass
    > > {
    > > MyUnmanagedWrapperInstance.Method1();
    > > }
    > >
    > > 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 handleToManagedClass initialized? Initially it will hold a
    > nullptr. Testing a gcroot for nullptr is a pain, try:
    > (handleToManagedClass.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 MyUnmanagedWrapper. 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
    > >
    Amit Dedhia, Oct 12, 2006
    #5
  6. Amit Dedhia

    Ben Voigt Guest

    "Amit Dedhia" <> wrote in message
    news:...
    > 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 MyUnmanagedWrapper, has a data member declared as
    >
    > gcroot<Object^> handleToManagedClass;


    Should be gcroot<MyManagedClass^> handleToManagedClass;

    >
    > This is declared in the top of the class. It has wrapper methods whose
    > implementation is something like this:
    >
    > void MyUnmanagedWrapper::Method1()
    > {
    > (*this)->Method1();


    Should be handleToManagedClass->Method1();

    Thought I saw this exact same question with the exact same broken code a
    week ago.

    > }
    >
    > and then I have...
    >
    > class MyApplicationClass
    > {
    > MyUnmanagedWrapperInstance.Method1();
    > }
    >
    > 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 handleToManagedClass or leave it
    as nullptr?

    >
    > Best regards
    > Amit Dedhia
    >
    Ben Voigt, Oct 24, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weston Fryatt
    Replies:
    2
    Views:
    961
    Weston Fryatt
    Jan 1, 2005
  2. Arvind Ghai

    Drag n drop from managed to unmanaged apps

    Arvind Ghai, Feb 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    589
    Arvind Ghai
    Feb 15, 2005
  3. =?Utf-8?B?U2FuZHk=?=

    Managed and Unmanaged Code Confusion

    =?Utf-8?B?U2FuZHk=?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    529
    Kevin Spencer
    Oct 26, 2004
  4. Dave
    Replies:
    2
    Views:
    424
    Deming He
    Dec 13, 2003
  5. GAH
    Replies:
    0
    Views:
    493
Loading...

Share This Page