Calling Managed functions from unmanaged class

A

Amit Dedhia

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
 
B

Ben Voigt

Amit Dedhia said:
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 said:
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)
 
D

Duane Hebert

Noah Roberts said:
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>
 
A

Amit Dedhia

Ben said:
Should be

Should be
handleToManagedClass->Method1();

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?
 
B

Ben Voigt

Amit Dedhia said:
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 said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top