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

Alex Blekhman

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

Is XML writing the only reason for using ADO.NET? If yes,
then it seems like tremendous overkill. It's sufficient to
#import <msxml4.dll> and use perfectly native C++ classes to
do whatever XML requires.
 
G

Guest

Hi Amit,

You can use a managed class from any unmanaged C++ files directly. Here are
the steps.

1. Mark the unmanaged CPP as using managed extensions. This can be done from
the properties of the unmanaged CPP file in VS.Net 2003 or VS.Net 2005 IDE.

2. Pu the following declarations in your unmanaged CPP file where you want
to use the managed .Net ADO.Net and XML classes. Or better combine the
following declarations into a header file and use it in your unmanaged CPP
file

#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Xml;

3. If you want to wrap your ADO.Net and XMl managed code into an easy to use
class simply create a new CPP/header file in your MFC applicaiton and mark
the CPP as using managed extensions as described in step-1. Then use these
managed classes from your unmanaged code directly.

4. If you want to maintain the managed class as data member of the unmanaged
class then use "int" for the data member and store the GCHandle of the
managed class. This way your unmanaged header will still remain unmanaged and
the unmanaged CPP file can convert the GCHandle back and forth from int to
your managed class object.

This is called mixed mode programming in Managed C++ terms and is an
unmatched technique to use unamanged and manged code within the same CPP
file. This is only possible with Managed C++ (Managed C++ designers are
really great).

Let me know if you have any further queries in my explanation. I regularly
work with mixed mode classes in our product development.
 
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?
 
T

Tim Roberts

Adityanand Pasumarthi said:
Hi Amit,

You can use a managed class from any unmanaged C++ files directly. Here are
the steps.
...
This is called mixed mode programming in Managed C++ terms and is an
unmatched technique to use unamanged and manged code within the same CPP
file. This is only possible with Managed C++ (Managed C++ designers are
really great).

Let me know if you have any further queries in my explanation. I regularly
work with mixed mode classes in our product development.

This was a fabulous explanation. My compliments to you. I've marked it as
"keep forever".
 
B

Ben Voigt

Tim Roberts said:
This was a fabulous explanation. My compliments to you. I've marked it
as
"keep forever".

Only don't use "Managed C++" in VS2005, it's been replaced by C++/CLI.
 

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

Top