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.
--
Regards,
Aditya.P
"Amit Dedhia" wrote:
>
Ben Voigt wrote:
"Amit Dedhia" <am********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.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 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
>